Click事件-从另一个类访问布尔变量[关闭]

Click事件-从另一个类访问布尔变量[关闭],第1张

Click事件-从另一个类访问布尔变量[关闭]

您的问题涉及不同类的对象之间的通信,并且有几种方法可以实现此目的,但是最基本的方法是将一个类中的对象的方法调用到另一个类中。

首先让我们设置问题,…我创建了名为MyPanel2和Checkers2的类,以区分它们与您的类。

假设在MyPanel2中,我们有一个Checkers2字段和一个名为selected的布尔值字段,并将其设置为false:

private Checkers2 checkers;private boolean selected = false;

以及适当的布尔型getter和setter:

public void setSelected(boolean selected) {    this.selected = selected;}public boolean isSelected() {    return selected;}

并说在Checkers2类中,数组中包含10个MyPanel2实例,并且您希望用户能够“选择”该类的实例,但只允许选择其中的7个,并假设您想要要使用当前使用的设置,可以给主类一个方法

publicbooleanisPanelDisabled()
,并让MyPanel2类调用此方法来确定是否允许选择。因此,在MyPanel2中,您可以拥有一个类似以下内容的MouseListener:

@Overridepublic void mousePressed(MouseEvent e) {    if (selected) {        return;    }    // call the Checkers2 boolean method to check    if (checkers.isPanelDisabled()) {        setBackground(DISABLED_COLOR);    } else {        setBackground(SELECTED_COLOR);        setSelected(true);    }}

在Checkers2

.isPanelDisabled()
方法中,您将遍历MyPanel2实例数组以查看已选择了多少实例,类似的方法可能会起作用:

public boolean isPanelDisabled() {    int count = 0;    for (MyPanel2 panel2 : myPanels) {        if (panel2.isSelected()) { count++;        }    }    return count >= MAX_COUNT;}

整个MCVE可测试代码如下所示:

import java.awt.BorderLayout;import java.awt.Color;import java.awt.Dimension;import java.awt.GridLayout;import java.awt.event.KeyEvent;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import javax.swing.BorderFactory;import javax.swing.JButton;import javax.swing.Jframe;import javax.swing.JPanel;import javax.swing.SwingUtilities;public class Checkers2 extends Jframe {    private static final int MAX_COUNT = 7;    private final int ROWS = 2;    private final int COLS = 5;    private final int GAP = 2;    private final int NUM = ROWS * COLS;    private MyPanel2[] myPanels = new MyPanel2[NUM];    public Checkers2() {        super("Checkers");        setDefaultCloseOperation(EXIT_ON_CLOSE);        JPanel gridPanel = new JPanel(new GridLayout(ROWS, COLS, 1, 1));        gridPanel.setBackground(Color.BLACK);        gridPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK));        for (int i = 0; i < myPanels.length; i++) { MyPanel2 myPanel = new MyPanel2(this); gridPanel.add(myPanel); myPanels[i] = myPanel;        }        JButton resetButton = new JButton("Reset");        resetButton.setMnemonic(KeyEvent.VK_R);        resetButton.addActionListener(evt -> { for (MyPanel2 myPanel2 : myPanels) {     myPanel2.reset(); }        });        JButton exitButton = new JButton("Exit");        exitButton.setMnemonic(KeyEvent.VK_X);        exitButton.addActionListener(evt -> System.exit(0));        JPanel buttonPanel = new JPanel();        buttonPanel.add(resetButton);        add(gridPanel);        add(buttonPanel, BorderLayout.PAGE_END);        pack();        setLocationRelativeTo(null);    }    public boolean isPanelDisabled() {        int count = 0;        for (MyPanel2 panel2 : myPanels) { if (panel2.isSelected()) {     count++; }        }        return count >= MAX_COUNT;    }    public static void main(String[] args) {        SwingUtilities.invokeLater(() -> { new Checkers2().setVisible(true);        });    }}class MyPanel2 extends JPanel {    private static final int PREF_W = 200;    private static final int PREF_H = PREF_W;    private static final int GR = 240;    public static final Color base_COLOR = new Color(GR, GR, GR);    public static final Color DISABLED_COLOR = Color.CYAN;    public static final Color SELECTED_COLOR = Color.BLACK;    private Checkers2 checkers;    private boolean selected = false;    public MyPanel2(Checkers2 checkers) {        setBackground(base_COLOR);        this.checkers = checkers;        setPreferredSize(new Dimension(PREF_W, PREF_H));        addMouseListener(new MyMouse());    }    public void setSelected(boolean selected) {        this.selected = selected;    }    public boolean isSelected() {        return selected;    }    public void reset() {        setBackground(base_COLOR);        setSelected(false);    }    private class MyMouse extends MouseAdapter {        @Override        public void mousePressed(MouseEvent e) { if (selected) {     return; } if (checkers.isPanelDisabled()) {     setBackground(DISABLED_COLOR); } else {     setBackground(SELECTED_COLOR);     setSelected(true); }        }    }}

另一个 选择是 MyPanel中提取所有逻辑,并将其放入主程序中,例如:

import java.awt.*;import java.awt.event.*;import javax.swing.*;@SuppressWarnings("serial")public class Checkers3 extends JPanel {    private static final int MAX_COUNT = 7;    private final int ROWS = 2;    private final int COLS = 5;    private final int GAP = 2;    private final int NUM = ROWS * COLS;    private MyPanel3[] myPanels = new MyPanel3[NUM];    public Checkers3() {        JPanel gridPanel = new JPanel(new GridLayout(ROWS, COLS, 1, 1));        gridPanel.setBackground(Color.BLACK);        gridPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK));        MyMouse myMouse = new MyMouse();        for (int i = 0; i < myPanels.length; i++) { MyPanel3 myPanel = new MyPanel3(); myPanel.addMouseListener(myMouse); gridPanel.add(myPanel); myPanels[i] = myPanel;        }        JButton resetButton = new JButton("Reset");        resetButton.setMnemonic(KeyEvent.VK_R);        resetButton.addActionListener(evt -> { for (MyPanel3 myPanel : myPanels) {     myPanel.reset(); }        });        JButton exitButton = new JButton("Exit");        exitButton.setMnemonic(KeyEvent.VK_X);        exitButton.addActionListener(evt -> System.exit(0));        JPanel buttonPanel = new JPanel();        buttonPanel.add(resetButton);        setLayout(new BorderLayout());        add(gridPanel);        add(buttonPanel, BorderLayout.PAGE_END);    }    public boolean isPanelDisabled() {        int count = 0;        for (MyPanel3 panel : myPanels) { if (panel.isSelected()) {     count++; }        }        return count >= MAX_COUNT;    }    private class MyMouse extends MouseAdapter {        @Override        public void mousePressed(MouseEvent e) { MyPanel3 myPanel = (MyPanel3) e.getSource(); if (myPanel.isSelected()) {     return; // it's already selected } else if (isPanelDisabled()) {     myPanel.setSelected(false); } else {     myPanel.setSelected(true); }        }    }    private static void createAndShowGui() {        Checkers3 mainPanel = new Checkers3();        Jframe frame = new Jframe("Checkers");        frame.setDefaultCloseOperation(Jframe.EXIT_ON_CLOSE);        frame.getContentPane().add(mainPanel);        frame.pack();        frame.setLocationRelativeTo(null);        frame.setVisible(true);    }    public static void main(String[] args) {        SwingUtilities.invokeLater(() -> createAndShowGui());    }}@SuppressWarnings("serial")class MyPanel3 extends JPanel {    private static final int PREF_W = 200;    private static final int PREF_H = PREF_W;    private static final int GR = 240;    public static final Color base_COLOR = new Color(GR, GR, GR);    public static final Color DISABLED_COLOR = Color.CYAN;    public static final Color SELECTED_COLOR = Color.BLACK;    private boolean selected = false;    public MyPanel3() {        setBackground(base_COLOR);        setPreferredSize(new Dimension(PREF_W, PREF_H));    }    public void setSelected(boolean selected) {        this.selected = selected;        Color background = selected ? SELECTED_COLOR : DISABLED_COLOR;        setBackground(background);    }    public boolean isSelected() {        return selected;    }    public void reset() {        setSelected(false);        setBackground(base_COLOR);    }}

但是 BEST 选项是将所有逻辑放在一个或多个单独的模型类中,并使GUI尽可能哑。



欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5507405.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-13
下一篇 2022-12-13

发表评论

登录后才能评论

评论列表(0条)

保存