我正在开发一个 GUI。
Gui 的构造如下: 包含(自定义)leftPanel 和 rightPanel 的 JFrame。RightPanel 是选项卡窗格的扩展,并包含作为选项卡之一的 MainTab 类。
Maintab 有以下代码:
private void createKeyBindings(){
actionMap = this.getActionMap();
inputMap = this.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
inputMap.put(KeyStroke.getKeyStroke("UP),
"pressedUpArrow");
inputMap.put(KeyStroke.getKeyStroke("released UP"),
"releasedUpArrow");
actionMap.put("pressedUpArrow",
new PressedUpArrowAction());
actionMap.put("releasedUpArrow",
new ReleasedUpArrowAction());
inputMap.put(KeyStroke.getKeyStroke("RIGHT"),
"pressedRightArrow");
inputMap.put(KeyStroke.getKeyStroke("released RIGHT"),
"releasedRightArrow");
actionMap.put("pressedRightArrow",
new PressedRightArrowAction());
actionMap.put("releasedRightArrow",
new ReleasedRightArrowAction());
inputMap.put(KeyStroke.getKeyStroke("LEFT"),
"pressedLeftArrow");
inputMap.put(KeyStroke.getKeyStroke("released LEFT"),
"releasedLeftArrow");
actionMap.put("pressedLeftArrow",
new PressedLeftArrowAction());
actionMap.put("releasedLeftArrow",
new ReleasedLeftArrowAction());
inputMap.put(KeyStroke.getKeyStroke("RIGHT"),
"pressedRightArrow");
inputMap.put(KeyStroke.getKeyStroke("released RIGHT"),
"releasedRightArrow");
actionMap.put("pressedRightArrow",
new PressedRightArrowAction());
actionMap.put("releasedRightArrow",
new ReleasedRightArrowAction());
inputMap.put(KeyStroke.getKeyStroke("DOWN"),
"pressedDownArrow");
inputMap.put(KeyStroke.getKeyStroke("released DOWN"),
"releasedDownArrow");
actionMap.put("pressedDownArrow",
new PressedDownArrowAction());
actionMap.put("releasedDownArrow",
new ReleasedDownArrowAction());
}
因此,当我按下其中一个箭头键并释放它时,它必须采取行动。
但它只有在我释放其中一个箭头键时才会起作用。原因可能是 LeftPanel 或 RightPanel 的其他组件之一或面板本身已经将该键绑定用于其他用途。
我试图只清除类本身( this.getInputMap().clear() )中的输入图和动作图,但问题仍然存在。有什么解决办法吗?