JMenuBar at the top in MacOSX

2019-06-27 09:27发布

In the Java Desktop Application template used by Netbeans, a menu bar is created with JMenuBar and JMenuItems.

How can I get that bar displayed at the top, where menu bars are displayed in MacOSX instead of in-window, like in Windows?

3条回答
Rolldiameter
2楼-- · 2019-06-27 09:35

Note: This is outdated information - a more recent answer is needed.

Java applications look like traditional java applications even under OS X.

If you want a native look and feel, there are a few tweaks you have to do. This article series describes them.

http://www.oracle.com/technetwork/articles/javase/javatomac-140486.html http://www.oracle.com/technetwork/java/javatomac2-138389.html

This includes setting the Dock icon and text, and integrating with the Applications menu.

I believe that the OS X "wrap jar as an application" utility with XCode sets all these properties automatically.

查看更多
The star\"
3楼-- · 2019-06-27 09:47

By adding something like this into your code:

if (System.getProperty("os.name").contains("Mac")) {
  System.setProperty("apple.laf.useScreenMenuBar", "true");
}
查看更多
你好瞎i
4楼-- · 2019-06-27 10:00

I had the same issue, but I realized that the MenuBar needs to be added to the frame as:

frame.setJMenuBar(menuBar);

instead of: frame.add(jMenuBar); along with: System.setProperty("apple.laf.useScreenMenuBar", "true"); in the main method.

查看更多
登录 后发表回答