如何在Swing中组织我的动作? [英] How do I organize my Actions in Swing?
问题描述
我正在替换我的匿名ActionListeners
I am currently replacing my anonymous ActionListeners
new ActionListener() {
@Override
public void actionPerformed(final ActionEvent event) {
// ...
}
}
包含表示操作的类文件:
with class files representing actions:
public class xxxxxxxAction extends AbstractAction {
}
但是,我的GUI能够执行很多操作(例如CreatePersonAction,RenamePersonAction,DeletePersonAction,SwapPeopleAction等) 。)。
However, my GUI is able to perform a lot of actions (eg. CreatePersonAction, RenamePersonAction, DeletePersonAction, SwapPeopleAction, etc.).
有没有一种方法可以将这些类组织成一个连贯的结构?
Is there a good way to organize these classes into some coherent structure?
推荐答案
您可以将您的操作保存在单独的包中以隔离它们。有时,将它们保存在一个类中是有用的,特别是如果操作是相关的或具有共同的父级,例如:
You can keep your actions in a separate package to isolate them. Sometimes, it is useful to keep them in one class, especially if actions are related or have a common parent, for example:
public class SomeActions {
static class SomeActionX extends AbstractAction {
@Override
public void actionPerformed(ActionEvent e) {
}
}
static class SomeActionY extends AbstractAction {
@Override
public void actionPerformed(ActionEvent e) {
}
}
static class SomeActionZ extends AbstractAction {
@Override
public void actionPerformed(ActionEvent e) {
}
}
}
然后访问它们:
JButton button = new JButton();
button.setAction(new SomeActions.SomeActionX());
这篇关于如何在Swing中组织我的动作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!