簡體   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