java 如何添加 JMenuBar 快捷方式?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3717936/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add JMenuBar shortcuts?
提问by Hymannad
Adding shortcuts to JMenuBar submenu items in the Java Swing GUI designer is obvious, but how are shortcuts added to JMenuBar main menu items?
在 Java Swing GUI 设计器中向 JMenuBar 子菜单项添加快捷方式是显而易见的,但是如何向 JMenuBar 主菜单项添加快捷方式呢?
回答by Vivien Barousse
You have two types of keyboard shortcuts: mnemonics and accelerators.
您有两种类型的键盘快捷键:助记符和加速键。
Mnemonics are usually triggered using Alt+KEY. That's the letter that's underlined in the menu item text (F for File, for example). Accelerators are application-wide shortcuts that are usually triggered using Ctrl+KEY.
助记符通常使用 Alt+KEY 触发。这是菜单项文本中带下划线的字母(例如,F 表示文件)。加速器是应用程序范围的快捷方式,通常使用 Ctrl+KEY 触发。
To use mnemonics, you can use the setMnemonic()
method:
要使用助记符,您可以使用以下setMnemonic()
方法:
menuItem.setMnemonic('F');
To use accelerators, you have to use the setAccelerator()
method.
要使用加速器,您必须使用该setAccelerator()
方法。
menuItem.setAccelerator(KeyStroke.getKeyStroke(
java.awt.event.KeyEvent.VK_S,
java.awt.Event.CTRL_MASK));
回答by Sean
The Sun/Oracle site has a great Tutorial on using JMenu'sWhen you are dealing with shortcut keys, Java uses mnemonic or Accelerator depending on the shortcut you want to use. you can set the mnemonic using the following
Sun/Oracle 站点有一个很好的使用JMenu 的教程。 当您处理快捷键时,Java 使用助记符或加速器取决于您要使用的快捷键。您可以使用以下方法设置助记符
menuItem.setMnemonic(KeyEvent.VK_T);
and the accelerator via
和加速器通过
menuItem.setAccelerator(KeyStroke.getKeyStroke(
KeyEvent.VK_T, ActionEvent.ALT_MASK));
These are both examples taken from the link above
这些都是从上面的链接中获取的示例