How do you make menu item (JMenuItem) shortcut?

后端 未结 2 589
借酒劲吻你
借酒劲吻你 2020-12-31 02:11

So i noticed that in awt there is a MenuItem constructor for adding a CTRL + (some key) shortcut, but there is no such constructor for JMenuIte

2条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-31 02:51

    Example for CTRL + N.

    menuItem.setAccelerator(KeyStroke.getKeyStroke('N', Toolkit.getDefaultToolkit ().getMenuShortcutKeyMask()));
    

    Toolkit.getDefaultToolkit ().getMenuShortcutKeyMask() returns control key (ctrl) on Windows and linux, and command key (⌘) on Mac OS.

提交回复
热议问题