我已经为此工作了两天,但我仍然无法弄清楚。
所以我有这些课程:


MyContentPane
参数面板
控制面板


第一类看起来像这样:

public class MyContentPane extends JPanel{

private ParametersPanel parametersPanel;
private ControlsPanel controlPanel;
private CashRegistersPanel cashRegistersPanel;

public MyContentPane() {

    parametersPanel = new ParametersPanel();
    controlPanel = new ControlsPanel(parametersPanel);
    cashRegistersPanel = new CashRegistersPanel();

    this.setLayout(null);
    this.add(controlPanel);
    this.add(parametersPanel);
    this.add(cashRegistersPanel);

    controlPanel.setBounds(0, 0, 300, 250);
    parametersPanel.setBounds(0, 250, 300, 450);
    cashRegistersPanel.setBounds(300, 0, 1500, 700);

    this.setPreferredSize(new Dimension(1800,700));

}


}

第二类看起来像这样:

public class ParametersPanel extends JPanel{

private ControlsPanel controlsPanel;

private JButton reset;

public ParametersPanel() {

    controlsPanel = new ControlsPanel(this);


    this.setBackground(new Color(219,221,255));

    reset       = new JButton("Reset parameters");

    reset.setFont(new Font("Arial", Font.BOLD, 14));

    this.setLayout(null);


    this.add(reset);

    reset.setBounds(10, 245, 280, 30);



    reset.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            boolean startIsPressed = controlsPanel.StartisPressed();
            System.out.println("Boolean: " + startIsPressed);
        }
    });

}


最后一类是:

public class ControlsPanel extends JPanel{

private JButton start;
private JButton stop;

private boolean startIsPressed;

public ControlsPanel(final ParametersPanel panel) {

    start       = new JButton("Start");
    stop        = new JButton("Stop");

    start.setFont(new Font("Arial", Font.BOLD, 14));
    stop.setFont(new Font("Arial", Font.BOLD, 14));

    this.setLayout(null);
    this.setBackground(new Color(199,202,255));

    this.add(start);
    this.add(stop);


    start.setBounds(10, 10, 280, 30);
    stop.setBounds(10, 50, 280, 30);

    stop.setEnabled(false);


    start.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {

        if (start.getText().equals("Start")) {
            start.setText("Pause");
            stop.setEnabled(true);
            startIsPressed = true;

            System.out.println("Start Button boolean value: " + startIsPressed + stringStartIsPressed);

        }
    });

public boolean StartisPressed() {
    return startIsPressed;
}


现在的问题是,一旦按下ControlsPanel中的“开始”按钮,布尔值就会变成true。但是,当我通过按下重置按钮在ParametersPanel中询问此布尔值时,它返回false。我发现如果我在第一类(MyContentPane)中更改面板清算的顺序,就可以解决问题,但是我不能在ControlsPanel中要求布尔值...

添加了另一堂课

public class CashRegistersPanel extends JPanel{

private Image img;
private int amount;
private ParametersPanel parametersPanel;
private ControlsPanel controlsPanel;
private boolean startIsPressed;

public CashRegistersPanel() {

    parametersPanel = new ParametersPanel();

    startIsPressed = controlsPanel.StartisPressed();

    this.setBackground(new Color(237,237,237));
    this.setLayout(null);

    CashRegister cashRegister = new CashRegister();
    img = cashRegister.getImg();

    amount = parametersPanel.getAmountOfRegisters();


}
    public void setControlsPanel(ControlsPanel cp) {
    controlsPanel = cp;

}


我按照你们的建议更改了MyContentPane并添加了更多内容:

public class MyContentPane extends JPanel{

private ParametersPanel parametersPanel;
private ControlsPanel controlPanel;
private CashRegistersPanel cashRegistersPanel;

public MyContentPane() {

    parametersPanel = new ParametersPanel();
    controlPanel = new ControlsPanel(parametersPanel);
    parametersPanel.setControlsPanel(controlPanel);
    cashRegistersPanel = new CashRegistersPanel(parametersPanel, controlPanel);


谢谢大家,问题解决了!

最佳答案

您要创建2个ControlsPanel

不应该在ParametersPanel中创建一个,这是令人反感的行:

controlsPanel = new ControlsPanel(this);


而是在ParametersPanel上创建一个setter方法:

public void setControlsPanel(ControlsPanel cp) {
    controlsPanel = cp;
}


并将第一类中的初始化更改为:

parametersPanel = new ParametersPanel();
controlPanel = new ControlsPanel(parametersPanel);
parametersPanel.setControlsPanel(controlPanel); // <- new line
cashRegistersPanel = new CashRegistersPanel();

10-04 11:49