繁体   English   中英

如何在Java中将ActionListener添加到框架中的按钮

[英]How to add an ActionListener to a button in a frame in Java

我正在尝试测试按钮,但无法使动作监听器正常工作

public class ButtonTester implements ActionListener {

static JLabel Label = new JLabel("Hello Buttons! Will You Work?!");
public static void main(String[] args) {
    //Creating a Label for step 3
    // now for buttons
    JButton Button1 = new JButton("Test if Button Worked");
    // step 1: create the frame
    JFrame frame = new JFrame ("FrameDemo");
    //step 2: set frame behaviors (close buttons and stuff)
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    //step 3: create labels to put in the frame
    frame.getContentPane().add(Label, BorderLayout.NORTH);
    frame.getContentPane().add(Button1, BorderLayout.AFTER_LAST_LINE);
    //step 4: Size the frame
    frame.pack();
    //step 5: show the frame 
    frame.setVisible(true);
    Button1.setActionCommand("Test");
    Button1.setEnabled(true);
    Button1.addActionListener(this); //this line here won't work
}
@Override
public void actionPerformed(ActionEvent e) {
    // TODO Auto-generated method stub
    if("Test".equals(e.getActionCommand()))
    {
        Label.setText("It Worked!!!");
    }
  }

}

this已经在没有上下文static方法

而是尝试使用类似Button1.addActionListener(new ButtonTester()); 代替...

更新

您可能还想看看Initial Threads,因为Swing有一些特殊要求。

静态方法不是一类的实例相关联,因此this不能使用。

您可以将所有代码从main移到ButtonTester的非静态方法(例如, run() ,然后从main执行以下操作:

new ButtonTester().run();

您还可以为ActionListener使用匿名内部类:

Button1.addActionLister(new ActionListener() {
    @Override public void actionPerformed (ActionEvent e) {
        // ... 
    }
});

Java表示您不能从静态上下文访问非静态实体(这是指非静态且main()是静态的对象),因此我们使用构造函数进行初始化:

public class ButtonTester implements ActionListener {
static JLabel Label = new JLabel("Hello Buttons! Will You Work?!");
ButtonTester()  //constructor
{
 //Creating a Label for step 3
    // now for buttons
    JButton Button1 = new JButton("Test if Button Worked");
    // step 1: create the frame
    JFrame frame = new JFrame ("FrameDemo");
    //step 2: set frame behaviors (close buttons and stuff)
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    //step 3: create labels to put in the frame
    frame.getContentPane().add(Label, BorderLayout.NORTH);
    frame.getContentPane().add(Button1, BorderLayout.AFTER_LAST_LINE);
    //step 4: Size the frame
    frame.pack();
    //step 5: show the frame 
    frame.setVisible(true);
    Button1.setActionCommand("Test");
    Button1.setEnabled(true);
    Button1.addActionListener(this); //this line here won't work
}


public static void main(String[] args) {
ButtonTester test1=new ButtonTester();// constructor will be invoked and new object created


}
@Override
public void actionPerformed(ActionEvent e) {
    // TODO Auto-generated method stub
    if("Test".equals(e.getActionCommand()))
    {
        Label.setText("It Worked!!!");
    }
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM