Java Swing JMenu 助记符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4434894/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 17:25:50  来源:igfitidea点击:

Java Swing JMenu Mnemonic

javaswing

提问by zfranciscus

I have a menu that I created using JMenu. I want to assign a shortcut key Alt-F to this menu. I used setMnemonic('F') to do that, but the menu does not recognise the mnemonic.

我有一个使用 JMenu 创建的菜单。我想为此菜单分配一个快捷键 Alt-F。我使用 setMnemonic('F') 来做到这一点,但菜单无法识别助记符。

What is the best way to troubleshoot or debug this problem ? I find that setting a break point does not help that much.

解决或调试此问题的最佳方法是什么?我发现设置断点并没有多大帮助。

Thank you.

谢谢你。

Code Snippet:

代码片段:

//higher up in variable declaration
/** Menus on the menu bar */
private JMenu uiFindMnu           = new JMenu("Find");
...
//inside the constructor
// set mnemonic for the Find menu
uiFindMnu.setMnemonic('F');

回答by LaGrandMere

Use setMnemonic(KeyEvent.VK_F);

setMnemonic(KeyEvent.VK_F);

I recommend you to read this about JMenus : Howto use Menus

我建议您阅读有关 JMenus 的内容:Howto use Menus

Here is an extract of this article :

这是这篇文章的摘录:

Menus support two kinds of keyboard alternatives: mnemonics and accelerators. Mnemonics offer a way to use the keyboard to navigate the menu hierarchy, increasing the accessibility of programs. Accelerators, on the other hand, offer keyboard shortcuts to bypass navigating the menu hierarchy. Mnemonics are for all users; accelerators are for power users.

A mnemonic is a key that makes an already visible menu item be chosen. For example, in MenuDemo the first menu has the mnemonic A, and its second menu item has the mnemonic B. This means that, when you run MenuDemo with the Java look and feel, pressing the Alt and A keys makes the first menu appear. While the first menu is visible, pressing the B key (with or without Alt) makes the second menu item be chosen. A menu item generally displays its mnemonic by underlining the first occurrence of the mnemonic character in the menu item's text, as the following snapshot shows. B is the mnemonic character for this menu item

You can specify a mnemonic either when constructing the menu item or with the setMnemonic method. Here are examples of setting mnemonics and accelerators:

菜单支持两种键盘选项:助记符和加速键。助记符提供了一种使用键盘导航菜单层次结构的方法,从而增加了程序的可访问性。另一方面,加速器提供了绕过菜单层次结构导航的键盘快捷键。助记符适用于所有用户;加速器适用于高级用户。

助记符是使已经可见的菜单项被选择的键。例如,在 MenuDemo 中,第一个菜单具有助记符 A,其第二个菜单项具有助记符 B。这意味着,当您使用 Java 外观运行 MenuDemo 时,按 Alt 和 A 键会显示第一个菜单。当第一个菜单可见时,按 B 键(有或没有 Alt)可以选择第二个菜单项。菜单项通常通过在菜单项文本中第一次出现的助记符字符下划线来显示其助记符,如下面的快照所示。B 是这个菜单项的助记符

您可以在构造菜单项时或使用 setMnemonic 方法指定助记符。以下是设置助记符和加速器的示例:

//Setting the mnemonic when constructing a menu item:
menuItem = new JMenuItem("A text-only menu item",
                         KeyEvent.VK_T);

//Setting the mnemonic after creation time:
menuItem.setMnemonic(KeyEvent.VK_T);

As you can see, you set a mnemonic by specifying the KeyEvent constant corresponding to the key the user should press.

如您所见,您可以通过指定与用户应按下的键相对应的 KeyEvent 常量来设置助记符。

回答by robert_x44

Inside of a class constructor (extending JFrame):

在类构造函数内部(扩展 JFrame):

JMenu helpmenu = new JMenu("File");
helpmenu.setMnemonic('F');
JMenuBar menubar = new JMenuBar();
menubar.add(helpmenu);
setJMenuBar(menubar);

That worked fine for me. You'll have to give some more details about your code in order for me to give a better answer. As far as troubleshooting SWING or any application GUI, one of the best recommendations I can give is to create the simplest possible scenario. I keep a bare-bones JFrame template around that I can throw simple code like this inside for testing. Once you know it works in the simplest scenario you can step back to your project and discover what other portion of your GUI is causing a conflict with this functionality.

这对我来说很好。您必须提供有关您的代码的更多详细信息,以便我提供更好的答案。就 SWING 或任何应用程序 GUI 的故障排除而言,我能给出的最佳建议之一是创建最简单的可能场景。我保留了一个基本的 JFrame 模板,我可以将这样的简单代码放入其中进行测试。一旦您知道它在最简单的场景中工作,您就可以返回到您的项目并发现 GUI 的其他部分导致与此功能发生冲突。

Just out of curiosity, you don't happen to have a local variable called 'uiFindMnu' in your Constructor that is hiding your class variable, do you? I'd double check to make sure that the variable that you are calling setMnemonic() on is the one that is added to your MenuBar (and actually displayed).

出于好奇,您的构造函数中不会碰巧有一个名为“uiFindMnu”的局部变量来隐藏您的类变量,对吗?我会仔细检查以确保您调用 setMnemonic() 的变量是添加到您的 MenuBar (并实际显示)的变量。

回答by Ravindra Gullapalli

Suffered with similar problem and realised that due to the setting of Look and feelafter initialising the components caused the issue. Flipped the statements and it worked.

遇到了类似的问题,意识到是由于初始化组件后的外观设置导致了问题。翻转语句,它起作用了。

Posted a blog post here

在这里发表了一篇博文