简体   繁体   English

JavaFX 菜单项在右侧显示快捷方式

[英]JavaFX menu item show shortcuts on right hand side

Take for example the menu items from the edit menu in JavaFX Scene Builder以 JavaFX Scene Builder 中编辑菜单中的菜单项为例

See how they display the shortcuts on the right?看看他们如何在右侧显示快捷方式? Is there any easy way to achieve the same effect using JavaFX?有没有什么简单的方法可以使用 JavaFX 实现相同的效果? Thanks.谢谢。

You can add an accelerator key in scene builder or add it directly in the fxml file like so您可以在场景构建器中添加一个加速键,或者像这样直接在 fxml 文件中添加它

      <MenuItem mnemonicParsing="true" onAction="#mnuSaveAction" text="%menu.title.save" fx:id="mnuSave">
        <accelerator>
          <KeyCodeCombination alt="UP" code="S" control="DOWN" meta="UP" shift="UP" shortcut="UP" />
        </accelerator>
      </MenuItem>

If by "in javafx" you mean without using fxml you can use mnuSave.setAccelerator(KeyCombination);如果“在 javafx”中是指不使用 fxml,则可以使用mnuSave.setAccelerator(KeyCombination);

If your are not using fxml file then you can add key combination like below.如果您不使用 fxml 文件,那么您可以添加如下组合键。

MenuItem gotoLine = new MenuItem("Goto");
KeyCombination gotoKeyCombination = new KeyCodeCombination(KeyCode.G, KeyCombination.CONTROL_DOWN);
gotoLine.setAccelerator(gotoKeyCombination);
gotoLine.setOnAction(event -> {
            System.out.println("CTRL+G event triggered");
            System.out.println(event.toString());
 });

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

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