我正在尝试测试按钮,但无法让动作侦听器工作
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!!!");
}
}
}