2008-11-20 114 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,它就會自動在原生菜單欄中顯示!