添加 MouseListener 会破坏 Keylistener
当我删除所有 MouseListener 组件并运行小程序时,我必须单击它以使其获得焦点,然后 KeyListener 才能正常工作。为了解决这个问题,我添加了一个“打开页面”,您必须在游戏运行之前单击“开始”。这将使小程序获得焦点,并且在播放器同时准备好之前不会启动。我添加了 MouseListener,效果很好,但现在 KeyListener 根本不注册。我不知道为什么,但我想这又与焦点有关。有人知道这里应该做什么吗?
代码可以在这里找到:http://pastebin.com/LDxtk878
谢谢!
When I remove all of the MouseListener components and run the applet I have to click on it to give it focus, then the KeyListener works perfectly. To solve this I added an "opening page" where you have to click on start before the game runs. This would give the applet focus and not start until the player was ready at the same time. I add in the MouseListener and it works great, but now the KeyListener does not register at all. I can not find out why, but I imagine it has something to do with focus again. Anyone know what should be done here?
Code can be found here: http://pastebin.com/LDxtk878
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果不检查所有 322 行代码,您可以查看文章 如何编写按键侦听器,请注意“注意:要触发键盘事件,组件必须具有键盘焦点”之后的部分。
另外,请考虑扩展
JApplet
作为替代方案。Without examining all 322 lines of your code, you might look at the article How to Write a Key Listener with attention to the section following "Note: To fire keyboard events, a component must have the keyboard focus."
Also, consider extending
JApplet
, as an alternative.