简体   繁体   English

如何在Java Swing中为多个按钮添加多个ActionListener

[英]How to add multiple ActionListeners for multiple buttons in Java Swing

I know how to create one button and an Action Listener for it. 我知道如何为它创建一个按钮和一个动作监听器。 But I want to have several buttons and actionListeners for them doing separate actions unrelated to each other. 但是我希望它们有几个按钮和actionListeners,它们可以执行彼此无关的单独操作。

Example: 例:

protected JButton x;

x = new JButton("add");
x.addActionListener(this);

public void actionPerformed(ActionEvent evt) { //code.....}

Now I want to have other buttons which may hav different functions like subtract, multiply etc. please suggest. 现在我想要其他按钮可能有不同的功能,如减法,乘法等,请建议。 thanks 谢谢

What about: 关于什么:

    JButton addButton = new JButton( new AbstractAction("add") {
        @Override
        public void actionPerformed( ActionEvent e ) {
            // add Action
        }
    });

    JButton substractButton = new JButton( new AbstractAction("substract") { 
        @Override
        public void actionPerformed( ActionEvent e ) {
            // substract Action
        }
    });

Use inner classes: 使用内部类:

x = new JButton("add"); 
x.addActionListener(
  new ActionListener() {
    public void actionPerformed(ActionEvent e) {
      //your code here
    }
  }
);

您可以使用ActionEvent.getSource()来确定源并相应地执行操作,也可以为每个源定义不同的ActionListeners

how about... 怎么样...

protected JButton x, z, a, b,c;

x = new JButton("add x");
z = new JButton("add z");
a = new JButton("add a");
b = new JButton("add b");
c = new JButton("add c");
x.addActionListener(this);
z.addActionListener(this);
a.addActionListener(this);
b.addActionListener(this);
c.addActionListener(this);

then 然后

public void actionPerformed(ActionEvent evt)
{
     if (evt.getSource()==x)
         {
            //do something
         }
     else if (evt.getSource()==z)
         {
            //do something
         }
     else if (evt.getSource()==a)
         {
            //do something
         }
     else if (evt.getSource()==b)
         {
            //do something
         }
     else if (evt.getSource()==c)
         {
            //do something
         }
}

this is always works for me, but honestly I'm not sure if it's not bad practice 这对我来说总是有用,但说实话,我不确定这是不是不好的做法

You just need to create new instance of the ActionListener each time. 您只需要每次都创建ActionListener新实例。 BTW for lots of reasons it is recommended to use Action 's instead. BTW由于很多原因,建议使用Action代替。

Here is nice resource which also explains why you should go with using Actions over ActionListeners, a Java tutorial titled How to Use Actions 这里有一个很好的资源,它也解释了为什么你应该使用Action over ActionListeners,一个名为How to Use Actions的Java教程

EDIT : @fmucar is right you can do it all in a single ActionListener. 编辑 :@fmucar是对的,你可以在一个ActionListener中完成所有操作。 Though having separate functional Actions allows you to reuse them independently. 虽然具有单独的功能操作允许您独立地重用它们。

You can create different action listener instances, not using your class: 您可以创建不同的动作侦听器实例,而不是使用您的类:

x.addActionListener(new ActionListener(){
    @Override
    public void actionPerformed(ActionEvent e)
    { ... }
});

There are several other methods to create action listener, just like any class, but for short actions this (anonymous class) is a convenient way. 还有其他一些方法来创建动作侦听器,就像任何类一样,但对于简短的动作,这个(匿名类)是一种方便的方法。

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

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