如何使用屏幕菜单栏与助记组合键(如Ctrl + Alt + F),而不是窗口菜单栏上的MacOS?
预期结果(* 屏幕 * 菜单栏中的菜单):
实际结果(* 窗口 * 菜单栏中的菜单):
代码:
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
class GUI extends JFrame {
public static void main(String[] args) {
new GUI();
}
public GUI() {
// Use the screen menu bar instead of the window menu bar.
System.setProperty("apple.laf.useScreenMenuBar", "true");
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("File");
JMenuItem menuItem1 = new JMenuItem("New");
JMenuItem menuItem2 = new JMenuItem("Open");
JMenuItem menuItem3 = new JMenuItem("Close");
// Make the menu listens to <Ctrl> + <Alt> + <F> mnemonic key events.
menu.setMnemonic('F');
menu.add(menuItem1);
menu.add(menuItem2);
menu.add(menuItem3);
menuBar.add(menu);
setJMenuBar(menuBar);
setSize(854, 480);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
}
1条答案
按热度按时间vd8tlhqk1#
看起来这是不可能的,在macOS上没有访问菜单栏的助记符。
请参见超级用户上的类似问题:How to select File menu in Mac, just like Alt + F in Windows?。虽然这个问题不是Swing特有的,但答案强调了这在macOS上是不可能的。Swing会尽力显示助记符调用的菜单。