java 如何在MacOS上使用带有助记键组合的屏幕菜单栏?

tquggr8v  于 2023-01-07  发布在  Java
关注(0)|答案(1)|浏览(69)

如何使用屏幕菜单栏与助记组合键(如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);
  }
}
vd8tlhqk

vd8tlhqk1#

看起来这是不可能的,在macOS上没有访问菜单栏的助记符。
请参见超级用户上的类似问题:How to select File menu in Mac, just like Alt + F in Windows?。虽然这个问题不是Swing特有的,但答案强调了这在macOS上是不可能的。Swing会尽力显示助记符调用的菜单。

相关问题