ActionListener最佳实践 [英] ActionListener best practices

查看:188
本文介绍了ActionListener最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我已经阅读了如何编写Action Listener 教程,我已经阅读了这个问题,但我仍然有一些疑问:我想知道哪个解决方案是最好的,当我必须多次执行相同的操作。



我想重复使用相同的 ActionListener ,但我不知道如何以最好的方式实现这个期限:代码可读性,代码可维护性,性能和代码风格)。



首先是标准代码(如果我不会重用动作监听器):

  btnMenu.addActionListener(
new ActionListener(){
public void actionPerformed e){
Navigator.showMenu();
}
}
);

这样我就无法重用任何obv,因为它是一个匿名的内部类...

现在,我可以想到以下解决方案:


  1. 存储一个匿名内部类在一个字段中(最有可能是 static final );

  2. 编写一个实现 ActionListener 接口。

解决方案1的示例代码:

  public static final MENU_ACTION_LISTENER = new ActionListener(){
public void actionPerformed(ActionEvent e){
Navigator.showMenu();
}
};

btnMenu.addActionListener(MENU_ACTION_LISTENER);

解决方案2的代码示例:

  // package-private,只有GUI包类才能使用它。 
//很可能我不会需要将其子类化,所以为什么不使它成为最终?
final class MenuActionListener实现ActionListener {
public void actionPerformed(ActionEvent e){
Navigator.showMenu();
}
}

//现在,无论我需要使用它:
btnMenu.addActionListener(new MenuActionListener());

我对这两种解决方案有些疑问:


  1. 在哪里存储对匿名动作侦听器的引用?我可以使用一种实用类(例如 ActionListenersUtil ),其中存储我想在 static final 中重用的所有动作监听器但是我不喜欢它...在我看来,设计不好。


  2. 更有意义的是,最好遵循命令模式 ...我最初对这些软件包有一些疑问...我想让所有的听众gui元素的单独包(例如 com.myapp.gui 和用于侦听器的 com.myapp.gui.listeners 。但是当我写下来的时候,我意识到我别无选择:唯一有意义的地方是在同一个包中(因为他们必须是包私人的),即使为了订单我想把它们全部放在一个单独的包中
    我仍​​然有一些疑问,因为大多数动作侦听器可能是可重用的,甚至在GUI改变的情况下,它仍然是一个好主意另一个问题是:调用 btnMenu.addActionListener(MENU_ACTION_LISTENER)之间有什么区别?



$ b ;
btnMenu.addActionListener(new MenuActionListener()); (根据JVM的说法,类加载,类编译,每个类占用的内存,垃圾收集等)?



请帮忙,我现在很困惑! :(

解决方案

最好的方法是创建一个 Action 一个 ActionListener ,如果你必须将它附加到按钮,菜单...。 Action 是一个 JButton ,该模型旨在共享



这也使您能够更改文本,图标,启用状态和 Action 所有按钮的其他属性同时没有多个调用,此外,它可以附加到菜单项,如



要创建一个 Action ,您不必从头开始。 AbstractAction 是一个好的起点。 Swing 动作教程也是一个很好的阅读。



Sorry for the title, probably too generic.

I already read How to Write an Action Listener tutorial by Java and I already read this question, but I still have some doubts: I was wondering which solution is the best when I have to perform the same action multiple time.

I'd want to reuse the same ActionListener, but I am not sure on how to achieve this in the best way (speaking in term of: code readability, code mantainability, performance and code style).

First the "standard" code (which I would use if I'm not going to reuse the action listener):

btnMenu.addActionListener(
    new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            Navigator.showMenu();
        }
    }
);

This way I can't reuse anything obv, since it's an anonymous inner class...

Now, I can think of the following solutions:

  1. Store a reference of an Anonymous Inner Class in a field (that will most likely be static final);
  2. Write a new class that implements ActionListener interface.

Example code for solution 1:

public static final MENU_ACTION_LISTENER = new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        Navigator.showMenu();
    }
};

btnMenu.addActionListener(MENU_ACTION_LISTENER);

Example code for solution 2:

// package-private, only GUI-package classes should be able to use it.
// most likely I won't ever need to subclass it, so why not making it final?
final class MenuActionListener implements ActionListener  {
    public void actionPerformed(ActionEvent e) {
        Navigator.showMenu();
    }
}

// now, wherever I need to use it:
btnMenu.addActionListener(new MenuActionListener());

I have some doubts about both solutions:

  1. Where to store references to Anonymous Action Listeners? I could have a sort of utility class (e.g. ActionListenersUtil) where store all action listeners I want to reuse in static final fields, but I don't like it... it seems to me poor design.

  2. It makes more sense, probably best follows the Command Pattern... I initially had some doubt about the packages... I'd like to have all listeners in a separate package (e.g. com.myapp.gui for gui elements and com.myapp.gui.listeners for listeners. But when I wrote down this I realized that I have no choice: the only place where it make sense is in the same package (because they must be package-private), even if for the sake of order I would have like to put them all in a separate package. I still have some doubt though, because most action listeners may be reusable even in case GUI changes; it still would be a good idea to have it in the same package?

Another question: what's the difference between calling btnMenu.addActionListener(MENU_ACTION_LISTENER); and btnMenu.addActionListener(new MenuActionListener()); (speaking in term of JVM, class loading, class compiling, memory occupied by each class, garbage collection and so on)?

Please help, I'm so confused right now! :(

解决方案

The best way is to create an Action instead of an ActionListener if you have to attach it to buttons, menus, ... . An Action is the model of a JButton, and the model is meant to be shared

This also gives you the ability to change the text, icon, enabled status, and other properties of all the buttons that the Action is attached to at the same time without multiple calls. Plus, it can be attached to menu items and such as well.

To create an Action you do not have to start from scratch. AbstractAction is a good starting point. The Swing Action tutorial is also a good read.

这篇关于ActionListener最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆