【问题标题】:Dealing with LOTS of Key mappings处理大量的键映射
【发布时间】:2013-08-14 10:20:24
【问题描述】:

在 Swing 中为组件的大量键映射定义操作的最佳方式是什么?

我在 Swing 中构建了自己的文本视图,并希望为很多键定义操作。我目前正在这样做的方式(到目前为止大约 10 个键)是:

ActionMap actionMap = DBDocument.this.getActionMap();
int condition = JComponent.WHEN_FOCUSED;
InputMap inputMap = DBDocument.this.getInputMap(condition);
String tab = "tab";
actionMap.put(tab, new AbstractAction() {
            private static final long   serialVersionUID    = 1L;
            @Override
             public void actionPerformed(ActionEvent ap) {
                if(mDocumentModel != null){
                    //Do some stuff here
                }
             }
          });

这显然是定义键绑定的一种非常冗长的方式。理想情况下,我将能够定义一个处理许多可能性的操作(例如 [A-Z] 或 [0-9])。我已经在 OpenJDK 中搜索了定义,但并没有走得太远。

【问题讨论】:

  • 把所有"LOTS of Key mappings" to array,使用一个数组作为普通键[A-Z]/[0-9],第二个作为修饰符+键(例如ALT + [A-Z]/[0-9]),等
  • 并为您的 XxxAction 使用 is/setEnabled

标签: java swing keyboard-shortcuts key-bindings actionevent


【解决方案1】:

感谢您通过应用DRY 原则来避免重复。引用了几个 DRY 操作的示例 here,但公共线程是共享一个公共模型的操作,例如 Document,或者由一组相关按钮共享。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-05-30
    • 2023-03-25
    • 2019-12-12
    • 1970-01-01
    • 2019-06-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多