简体   繁体   English

如何在 JavaFX 中向菜单(不是 MenuItem)添加快捷方式

[英]How to add shortcuts to Menu (not MenuItem) in JavaFX

I want to do menu like this (with shortcuts):我想做这样的菜单(使用快捷方式):

在此处输入图像描述

So, I did:所以我做了:

Menu fileMenu = new Menu("File");
fileMenu.setAccelerator(new KeyCodeCombination(KeyCode.F, KeyCombination.ALT_DOWN));

But it doesn't work.但它不起作用。 When I press ALT+F nothing happens.当我按 ALT+F 时,没有任何反应。 Besides, I don't know how to make key letters underlined.此外,我不知道如何使关键字母加下划线。 Can anyone say how to do it?谁能说怎么做?

What you're looking for is a "mnemonic" , not an accelerator.您正在寻找的是“助记符” ,而不是加速器。 Simply put an underscore ( _ ) in front of the letter you want to use and ensure the mnemonicParsing property is set to true , which it is by default.只需在要使用的字母前加上下划线 ( _ ) 并确保mnemonicParsing属性设置为true ,这是默认设置。

For example, the following:例如,以下内容:

Menu fileMenu = new Menu("_File");

Will let you use ALT + F to display the menu.会让你使用ALT + F来显示菜单。

Note this works with MenuItem as well ( Menu extends MenuItem ).请注意,这也适用于MenuItemMenu扩展MenuItem )。 Adding a mnemonic to a MenuItem provides another way for your user to fire the item's action (in addition to clicking it, using an accelerator, etc.).将助记符添加到MenuItem为您的用户提供了另一种方式来触发项目的操作(除了单击它、使用加速器等)。


Warning: Mnemonics are not supported on all platforms (eg, Macs).警告:并非所有平台(例如 Mac)都支持助记符。 Consequently, you may want to define the accelerator as well.因此,您可能还想定义加速器。

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

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