简体   繁体   English

如何在 Swing 中组织我的操作?

[英]How do I organize my Actions in Swing?

I am currently replacing my anonymous ActionListeners我目前正在更换我的匿名 ActionListeners

new ActionListener() {
    @Override
    public void actionPerformed(final ActionEvent event) {
        // ...
    }
}

with class files representing actions:使用代表动作的类文件:

public class xxxxxxxAction extends AbstractAction {
}

However, my GUI is able to perform a lot of actions (eg. CreatePersonAction, RenamePersonAction, DeletePersonAction, SwapPeopleAction, etc.).但是,我的 GUI 能够执行很多操作(例如 CreatePersonAction、RenamePersonAction、DeletePersonAction、SwapPeopleAction 等)。

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) {
        }
    }
}

Then to access them:然后访问它们:

JButton button = new JButton();
button.setAction(new SomeActions.SomeActionX());

I'm just feeling the strain of converting ~60 ActionListeners into separate classes.我只是感觉到将 ~60 个 ActionListeners 转换为单独的类的压力。

Only you can decide if 60 is minimal.只有您可以决定60是否是最小的。 This example uses four instances of a single class.示例使用单个类的四个实例。 StyledEditorKit , seen here , is a good example if grouping as a series of static factory methods .如果将这里看到的StyledEditorKit分组为一系列静态工厂方法,它就是一个很好的例子。 The example cited here uses nested classes.此处引用的示例使用嵌套类。 JHotDraw , cited here , generates suitable actions dynamically.此处引用的JHotDraw动态生成合适的动作。

First of all, you should provide public methods for all actionPerformed used in your actions (createPerson, removePerson, etc.).首先,您应该为您的操作(createPerson、removePerson 等)中使用的所有 actionPerformed 提供公共方法。 All these action methods should be in one class (I call it PersonController).所有这些操作方法都应该在一个类中(我称之为 PersonController)。 Then, you need to define your AbstractPersonAction:然后,您需要定义您的 AbstractPersonAction:

public class AbstractPersonAction extends AbstractAction {
  private PersonController controller;

  public AbstractPersonAction(PersonController aController) {
    controller = aController;
  }

  protected PersonController getController() {
    return controller;
  }
}

Now you can extract all your actions into separate classes.现在,您可以将所有操作提取到单独的类中。

public class CreatePersonAction extends AbstractPersonAction {

  public CreatePersonAction(PersonController aController) {
    super(controller);
  }

  public void actionPerformed(ActionEvent ae) {
    getController().createPerson();
  }
}

These actions can be a part of an outer class or be placed in a separate "actions" package.这些动作可以是外部类的一部分,也可以放在单独的“动作”包中。

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

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