抱歉标题,可能太笼统了.
Sorry for the title, probably too generic.
我已经阅读了如何编写动作监听器Java教程,我已经阅读了这个问题,但我仍然有一些疑问:我想知道当我必须多次执行相同的操作时哪种解决方案最好.
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.
我想重用相同的 ActionListener
,但我不确定如何以最佳方式实现这一点(从代码可读性、代码可维护性、性能和代码风格的角度来说)).
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();
}
}
);
这样我就不能重用任何 obv,因为它是一个匿名内部类...
This way I can't reuse anything obv, since it's an anonymous inner class...
现在,我可以想到以下解决方案:
Now, I can think of the following solutions:
static final
);ActionListener
接口的新类.static final
);ActionListener
interface.方案一的示例代码:
public static final MENU_ACTION_LISTENER = new ActionListener() {
public void actionPerformed(ActionEvent e) {
Navigator.showMenu();
}
};
btnMenu.addActionListener(MENU_ACTION_LISTENER);
解决方案2的示例代码:
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:
在哪里存储对匿名操作侦听器的引用?我可以有一种实用程序类(例如 ActionListenersUtil
),其中存储我想在 static final
字段中重用的所有动作侦听器,但我不喜欢它......在我看来,设计很糟糕.
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.
这更有意义,可能最好遵循 命令模式...我最初对这些包有些怀疑...我希望将所有侦听器放在一个单独的包中(例如 com.myapp.gui
用于 gui 元素和 com.myapp.gui.listeners
供听众使用.但是当我写下来时,我意识到我别无选择:唯一有意义的地方是在同一个包中(因为它们必须是包私有的),即使为了订单我想把它们都放在一个单独的包里.不过我仍然有一些疑问,因为即使在 GUI 发生变化的情况下,大多数动作侦听器也可能是可重用的;将它放在同一个包中仍然是个好主意吗?
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?
另一个问题:调用 btnMenu.addActionListener(MENU_ACTION_LISTENER);
和 btnMenu.addActionListener(new MenuActionListener());
有什么区别(就 JVM 而言,类加载、类编译、各类占用内存、垃圾回收等)?
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! :(
最好的方法是创建一个 Action
而不是 ActionListener
如果你必须将它附加到按钮、菜单……Action
是 JButton
的模型,模型是共享的
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
这还使您能够同时更改 Action
附加到的所有按钮的文本、图标、启用状态和其他属性,而无需多次调用.此外,它还可以附加到菜单项等.
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.
要创建 Action
,您不必从头开始.AbstractAction
是一个很好的起点.Swing Action
教程 也是很好的阅读.
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 最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!