Java 为什么我能';t添加写入其他文件中的actionListener
我在另一个文件中有一个ActionListener&我想在我的gui代码中使用这个ActionListenerJava 为什么我能';t添加写入其他文件中的actionListener,java,swing,Java,Swing,我在另一个文件中有一个ActionListener&我想在我的gui代码中使用这个ActionListener package GUI; public class CheckButtonGUI { private JPanel mainPanel; private JButton One, Two, Three, Four; JLabel buttonStatus; public JPanel getGUI() { CheckButt
package GUI;
public class CheckButtonGUI {
private JPanel mainPanel;
private JButton One, Two, Three, Four;
JLabel buttonStatus;
public JPanel getGUI()
{
CheckButtonGUI cbg = new CheckButtonGUI();
mainPanel = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(5, 5, 0, 0);
One = new JButton("1");
Two = new JButton("2");
Three = new JButton("3");
Four = new JButton("4");
buttonStatus = new JLabel("No Button Pressed");
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = GridBagConstraints.RELATIVE;
gbc.fill = GridBagConstraints.HORIZONTAL;
mainPanel.add(One,gbc);
gbc.gridx = 4;
//gbc.gridy = 0;
mainPanel.add(Two,gbc);
gbc.gridx = 0;
gbc.gridy = 1;
gbc.gridwidth = GridBagConstraints.RELATIVE;
gbc.fill = GridBagConstraints.HORIZONTAL;
mainPanel.add(Three,gbc);
gbc.gridwidth = 1;
gbc.fill = GridBagConstraints.NONE;
gbc.gridx = 4;
mainPanel.add(Four,gbc);
gbc.gridwidth=3;
gbc.gridx = 0;
gbc.gridy = 2;
mainPanel.add(buttonStatus,gbc);
// Action Listeners
CheckListener cli = new CheckListener(this);
One.addActionListener(cli);
Two.addActionListener(cli);
Three.addActionListener(cli);
Four.addActionListener(cli);
return mainPanel;
}
public void displayText(String str)
{
buttonStatus.setText(str);
}
private static void createAndShowGUI() {
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("[=] JTextField of Dreams [=]");
CheckButtonGUI demo = new CheckButtonGUI();
frame.setContentPane(demo.getGUI());
frame.setLocation(550, 350);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// We no longer manually re-size, we use pack to automatically size the frame.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
每个&everything都可以正常工作,但只要我尝试添加actionListener,EclipseIDE就会显示错误。若我将其设置为null,则错误将被删除,但编译器将无法工作。我不明白我做错了什么。。。以下是我的actionListener,以防您需要更多详细信息
package GUI;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class CheckListener implements ActionListener
{
//private ButtoncheckGUI bcg;
private CheckButtonGUI cbg;
public CheckListener(ButtoncheckGUI b)
{
//bcg = b;
cbg = b;
}
public void actionPerformed(ActionEvent action)
{
String op = action.getActionCommand();
if(op.equals("1"))
{
cbg.displayText("Button 1 was Pressed");
}else if (op.equals("2"))
{
cbg.displayText("Button 2 was Pressed");
}else if (op.equals("3"))
{
cbg.displayText("Button 3 was Pressed");
}else if (op.equals("4"))
{
cbg.displayText("Button 4 was Pressed");
}else
{
cbg.displayText("unExpected Input");
}
}
}
实际上,错误在actionListener的构造函数参数中。i、 不知何故,用错误的“gui模型”对象实例化actionListner的构造函数是错误的。如您所见,在我的控制器类(即actionlistener)中,ButtonChGui b被实例化,而不是CheckButtonGui b。用CheckButtonGUI替换ButtoncheckGUI解决了问题 您为
CheckListener
提供了代码,但在代码的其他任何地方都没有引用它。也许这就是你的问题。@user489041我更改了它,但每当按下putton时,我都要访问actionListener。@WhiskySpider请查看更正后的代码。我可以在其他GUI模型(//private-ButtoncheckGUI-bcg)中使用相同的文件,但是当我为这个GUI模型实现相同的方法(private-CheckButtonGUI-cbg)时,它会显示错误。在另一个模型中,我使用相同的方法引用了相同的actionListener,但我不知道这里有什么问题。我必须提到的一件事是:在另一个gui模型中,我返回这个面板,即返回这个,但在这里我返回,即返回主面板。其余的代码是一样的!