【发布时间】: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