简体   繁体   English

你如何在 Java 中将 ActionListener 添加到 JButton 上

[英]How do you add an ActionListener onto a JButton in Java

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 ,因此当它们被单击时,我可以在我的程序中调用它们?

Two ways:两种方式:

1. Implement ActionListener in your class, then use jBtnSelection.addActionListener(this); 1.在你的类中实现ActionListener,然后使用jBtnSelection.addActionListener(this); Later, you'll have to define a menthod, public void actionPerformed(ActionEvent e) .稍后,您必须定义一个方法, 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.但是,对多个按钮执行此操作可能会令人困惑,因为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() .稍后,您必须定义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.当您有多个按钮时,这会更有效,因为您对处理操作的各个方法的调用就在按钮的定义旁边。

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() )。

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*/ );

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
            }
        });
    }
}

I'm didn't totally follow, but to add an action listener, you just call addActionListener (from Abstract Button).我并没有完全遵循,但是要添加一个动作侦听器,您只需调用addActionListener (来自抽象按钮)。 If this doesn't totally answer your question, can you provide some more details?如果这不能完全回答您的问题,您能否提供更多详细信息?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM