简体   繁体   English

在扩展Button的类中定义和ActionListener

[英]Define and ActionListener in class which extends Button

How to place Action Listener directly in definition of class that extends Button ? 如何将Action Listener直接放置在扩展Button的类的定义中?

If object of class Button is created then we could simply use anonumous inner class : 如果创建了Button类的对象,那么我们可以简单地使用匿名内部类:

b = new Button("Click me");
b.addActionListener(
                    new ActionListener(){
                        public void actionPerformed(ActionEvent e){
                            System.out.println("stringToPrint");
                        }
                    }
                );

how to do the same in below :
class CustomizedButton extends Button{
   String customClass;

   Button(String stringToPrint){
      super(customClass); //customClass is also button name
      this customString = stringToPrint;
   }

   /*this.addActionListener( //don't work this way
       new ActionListener(){
            public void actionPerformed(ActionEvent e){
                System.out.println(customClass);//use outer(?) field
            }
        }
     );*/
}

I need to create 20 almost identical but slightly different buttons, so anonymous inner is too long 我需要创建20个几乎相同但略有不同的按钮,所以匿名内部太长了

You could declare a private nested class, like so: 您可以声明一个私有嵌套类,如下所示:

public class CustomizedButton extends Button{
    String customClass;

    CustomizedButton(String stringToPrint){
        super(customClass); //customClass is also button name
        this.customString = stringToPrint;
        addActionListener(new MyListener());
    }

    private class MyListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            // TODO: listener code here
        }
    }
}

But it's not much different from using an anonymous inner class or lambda: 但这与使用匿名内部类或lambda并没有太大区别:

public class CustomizedButton extends Button{
    String customClass;

    CustomizedButton(String stringToPrint){
        super(customClass); //customClass is also button name
        this.customString = stringToPrint;
        addActionListener(e -> myListenerCode(e));
    }

    private void myListenerCode(ActionEvent e) {
        // TODO: listener code here
    }

}

Having said this, other issues come to mind: 说了这么多,我想到了其他问题:

  • Usually it's best to favor composition over inheritance. 通常,最好是继承优先于合成。 I would bet that what you really want is some sort of factory method that creates your button complete with listener 我敢打赌,您真正想要的是某种工厂方法,可以创建带有侦听器的按钮
  • Why use AWT components such as the java.awt.Button class when it is 20+ yrs out of date? 如果过期20年以上,为什么还要使用java.awt.Button类之类的AWT组件? Why not Swing JButtons instead? 为什么不摆JButton呢?
  • If you were using Swing JButtons, best would be to create a custom Action rather than extend JButton. 如果使用的是Swing JButton,最好是创建一个自定义Action而不是扩展JButton。 Actions can hold and change many button properties, including a listener, the displayed text, icons, the tool tip text (displayed on hover).... 动作可以保留和更改许多按钮属性,包括侦听器,显示的文本,图标,工具提示文本(悬停显示)...。
  • For that matter, you should favor JavaFX if this is a new project, since this is the current best-supported Java GUI library. 因此,如果这是一个新项目,则应首选JavaFX,因为这是当前受支持程度最高的Java GUI库。

For example an AbstractAction class could look something like: 例如,AbstractAction类可能类似于:

public class CustomizedAction extends AbstractAction{
    String text;

    CustomizedAction(String text, int mnemonic){
        super(text); //text is also button name
        this.text = text;
        putValue(MNEMONIC_KEY, mnemonic); // for alt-key short cut if desired
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        String currentName = getValue(NAME); // same value as the text field
        System.out.println(currentName);

        // TODO: more listener code here
    }

}

and could be used like so: 可以这样使用:

JButton button = new JButton(new CustomizedAction("Foo", KeyEvent.VK_F));

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

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