2008-11-20 35 views
16

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

下面是摘录:

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()); 
      } 

     } 
    }); 
} 

菜单栏应该显示为“测试”的第一个菜单项,可惜这种情况并非如此。另一方面,文件菜单正常工作。有任何想法吗?

+2

户外看看所有这些例外 – brendan 2012-07-10 13:24:05

+0

@brendan这是4年前,拜托啊;) – Kezzer 2012-07-10 14:56:45

回答

21

@Kezzer

我想我明白这是怎么回事。如果将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类放在一个不同的文件中。

9

您需要在主线程中设置“com.apple.mrj.application.apple.menu.about.name”系统属性,而不是在Swing线程中(换句话说,只是将其设置为第一行该程序)。

3

据我所知,你想重命名你的应用程序菜单显示在os x菜单栏上。那么,我没有找到一个系统属性,但我发现了一个命令行选项:

-Xdock:name="YourNameHere" 

这为我工作。

BTW:该syystem属性com.apple.mrj.application.apple.menu.about.name是在应用程序菜单中重命名有关的菜单项,没有菜单栏本身

this link这里(的old link可能在太阳 - 甲骨文收购后的某个时间被杀死)。

+1

_this link_死 – 2015-05-19 13:02:57

+1

@Ben:谢谢,更新的链接 – dhiller 2015-05-26 08:59:30

0

@Matt:不幸的是,没有为我工作。

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(); 
     } 
    }); 
} 

这就是我曾尝试除了您的评论。

0

如果你想提供一个在Mac OS X上看起来很原生的应用程序,一个重要的部分就是提供一个应用程序包。在应用程序包中,您将能够提供属性列表文件以解决此问题。

一些官方信息:Java Development Guide for Mac OS X

1

当您构建应用程序时,您还可以使用Macify,因此您不需要更改任何代码。

0

如果您要启动多个JFrame并使用mac菜单,那么其中一个JFrame可能会将具有不同菜单结构的JFrame替换为您想要的JFrame。我不确定是否可以为多个JFrame共享一个JMenuBar,但是您可以创建一个应用程序范围的菜单栏,以便实现所有框架。

0

对于任何感兴趣的人,虽然这个问题是6岁,我有同样的问题。 Swing菜单不会显示在Mac本地栏中。 我发现了一个更简单,更直接的解决方案... 只需向您的JFrame添加一个Java.awt菜单组件而不是JMenu,它就会自动在原生菜单栏中显示!