使用注册表管理Swing操作 [英] Managing Swing Actions with a Registry

查看:200
本文介绍了使用注册表管理Swing操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常,当我创建Swing(或任何UI)应用程序时,我会在菜单项和按钮上显示各种操作。我通常创建一个动作注册表并将动作存储在那里,然后当发生某些事情时,我会根据应用程序的状态在注册表中禁用/启用动作。
我不会称自己是一个狂热的Swing开发人员,虽然我知道我的方法很好,但这是一个非常典型的管理动作的模式吗?或者有更标准的方法吗?

Typically when I'm creating a Swing (or any UI) application, I have various Actions that appear on menu items and buttons. I usually create an action registry and store the actions in there and then when certain things occur, I disable/enable actions in the registry based on the state of the application. I wouldn't call myself an avid Swing developer, although I know my way around it well enough, but is this a pretty typical pattern for managing Actions? Or is there a more standard way of doing it?

谢谢,

杰夫

推荐答案

杰夫,你的方法似乎是一个很好的方法。我做同样的事情。我调用注册表ActionHandler,它看起来像这样:

Jeff, your approach seems like a good approach. I do the same thing. I call the registry ActionHandler and it looks like this:

import com.google.common.collect.ClassToInstanceMap;
import com.google.common.collect.ImmutableClassToInstanceMap;

import javax.swing.*;
import javax.swing.text.DefaultEditorKit;

public class ActionHandler {

    private static final ClassToInstanceMap<Action> actionMap =
            new ImmutableClassToInstanceMap.Builder<Action>().
                    put(DefaultEditorKit.CutAction.class, new DefaultEditorKit.CutAction()).
                    put(DefaultEditorKit.CopyAction.class, new DefaultEditorKit.CopyAction()).
                    put(DefaultEditorKit.PasteAction.class, new DefaultEditorKit.PasteAction()).
                    put(RefreshAction.class, new RefreshAction()).
                    put(MinimizeAction.class, new MinimizeAction()).
                    put(ZoomAction.class, new ZoomAction()).
                    build();

    public static Action getActionFor(Class<? extends Action> actionClasss) {
        return actionMap.getInstance(actionClasss);
    }
}

现在禁用,比如ZoomAction,我用

Now to disable, say ZoomAction, I use

   ActionHandler.getActionFor(ZoomAction.class).setEnabled(false);

这篇关于使用注册表管理Swing操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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