你如何在 Java 中将 ActionListener 添加到 JButton 上
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/284899/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you add an ActionListener onto a JButton in Java
提问by user37037
private JButton jBtnDrawCircle = new JButton("Circle");
private JButton jBtnDrawSquare = new JButton("Square");
private JButton jBtnDrawTriangle = new JButton("Triangle");
private JButton jBtnSelection = new JButton("Selection");
How do I add action listeners to these buttons, so that from a main method I can call actionperformed
on them, so when they are clicked I can call them in my program?
我如何向这些按钮添加动作侦听器,以便我可以从主方法调用actionperformed
它们,以便在单击它们时可以在我的程序中调用它们?
回答by Alex B
Your best bet is to review the Java Swing tutorials, specifically the tutorial on Buttons.
最好的办法是查看Java Swing 教程,特别是关于 Buttons的教程。
The short code snippet is:
简短的代码片段是:
jBtnDrawCircle.addActionListener( /*class that implements ActionListener*/ );
回答by AdamC
I'm didn't totally follow, but to add an action listener, you just call addActionListener(from Abstract Button). If this doesn't totally answer your question, can you provide some more details?
我并没有完全遵循,但是要添加一个动作侦听器,您只需调用addActionListener(来自抽象按钮)。如果这不能完全回答您的问题,您能否提供更多详细信息?
回答by David Koelle
Two ways:
两种方式:
1.Implement ActionListener in your class, then use jBtnSelection.addActionListener(this);
Later, you'll have to define a menthod, public void actionPerformed(ActionEvent e)
. However, doing this for multiple buttons can be confusing, because the actionPerformed
method will have to check the source of each event (e.getSource()
) to see which button it came from.
1.在你的类中实现ActionListener,然后使用jBtnSelection.addActionListener(this);
稍后,你必须定义一个方法,public void actionPerformed(ActionEvent e)
。但是,对多个按钮执行此操作可能会令人困惑,因为该actionPerformed
方法必须检查每个事件的来源 ( e.getSource()
) 以查看它来自哪个按钮。
2.Use anonymous inner classes:
2.使用匿名内部类:
jBtnSelection.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
selectionButtonPressed();
}
} );
Later, you'll have to define selectionButtonPressed()
.
This works better when you have multiple buttons, because your calls to individual methods for handling the actions are right next to the definition of the button.
稍后,您必须定义selectionButtonPressed()
. 当您有多个按钮时,这会更有效,因为您对处理操作的各个方法的调用就在按钮的定义旁边。
The second method also allows you to call the selection method directly. In this case, you could call selectionButtonPressed()
if some other action happens, too - like, when a timer goes off or something (but in this case, your method would be named something different, maybe selectionChanged()
).
第二种方法还允许您直接调用选择方法。在这种情况下,您也可以selectionButtonPressed()
在发生其他一些操作时调用- 例如,当计时器关闭时或某事时(但在这种情况下,您的方法可能会被命名为不同的名称selectionChanged()
)。
回答by Ronald Ortiz
I don't know if this works but I made the variable names
我不知道这是否有效,但我做了变量名
public abstract class beep implements ActionListener {
public static void main(String[] args) {
JFrame f = new JFrame("beeper");
JButton button = new JButton("Beep me");
f.setVisible(true);
f.setSize(300, 200);
f.add(button);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// Insert code here
}
});
}
}