Java 中对 MacOS X 的原生 Swing 菜单栏支持
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/307024/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Native Swing Menu Bar Support For MacOS X In Java
提问by Kezzer
A link that stands out is http://www.devdaily.com/blog/post/jfc-swing/handling-main-mac-menu-in-swing-application/however the menu bar under Mac OS X displays as the package name as opposed to the application name. I'm using the code in the above link without any luck, so I'm unsure if anything's changed in recent Mac OS versions.
一个突出的链接是http://www.devdaily.com/blog/post/jfc-swing/handling-main-mac-menu-in-swing-application/但是 Mac OS X 下的菜单栏显示为包名称而不是应用程序名称。我使用上面链接中的代码没有任何运气,所以我不确定最近的 Mac OS 版本是否有任何变化。
Here's an extract:
这是一个摘录:
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 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());
}
}
});
}
The first menu item on the menu bar should display as "test", unfortunately this isn't the case. The file menu works fine, on the other hand. Any ideas?
菜单栏上的第一个菜单项应该显示为“test”,不幸的是事实并非如此。另一方面,文件菜单工作正常。有任何想法吗?
采纳答案by Matt Solnit
@Kezzer
@Kezzer
I think I see what's going on. If you put the main() method in a different class, then everything works. So you need something like:
我想我明白发生了什么。如果将 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();
}
});
}
And then put your RootGUI class in a different file.
然后将您的 RootGUI 类放在不同的文件中。
回答by Matt Solnit
You need to set the "com.apple.mrj.application.apple.menu.about.name" system property in the main thread, not in the Swing thread (in other words, just make it the first line in the program).
您需要在主线程中而不是在 Swing 线程中设置“com.apple.mrj.application.apple.menu.about.name”系统属性(换句话说,只需将其设置为程序的第一行)。
回答by Daniel Hiller
As I understand you want to rename your application menu shown on the os x menu bar. Well, I didn't find a system property but I found a command line option:
据我了解,您想重命名 os x 菜单栏上显示的应用程序菜单。好吧,我没有找到系统属性,但我找到了一个命令行选项:
-Xdock:name="YourNameHere"
that worked for me.
这对我有用。
BTW: The syystem property com.apple.mrj.application.apple.menu.about.name is for renaming the about menu item in your application menu, not the menu bar itself
顺便说一句:系统属性 com.apple.mrj.application.apple.menu.about.name 用于重命名应用程序菜单中的 about 菜单项,而不是菜单栏本身
See this linkhere (the old linkwas probably killed sometime after the sun-oracle-aquisition).
回答by Vincent Robert
If you want to deliver an application that looks native on Mac OS X, one important part is to deliver an appplication bundle. Within the application bundle, you will be able to provide a property list file in order to solve this problems.
如果您想交付一个在 Mac OS X 上看起来是原生的应用程序,一个重要的部分是交付一个应用程序包。在应用程序包中,您将能够提供一个属性列表文件来解决这个问题。
Some official info: Java Development Guide for Mac OS X
回答by Jay Askren
回答by ramsey0
If you are launching multiple JFrames and you use the mac menu, then one of the JFrames could be replacing the JFrame you want with one that has a different menu structure. I'm not sure if you can share one JMenuBar for multiple JFrames, but you could just make an application wide menubar that all the frames instantiate.
如果您要启动多个 JFrame 并使用 mac 菜单,那么其中一个 JFrame 可能会用具有不同菜单结构的 JFrame 替换您想要的 JFrame。我不确定您是否可以为多个 JFrame 共享一个 JMenuBar,但您可以制作一个应用程序范围的菜单栏,所有框架都实例化。
回答by Pietro Pozzoli
for anybody interested, although this question is 6 years old i had the same problem. Swing menus aren't displayed in the mac native bar. I found an easier and more straightforward solution... Just add to your JFrame a Java.awt Menu Component instead of a JMenu and it will automatically display in the native Menubar!
对于任何感兴趣的人,虽然这个问题是 6 岁我有同样的问题。Swing 菜单不会显示在 Mac 原生栏中。我找到了一个更简单、更直接的解决方案...只需将 Java.awt 菜单组件而不是 JMenu 添加到您的 JFrame 中,它就会自动显示在本机菜单栏中!