简体   繁体   English

如何在Java应用程序中为菜单栏设置键盘快捷键?

[英]How do I set keyboard shortcuts for my menubar in a Java application?

I'm developing a simple drawing application in Java. 我正在用Java开发一个简单的绘图应用程序。 I'm restricted to using an AWT MenuBar (Not JMenuBar), and I need to set a few keyboard shortcuts and I can't find how to do so. 我仅限于使用AWT MenuBar(而不是JMenuBar),并且我需要设置一些键盘快捷键,但我找不到方法。 I need shortcuts for two MenuItems and for three CheckBoxMenuItems. 我需要两个MenuItems和三个CheckBoxMenuItems的快捷方式。

It would go something like this: 它会像这样:

newItem = Ctrl+N
quitItem = Ctrl+Q
rectangleItem = Ctrl+R
ovalItem = Ctrl+O
lineItem = Ctrl+L

Since the suggestion in the comments seems to have fixed at least part of your problem, I'm gonna put it here as an answer for users in the future. 由于评论中的建议似乎已经解决了您的问题中的至少一部分,因此我将其作为将来用户的答案。

Read the second half of this documentation and change your MenuItem constructors to take in a MenuShortcut as their second argument, like this (taken from the linked docs): 阅读本文档的后半部分,并更改MenuItem构造函数以将MenuShortcut作为其第二个参数,如下所示(从链接的文档中获取):

menu.add(new MenuItem("Save...", new MenuShortcut('s'));

it would be easyer if: 如果满足以下条件,它将更容易:

put a layered pane? 把分层的窗格? a glass in front, which will accept keyboard event in the whole area. 前面的玻璃杯将接受整个区域的键盘事件。

  • not sure what components is for awt, but for sure you have a very good reason to use it, not just look and feel? 不确定awt的组件是什么,但是确定您有充分的理由使用它,而不仅仅是外观和感觉?

There you set the shortcuts. 在此设置快捷方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM