本文介绍了焦点Java小程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我所做的都是基于一个的JPanel Java游戏。当我把它添加到一个JFrame它完美的作品,当我把它添加到JApplet的并测试它使用Eclipse的内置小程序测试它完美。然而,当我试图通过一个HTML网站运行小程序不起作用。它加载,但它没有考虑键盘输入。我有它设置,其中pressing的开始游戏,但即使当我点击游戏和preSS S,什么都不会发生。
我必须设置键盘焦点,因为我认为这是自动完成的。
解决方案
KeyEvents are only passed to the component that has focus. I would guess your panel doesn't have focus, so make sure you make the panel focusable and then use the requestFocusInWindow() method once the GUI is visible to make sure the panel has focus.
However, the better solution to the problem is not to rely on the KeyListener but instead to use Key Bindings. Swing was designed to use Key Bindings.
Read more about Key Bindings.
这篇关于焦点Java小程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!