当存在带有 JButton 的子组件时,JPanel 不监听按键事件

发布于 2024-08-31 23:04:04 字数 886 浏览 3 评论 0原文

我正在为我的大学项目开发​​地图编辑器。我遇到了一个问题,地图面板没有监听关键事件,而它应该监听。 当我将 ToolBarPane(扩展 JPanel)与 JComponent(例如 JButton、JComboBox)添加到其上实现 ActionListener 的 ToolBarPane(扩展 JPanel)以及将地图面板(扩展 JPanel)一起添加到框架(我使用 BorderLayout)时,就会发生这种情况。我有 System.out.println 语句来测试是否收到按键,并且它不打印,如果我删除工具栏,按键监听器会再次工作,所以 mouseListenner 会像 keyListener 一样被禁用,这意味着我可以' t 处理按下事件等,但 mouseListener 工作正常,我仍然可以处理鼠标移动事件。

以下是在没有 ToolBarPane 的情况下如何工作的屏幕截图

http://img684.imageshack.us /img684/3232/sampleku.png

注意,您可以使用鼠标在地图上放置图像,也可以像激光工具一样使用鼠标选择图像,并且通过按数字键可以在不同的图像之间切换图像,这工作正常,直到我添加显示如下的 ToolBarPane:

img291.imageshack.us/img291/8020/failve.png (请在此之前添加http,我只能发布一个超链接)

(我不能在这里发布图像,因为我是新用户)

随着ToolBarPane的打开,我不再能够处理关键事件。

我想焦点可能会以某种方式转移到该面板,但根本不确定。

身体知道这一点并且可以帮助我吗?

非常感谢

I'm working on a map editor for my college project. And I had a problem that the map panel is not listening key event while it should.
This happens when I add a ToolBarPane (which extends JPanel) with JComponent such as JButton, JComboBox that implements ActionListener on it AND the map panel (which extends the JPanel) together on to the Frame (I used BorderLayout). I have System.out.println statement to test if the key press is received, and it's not printing, if I remove the ToolBar, the key listener works again, so is the mouseListenner is disabled just like the keyListener, which means I can't handle press events etc, but the mouseListener works fine and I can still handle mouse move event.

Here is a screen shot how it works without the ToolBarPane

http://img684.imageshack.us/img684/3232/sampleku.png

note that you can use the mouse to put images on the map, you can also select images using the mouse just like a laser tool, and by pressing number key you can switch between different images, this works fine until I add the ToolBarPane which shows like this:

img291.imageshack.us/img291/8020/failve.png
(please add http before that, I can only post one hyperlink)

(I can't post images here because I'm a new user)

With the ToolBarPane on I was no longer able to handle the key event.

I guess it might by that the focus as been transferred to that panel somehow, but not sure at all.

Does and body know this and can help me out?

Thanks very much

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半步萧音过轻尘 2024-09-07 23:04:04

我建议您使用 InputMapWHEN_ANCESTOR_OF_FOCUSED_COMPONENT 或类似的东西。摘自如何使用键绑定

JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT
该组件包含(或者是)具有焦点的组件。此输入映射通常用于复合组件

,它对我来说非常有效。请参阅我的另一篇文章以获取更多信息和实际代码示例:

Java 游戏的键盘输入

或本教程:

Swing:理解输入/动作地图

I suggest you use the InputMap and WHEN_ANCESTOR_OF_FOCUSED_COMPONENT or something similar. Excerpt from How to Use Key Bindings:

JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT
The component contains (or is) the component that has the focus. This input map is commonly used for a composite component

That has worked very robustly for me. Have a look at my other post for more information and actual code examples:

Keyboard input for a game in Java

or this tutorial:

Swing: Understanding Input/Action Maps

荒人说梦 2024-09-07 23:04:04

您不应该使用 KeyListener。

Swing 被设计为使用更加灵活的键绑定。查看我的按键绑定的快速摘要,其中还包含一个链接参见 Swing 教程,其中包含更多详细信息。

(我不能在这里发布图片,因为我是新用户)

无论如何,图片并没有多大帮助。如果您需要更多帮助,请发布显示问题的 SSCCE(在尝试上述建议之后)。

You should NOT be using a KeyListener.

Swing was designed to use Key Bindings which is far more flexible. Check out my quick summary of Key Bindings which also includes a link to the Swing tutorial which conains far more detail.

(I can't post images here because I'm a new user)

An image doesn't help much anyway. If you need more help post your SSCCE which shows the problem (after trying the above suggestion).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文