我已经为此工作了两天,但我仍然无法弄明白。 所以我有这些课程:
第一堂课如下:
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);
谢谢你们,问题解决了!
答案 0 :(得分:3)
您正在创建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();
答案 1 :(得分:0)
我希望您在MyContentPane
中实例化一个ControlsPanel
,但ParametersPanel
会创建自己的ControlsPanel
。我认为你应该将ControlsPanel
传递给ParametersPanel
,而不是创建一个新的new
。请记住,每当您说public ParametersPanel() {
controlsPanel = new ControlsPanel(this);
时,您都会创建一个完全独立的对象。
改变这个:
public ParametersPanel(ControlPanel cp) {
controlsPanel = cp;
到此:
controlPanel = new ControlsPanel();
parametersPanel = new ParametersPanel(controlPanel);
并将MyControlPanel代码更改为:
public ControlsPanel() {
和ControlsPanel构造函数:
ParametersPanel
因为您似乎没有使用startIsPressed
的引用。
或者,您可以将{{1}}设置为静态变量,但我不认为这是您真正想要的。