如何在Java中向框架中的按钮添加ActionListener
我正在尝试测试按钮,但我无法让动作侦听器工作如何在Java中向框架中的按钮添加ActionListener,java,swing,jframe,jbutton,actionlistener,Java,Swing,Jframe,Jbutton,Actionlistener,我正在尝试测试按钮,但我无法让动作侦听器工作 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 JB
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!!!");
}
}
}
此
在静态
方法中没有上下文
相反,尝试使用类似于Button1.addActionListener(new ButtonTester())的东西代码>而不是
已更新
您可能还想了解一下Swing有一些特殊的要求……此
在静态方法中没有上下文
相反,尝试使用类似于Button1.addActionListener(new ButtonTester())的东西代码>而不是
已更新
您可能还想了解一下,因为Swing有一些特殊的要求……静态方法与类的实例不关联,因此不能使用此方法
您可以将所有代码从main移动到ButtonTest的非静态方法(例如,run()
),并从main执行类似操作:
new ButtonTester().run();
您还可以为ActionListener使用匿名内部类:
Button1.addActionLister(new ActionListener() {
@Override public void actionPerformed (ActionEvent e) {
// ...
}
});
静态方法与类的实例不关联,因此不能使用this
您可以将所有代码从main移动到ButtonTest的非静态方法(例如,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!!!");
}
}
}
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!!!");
}
}
}
您应该在Swing事件线程上使用main()中的SwingUtilities.invokeLater()创建Swing组件。让ButtonTester实现Runnable,将代码从main()移动到run(),然后执行SwingUtilities。从main()执行invokeLater(新ButtonTester())将一次解决许多问题。请学习java命名约定并遵守它们。您应该使用SwingUtilities.invokeLater()在Swing事件线程上创建Swing组件在main()中。让ButtonTester实现Runnable,将代码从main()移动到run(),然后执行SwingUtilities。从main()调用invokeLater(new ButtonTester())将一次解决许多问题。请学习java命名约定并坚持这些约定。同意,不喜欢停留在main
方法中。还忘了提到初始线程七如果您想更改或访问按钮的属性并希望获得更多的可访问性,那么常规的内部类可能会很好。同意,不要停留在main
方法中。还忘了提到初始threadsEven,如果您想要更改或访问按钮的属性,并且想要更多的可访问性,那么常规的内部类可能会很好。