我想对< & >键使用键绑定,然后在我的JFrame上使用这个键。
我正在使用下面的代码来尝试为< key获取它。
KeyStroke testStroke = KeyStroke.getKeyStroke("<");
mainJFrame.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW)
.put(testStroke, "clickButton");
mainJFrame.getRootPane().getActionMap().put("clickButton", new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("PRESS!!!!");
}
});我无法让它开始工作。但是,如果我使用像A这样的键,效果会很好。
KeyStroke testStroke = KeyStroke.getKeyStroke("A"); 因此,我认为是错误的KeyStroke,其余的代码是好的。
如何获得<& >?键的击键
发布于 2014-06-16 14:54:16
根据the documentation for getKeyStroke(char)
返回一个KeyStroke的共享实例,该实例表示指定字符的KEY_TYPED事件。
KeyStroke.getKeyStroke('<');
KeyStroke.getKeyStroke('>');之前你用的是字符串。getKeyStroke(java.lang.String)的查找和文档
解析一个字符串并返回一个KeyStroke。字符串必须具有以下语法:
此方法有一个复杂的语法可遵循。一个字母起作用,但一个特殊的字符不遵循语法。getKeyStroke(char)要简单得多。你应该用它代替。
https://stackoverflow.com/questions/24246418
复制相似问题