2013-08-07 86 views
1

我想测试一下按钮,但我不能让一个动作监听工作如何一个ActionListener添加到按钮的边框在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!!!"); 
    } 
    } 

} 
+1

您应该在Swing事件线程上使用SwingUtilities.invokeLater()在main()中创建Swing组件。让ButtonTester实现Runnable,将代码从main()移动到run(),然后从main()中执行SwingUtilities.invokeLater(new ButtonTester())将一次解决许多问题。 –

+0

请学习java命名约定并遵守它们。 – kleopatra

回答

3

this在一个static方法没有上下文

相反,尝试使用类似Button1.addActionListener(new ButtonTester());,而不是...

更新

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

+0

也是一个很好的解决方案。 –

3

静态方法与类的实例无关,因此无法使用this

你可以从主从主把所有的代码ButtonTester的非静态方法(比如,run(),例如)和做这样的事情:

new ButtonTester().run(); 

你也可以使用匿名内部类为ActionListener的:

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

同意,不喜欢在'main'方法中逗留。也忘了提及初始线程 – MadProgrammer

+1

即使一个普通的内部类可能是好的,如果你想改变或访问按钮的属性,并希望更多的可访问性。 – snickers10m

1

java的说,你不能从静态上下文访问非静态的实体(这是指一个对象,它是静态的非主和()是静态的),所以我们使用的构造函数用于初始化:

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!!!"); 
    } 
    } 
} 
相关问题