java - Java 中对 MacOS X 的 native Swing 菜单栏支持

标签 java swing macos

突出的链接是 http://www.devdaily.com/blog/post/jfc-swing/handling-main-mac-menu-in-swing-application/但是 Mac OS X 下的菜单栏显示为包名称而不是应用程序名称。我在没有任何运气的情况下使用上面链接中的代码,所以我不确定最近的 Mac OS 版本是否有任何更改。

摘录如下:

public RootGUI() {
    super("Hello");
    JMenuBar menuBar = new JMenuBar();
    JMenu file = new JMenu("File");
    JMenuItem item = new JMenuItem("Woah");
    file.add(item);
    menuBar.add(file);
    setJMenuBar(menuBar);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(100, 100);
    pack();
    setVisible(true);
}
public static void main(String[] args) {
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            try {
                System.setProperty("apple.laf.useScreenMenuBar", "true");
                System.setProperty("com.apple.mrj.application.apple.menu.about.name", "Test");
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                new RootGUI();
            }
            catch(ClassNotFoundException e) {
                System.out.println("ClassNotFoundException: " + e.getMessage());
            }
            catch(InstantiationException e) {
                System.out.println("InstantiationException: " + e.getMessage());
            }
            catch(IllegalAccessException e) {
                System.out.println("IllegalAccessException: " + e.getMessage());
            }
            catch(UnsupportedLookAndFeelException e) {
                System.out.println("UnsupportedLookAndFeelException: " + e.getMessage());
            }

        }
    });
}

菜单栏上的第一个菜单项应该显示为“测试”,不幸的是,情况并非如此。另一方面,文件菜单工作正常。有什么想法吗?

最佳答案

@凯泽

我想我明白发生了什么。如果您将 main() 方法放在一个不同的类中,那么一切正常。所以你需要这样的东西:

public class RootGUILauncher {
  public static void main(String[] args) {
    try {
                System.setProperty("apple.laf.useScreenMenuBar", "true");
                System.setProperty("com.apple.mrj.application.apple.menu.about.name", "Test");
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        }
        catch(ClassNotFoundException e) {
                System.out.println("ClassNotFoundException: " + e.getMessage());
        }
        catch(InstantiationException e) {
                System.out.println("InstantiationException: " + e.getMessage());
        }
        catch(IllegalAccessException e) {
                System.out.println("IllegalAccessException: " + e.getMessage());
        }
        catch(UnsupportedLookAndFeelException e) {
                System.out.println("UnsupportedLookAndFeelException: " + e.getMessage());
        }
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            new RootGUI();
        }
    });
}

然后将您的 RootGUI 类放在不同的文件中。

关于java - Java 中对 MacOS X 的 native Swing 菜单栏支持,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/307024/

相关文章:

java - appium TouchAction press 和 moveTo 滚动方法在 Android 8.1 上不起作用

Java 复杂路径冲突

python3 不会从 mac shell 脚本运行

java - 从通过 kerberos key 表进行身份验证的 Java 应用程序在远程服务器上执行脚本

java - org.apache.poi.openxml4j.exceptions.InvalidOperationException : Can't open the specified file:

java - 在不使用 Ctrl/Command 键的情况下选择 JList 中的多个项目

java - 使用布局将面板设置在屏幕中央

java - 当 TextField 中的字符串超过 5 个字符时启用按钮

cocoa - 在 Mac OS X 上将 RGBA 数据绘制到 View 中最直接的方法是什么?

macos - 检测显示器是否支持 30 位颜色