简体   繁体   English

在GUI组件上使用可重用操作的最佳实践

[英]Best Practice using reusable Actions on GUI Components

I tried differend things on how to make Actions resuable in my Swing applications. 我尝试了如何在我的Swing应用程序中使Action可恢复的不同之处。 I am not a friend on heavy-weight, 1000+ line classes (with lots of inner/anon classes) and try to split my code up into multiple classes. 我不是重量级,1000多个线类(有很多内部/非类)的朋友,并尝试将我的代码分成多个类。 Thus make them resuseable and exchangeable easily. 从而使它们易于重复使用和更换。

For reusing same Actions in an application I made for every Action its own class to use it in JMenuBar and JToolBar . 为了在我为每个Action创建的应用程序中重用相同的Actions ,它们在JMenuBar和JToolBar中使用它。 Please have a look at the minimal example below. 请看下面的最小例子。

Is this a good choosen practice (esp. using static inner classes)? 这是一个很好的选择练习(尤其是使用静态内部类)?

public class SomeGUI extends JFrame {

    public static void main(String[] args)
    {
        new SomeGUI();
    }

    public SomeGUI()
    {
        setJMenuBar(new MyMenuBar());
        add(new MyToolBar());

        setSize(400, 400);
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

}

class MyMenuBar extends JMenuBar {

    JMenu menu = new JMenu("File");

    public MyMenuBar()
    {
        add(menu);
        menu.add(new JMenuItem(new Actions.NewAction("New", null, "New File", KeyEvent.VK_N)));
    }

}

class MyToolBar extends JToolBar {

    public MyToolBar()
    {
        add(new Actions.NewAction("New", null, "New File", KeyEvent.VK_N));
    }

}

class Actions {

    static class NewAction extends AbstractAction {
        public NewAction(String name, ImageIcon icon, String desc, Integer mnemonic)
        {
            super(name, icon);
            putValue(SHORT_DESCRIPTION, desc);
            putValue(MNEMONIC_KEY, mnemonic);
        }

        @Override
        public void actionPerformed(ActionEvent paramActionEvent)
        {
            System.out.println("do the new action...");
        }
    }

}

Looking forward for your advises. 期待您的建议。 Thanks in advance. 提前致谢。

One suggestion would be not to use an aggregate class which holds all actions. 一个建议是不要使用包含所有操作的聚合类。 Just use a separate file for each class and make it public. 只需为每个类使用单独的文件并将其公开。

This way for using the class in another project you would just need to copy the file assuming it has no specific dependencies in current project. 这种方式在另一个项目中使用该类,您只需复制该文件,假设它在当前项目中没有特定的依赖项。

Is this a good choosen practice (esp. using static inner classes)? 这是一个很好的选择练习(尤其是使用静态内部类)?

This is the way much of the base Swing code is implemented. 这是实现大部分基本Swing代码的方式。

For example take a look at the source code of DefaultEditorKit where all the Actions are defined. 例如,查看DefaultEditorKit的源代码,其中定义了所有Actions。

What I often do (and it's even easier when using GutsAction from guts-gui project) is that I group related actions in one class. 我经常做的事情(当使用guts-gui项目中的GutsAction时更容易)是我将相关的动作分组在一个类中。

I store each action as a final public field (but if you hate public fields you can still make them private and dfine a getter method on each). 我将每个动作存储为final public字段(但如果您讨厌public字段,您仍然可以将它们设为private字段,并在每个字段上定义一个getter方法)。

Each action is defined as a tiny anonymous class where the final field is declared. 每个动作都被定义为一个微小的匿名类,其中声明了最终的字段。

The criteria for grouping are essentially functional but they can also include some context, eg you can have a class that include various actions that perform on the currently selected order (from an order JTable ), thus I can put the context (current selected order) in only one class, and I can also put there the necessary methods to enable/disable all actions when selection changes. 分组的标准本质上是功能性的,但它们也可以包含一些上下文,例如,您可以拥有一个类,其中包含对当前所选顺序执行的各种操作(来自订单JTable ),因此我可以放置上下文(当前选定的顺序)只有一个类,我也可以在那里放置必要的方法来启用/禁用选择更改时的所有操作。

Having grouped actions in one class helps avoid proliferation of action classes all over the place and having to manage instantiation of each individual action. 将操作分组在一个类中有助于避免遍布整个地方的操作类扩散,并且必须管理每个单独操作的实例化。 It also enables easier Dependency Injection of dependencies common to several actions. 它还可以更轻松地依赖注入多个操作共有的依赖项。

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

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