KeyEvent 不是由 JFrame java 生成的吗?
我有一个 java 程序,其中有一个 JFrame 和 3 个 JButton。我已经向 jframe 添加了一个 keylistener。当我运行该程序时,会打开一个 jframe 窗口,并且默认选择第一个按钮。我的问题是这个 JFrame 没有生成 KeyEvent。 现在,除了向 jframe 添加 KeyListener 之外,我还向按钮添加了 KeyListener。 现在按键事件由按钮生成。
如何让 JFrame 生成 KeyEvent 而不是 JButton 生成它们?
实际上,我的主要目的是为按钮构建键盘快捷键。
I have a java program with a JFrame and 3 JButtons in it. I have added a keylistener to jframe. When i run the program a jframe window is opened and the first button is selected by default. My problem is that a KeyEvent is not being generated by this JFrame.
Now, besides adding a KeyListener to the jframe, i also added a KeyListener to the buttons.
Now the keyevent is being generated by the buttons.
How do I make the JFrame generate KeyEvent instead of the JButton generating them ??
Actually, my main purpose is building keyboard shortcuts for the buttons.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请查看此处如何使用键绑定。
keylistener 的替代方案。
这是一个小示例,它有一个带有焦点的按钮并处理一个 KeyEvent (F2)。
在 F2 键上单击键绑定过程中,执行一个执行
System.out
打印的 ButtonClick。Have a look here How to Use Key Bindings.
An alternative to keylistener.
Here is a little Example it has a Button with focus and process a KeyEvent (F2).
On F2-clicked the Key-Binding process a ButtonClick which performed a
System.out
print.按键事件在当前聚焦的组件上调用(通常不是
JFrame
)The key event is called on the currently focused component (which is usually not the
JFrame
)