简体   繁体   中英

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. 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.

For example, the following:

Menu fileMenu = new Menu("_File");

Will let you use ALT + F to display the menu.

Note this works with MenuItem as well ( Menu extends 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.).


Warning: Mnemonics are not supported on all platforms (eg, Macs). Consequently, you may want to define the accelerator as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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