[英]How can I access a JButton on another file or class in Java
I'm having a problem with JButton.我的 JButton 有问题。 I need to change the text on the goPauseButton when it has been clicked, but I get this error: goPauseButton cannot be resolved
.单击 goPauseButton 后,我需要更改它上的文本,但出现此错误: goPauseButton cannot be resolved
。 I'm quite new to Java, so I started trying to solve the issue using techniques from other languages such as Free Pascal.我对 Java 很陌生,所以我开始尝试使用其他语言(如 Free Pascal)的技术来解决这个问题。 There you need to refer to the class where the button is in, and then the button.在那里你需要引用按钮所在的类,然后是按钮。 In my code it would look like this:在我的代码中,它看起来像这样:
PrisonersDilemma.goPauseButton.setText("Pause");
But then I get this error: Cannot make a static reference to the non-static field PrisonersDilemma.goPauseButton
但是后来我收到了这个错误: Cannot make a static reference to the non-static field PrisonersDilemma.goPauseButton
This is my code (so far), I've erased unimportant things:这是我的代码(到目前为止),我已经删除了不重要的东西:
Main class主班
import java.awt.BorderLayout;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.SwingUtilities;
import javax.swing.JButton;
import javax.swing.JLabel;
import java.util.Hashtable;
//...
public class PrisonersDilemma /* possible extends... */ {
// declaring
JFrame frame;
PlayingField field;
JPanel componentPanel;
public JButton goPauseButton;
public JPanel createComponentPanel() {
componentPanel = new JPanel();
componentPanel.setLayout(new GridLayout(2,6));
// set goPauseButton
goPauseButton = new JButton("GO!");
goPauseButton.addActionListener(field);
goPauseButton.setBounds(110,350, 80,20); // first coordinates, then size
frame.add(goPauseButton);
return componentPanel;
}
void buildGUI() {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
field = new PlayingField();
// set frame
frame = new JFrame("Prisoners Dilemma");
frame.add(field);
createComponentPanel();
frame.add(field, BorderLayout.CENTER);
frame.setLocation(200, 200);
frame.pack();
frame.setVisible(true);
frame.setSize(400, 450);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
} );
}
Class with ActionEventHandler带有 ActionEventHandler 的类
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.JButton;
public class PlayingField extends JPanel
implements ActionListener,
ChangeListener {
private boolean started;
@Override
public void actionPerformed(ActionEvent e) {
// TODO
if ("GO!".equals(e.getActionCommand())){
System.out.println("GO!");
started = true;
goPauseButton.setText("Pause"); // here is the error
} else if ("Pause".equals(e.getActionCommand())){
System.out.println("Pause");
started = false;
} else if ("Reset".equals(e.getActionCommand())){
System.out.println("Reset");
}
}
}
I think you need to change the way you're approaching the problem.我认为您需要改变处理问题的方式。 The PlayingField
has no responsibility for modifying the state of the goPauseButton
in PrisonersDilemma
. PlayingField
不负责修改PrisonersDilemma
goPauseButton
的状态。 Instead, PrisonersDilemma
should update the goPauseButton
and call an appropriate method of PlayingField
相反, PrisonersDilemma
应该更新goPauseButton
并调用适当的PlayingField
方法
For example...例如...
goPauseButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
goPauseButton.setText("Pause");
field.start();
}
});
And...和...
public class PlayingField extends JPanel {
public void start() {
System.out.println("GO!");
started = true;
}
public void pause() {
started = false;
System.out.println("Pause");
}
public void reset() {
System.out.println("Reset");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.