Java 在单独的类上添加 ActionListener

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11888463/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 06:47:28  来源:igfitidea点击:

Java add ActionListener on a separate class

javaswingjframeactionlistener

提问by Ewen

Heres what I want to do, one of the classes is for a JFrame which contains all JButtons, I want another class to listen for the actions made on the JFrame class. See the code below:

这是我想要做的,其中一个类用于包含所有 JButton 的 JFrame,我想要另一个类来侦听在 JFrame 类上所做的操作。请参阅下面的代码:

public class Frame extends JFrame{
    //all the jcomponents in here

}

public class listener implements ActionListener{
    //listen to the actions made by the Frame class

}

Thanks for your time.

谢谢你的时间。

回答by JeffS

Just add a new instance of your listener to whatever components you want to listen. Any class that implements ActionListenercan be added as a listener to your components.

只需将您的监听器的新实例添加到您想要监听的任何组件中即可。任何实现的类ActionListener都可以作为侦听器添加到您的组件中。

public class Frame extends JFrame {
    JButton testButton;

    public Frame() {
        testButton = new JButton();
        testButton.addActionListener(new listener());

        this.add(testButton);
    }
}

回答by Kumar Vivek Mitra

1.You can use Inner Class, or Anonymous Inner Classto solve this....

1.您可以使用Inner Class, 或Anonymous Inner Class来解决这个问题....

Eg:

例如:

Inner Class

内部类

public class Test{

 Button b = new Button();


 class MyListener implements ActionListener{

       public void actionPerformed(ActionEvent e) {

                    // Do whatever you want to do on the button click.
      } 

   }
}

Eg:

例如:

Anonymous Inner Class

匿名内部类

public class Test{

     Button b = new Button();

     b.addActionListener(new ActionListener(){

        public void actionPerformed(ActionEvent e) {

                        // Do whatever you want to do on the button click.
          } 


   });

    }

回答by olagjo

If you want one and the same instance of listenerto listen to all of the buttons in the frame, you would have to make the actionPerformed method collect all clicks and delegate based on command:

如果您希望同一个 的实例listener侦听框架中的所有按钮,则必须使 actionPerformed 方法收集所有点击并基于命令进行委托:

public class listener extends ActionListener{
    public void actionPerformed(ActionEvent e){
        String command = e.getActionCommand();
        if (command.equals("foo")) {
            handleFoo(e);
        } else if (command.equals("bar")) {
            handleBar(e);
        }
    }

    private void handleFoo(ActionEvent e) {...}
    private void handleBar(ActionEvent e) {...}
}

which will become easier in Java 7, where you can switch over strings! The ActionCommand of a button click will be the Text-attribute of the JButton, unless you set it otherwise

这将在 Java 7 中变得更容易,您可以在其中切换字符串!点击一个按钮的ActionCommand将是Text的-attribute JButton,除非你设置它,否则