我正在使用按键监听器和 jtabbed Pane ,每当我尝试使用键盘上的左右键时,它只会在选项卡之间切换。这真的很烦人,因为我实际上是在其中一个选项卡中使用箭头键。有什么方法可以关闭java中的“切换选项卡”按键功能吗?
提前谢谢
KeyListen keylistener = new KeyListen();
MainGUI.MainTabbedPane.addKeyListener(keylistener);
MainGUI.MainTabbedPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("LEFT"), "none");
MainGUI.MainTabbedPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("RIGHT"), "none");
最佳答案
是的,你必须取消注册键绑定(bind),你可以做这样的事情
tabComponent.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke("LEFT"), "none");
tabComponent.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke("RIGHT"), "none");
您可能想看看这个How to use KeyBindings 。不建议使用 KeyListeners 而是使用 KeyBindings,因为首先你必须有焦点,而且 keylisteners 适用于所有键,而 keybinding 适用于特定键。
关于java - JTabbedPane 正在读取击键并切换选项卡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17385314/