当子组件上带有JButton时,JPanel不监听按键事件 [英] JPanel not listening to key event when there is a child component with JButton on it

查看:131
本文介绍了当子组件上带有JButton时,JPanel不监听按键事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

这是没有ToolBarPane时的工作原理的屏幕截图

Here is a screen shot how it works without the ToolBarPane

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

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

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 (请在此之前添加http,我只能发布一个超链接)

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)

启用ToolBarPane后,我不再能够处理按键事件.

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?

非常感谢

推荐答案

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

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
该组件包含(或是)具有焦点的组件.此输入图通常用于复合组件

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:

使用Java游戏的键盘输入

或本教程:

摇摆:理解输入/动作图

这篇关于当子组件上带有JButton时,JPanel不监听按键事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆