在我按下 GUI 按钮之一后,Applet 中未捕获关键事件
我有音乐小程序,它使用键盘、鼠标和 GUI 按钮。当小程序首次加载时,键盘事件可以正常工作,鼠标事件也可以正常工作。但是,在我按下 GUI 按钮之一后,鼠标事件仍然有效,但键盘事件不起作用,并且在我刷新小程序之前不会再次开始工作。
在网上搜寻后,我找到了一些可能的解决方案,我尝试将 button.setFocusable(true);
和 button.addKeyListener(this);
添加到我的所有按钮,还有我的面板。一点效果都没有。我已经看到关于转换为 JApplet 并使用键绑定的建议,但肯定有更简单的方法吗? 抱歉缺少代码,自从我还是个新手以来我就一直在从事这个项目,它有点混乱,而且很长!
非常感谢任何帮助!
I have music Applet which uses keyboard, mouse and GUI buttons. When applet is first loaded keyboard events work fine, as do mouse events. However, after I have pressed one of my GUI buttons the mouse events still work but the keyboard events don't, and don't start working again until I refresh the applet.
After hunting around on the net I found some posible solutions, I've tried adding button.setFocusable(true);
and button.addKeyListener(this);
to all my buttons, and and my panels. No effect at all. I've seen recommendations for converting to a JApplet and using key binding, but surely there must be a simpler way?
Sorry for the lack of code, I've been working on this project since I was a newbie and it's a bit of a mess, and very long!
Any help much appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于 JButton 使用 Swing Action 或 ActionListener,而不是 KeyBindings (用于基于 Swing 的容器和 JComponent),也不使用 KeyListener
编辑
如果没有真正重要的原因,请不要使用史前的
AWT Applet
,使用JApplet
,可能就足够了,简单的JFrame
For JButton use Swing Action or default implementations for ActionListener, rather than KeyBindings (for Swing based Container and JComponents), nor using KeyListener
EDIT
if isn't there really important reasons, don't use prehistoric
AWT Applet
, useJApplet
, may be sufficient would be plainJFrame
尝试从项目中删除问题区域并将其放在这里。很有可能,在定位问题区域时,您已经发现了一些错误。
如果您的项目已经一团糟,那么您应该做的第一件事也是最重要的事情就是对其进行排序。如果这对你来说是一团糟,那说明你不理解它。所以,它根本行不通。这是你的第一个也是主要的错误。
Try to cut the problem area out of your project and put it here. Its highly probable, than while localizing the problem area you'll find some errors already.
If your project is a mess already, then the first and the most important thing you should do, is to order it. If it is a mess for you, that means you don't understand it. So, it simply can't work. That is your first and main error.