一个突出的链接是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()); } } }); }
菜单栏上的第一个菜单项应显示为"test",遗憾的是情况并非如此.另一方面,文件菜单工作正常.有任何想法吗?
@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类放在不同的文件中.
你需要在主线程中设置"com.apple.mrj.application.apple.menu.about.name"系统属性,而不是在Swing线程中设置(换句话说,只需将它作为程序中的第一行).