JPanel 按键侦听器
我正在尝试添加一个包含 JTabbedPane
的关键侦听器。
当收到 ctrl + tab 时,它应该切换选项卡。
但按键事件永远不会发送 我尝试将其添加到面板和选项卡式对象中 - 但没有成功。
这是我的代码
SwitchTabsListener ctrlTabListener = new SwitchTabsListener(genericTabbedPanel);
jMainFrame.addKeyListener(ctrlTabListener);
genericTabbedPanel.addKeyListener(ctrlTabListener);
I am trying to add a key listener that holds a JTabbedPane
.
It should switch the tabs when ctrl + tab is received.
But the keypressed event is never sent
I tried adding it to the panel and to the tabbed object - but with no success.
Here is my code
SwitchTabsListener ctrlTabListener = new SwitchTabsListener(genericTabbedPanel);
jMainFrame.addKeyListener(ctrlTabListener);
genericTabbedPanel.addKeyListener(ctrlTabListener);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在典型的情况下,您的关键事件不会被正确的 Swing 组件拦截。您必须了解光标下方的第一个组件将接收键盘事件。如果您用键盘选择一个按钮,则该 JButton 将接收按键事件。
为了确保获得所有这些事件,您不必在组件上注册,而是使用
KeyboardFocusManager
,无论按键事件发生在哪里,它将接收这些事件。然后,您的代码需要以下元素,
只要按下某个键,无论该键位于 UI 中的哪个位置,
myKeyEventDispatcher
都会收到对dispatchKeyEvent
的调用。这样,您可以确保您的代码被正确调用。注册密钥侦听器的替代方法需要您使用
HierarchyListener
为了添加您的关键侦听器:删除到每个作为根组件的子组件添加/删除的 Swing 组件。这不仅写起来很麻烦,而且很难调试和理解。这就是为什么我更喜欢更暴力,但将应用程序全局按键侦听器添加到特定键盘焦点管理器的相当优雅的方法。
In a typical fashion, your key event is not intercepted by the correct Swing component. You have to understand that the first component below the cursor will receive the keyboard event. Were you to select a button with your keyboard, it would be this JButton that would receive the key event.
To make sure you get all those events, you don't have to register on components, but rather by using a
KeyboardFocusManager
, which will receive key events wherever they occur.Your code then require the following elements
myKeyEventDispatcher
will then receive calls todispatchKeyEvent
whenever a key is pressed, wherever it is in UI. This way, you can make sure your code is correctly called.The alternative method of registering key listener would require you to use a
HierarchyListener
in order for your key listener to be added:removed to each and every swing component that appear to be added/removed as a child of your root component. This is not only cumbersome to write, but also really hard to debug and understand.This is why I prefer the more brute-force, but although quite elegant way of adding application global key listener to a specific keyboard focus manager.
Ctrl+Tab
和Ctrl+Shift+Tab
允许您在 Windows LookAndFeel 中默认循环浏览选项卡:Ctrl+Tab
andCtrl+Shift+Tab
allow you to cycle through tabs by default in the Windows LookAndFeel:这应该有效。这可能对您不起作用,因为
这是我为您编写的代码。
效果很好。尝试使用它并了解你的代码和我的代码之间有什么区别。如果失败,请将您的代码的较大片段发送给我们。
This should work. This probably does not work for you because
Here is the code I wrote for you.
It works fine. Try to play with it and understand what is the difference between yours and my code. If you fail please send us larger snippet of your code.
根据 Riduidel 的回答,这是一个完整的示例。我不确定如何确定该事件是来自按键还是按键释放。
Building from Riduidel's answer, here's a full example. I'm not sure how to determine if the event is from a key press or key release though.