macos 在 Mac 上设置 Java Swing 应用程序名称
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3154638/
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
Setting Java Swing application name on Mac
提问by Michael
I'm writing a Java Swing application for the Mac using Java 1.6. I've read a number of tutorials that step you through how to better integrate your Java application with OS X, but there's one thing I haven't been able to get working. I can't get the application name (the first, bolded menu item in the Mac menu bar) to display. By default, the fully-qualified class name of the main class is shown and I can't get it to change.
我正在使用 Java 1.6 为 Mac 编写 Java Swing 应用程序。我已经阅读了许多教程,这些教程向您介绍了如何更好地将 Java 应用程序与 OS X 集成,但是有一件事情我无法解决。我无法显示应用程序名称(Mac 菜单栏中的第一个粗体菜单项)。默认情况下,显示主类的完全限定类名,我无法更改它。
This sitesays that you have to set the following property:
该站点说您必须设置以下属性:
System.setProperty("com.apple.mrj.application.apple.menu.about.name", "AppName");
But that doesn't work (I'm running 10.6, so maybe the property name changed?).
但这不起作用(我正在运行 10.6,所以也许属性名称已更改?)。
When I create a new Java project in XCode (I normally use Eclipse), the name somehow magically gets set! (it starts you out with a runnable, boiler-plate application) I've looked all around the XCode project for how this is done, but I can't figure it out!
当我在 XCode 中创建一个新的 Java 项目时(我通常使用 Eclipse),这个名字不知何故神奇地被设置了!(它从一个可运行的样板应用程序开始)我已经查看了 XCode 项目的所有内容,以了解这是如何完成的,但我无法弄清楚!
My guess is that it only sets the application name if you wrap your Java application up in a Mac *.app package, but was wondering if anyone knew the answer. Thanks.
我的猜测是,如果您将 Java 应用程序包装在 Mac *.app 包中,它只会设置应用程序名称,但想知道是否有人知道答案。谢谢。
EDIT: Interestingly, it sets the application name if I package my application in a runnable JAR file, but not if I run it from Eclipse.
编辑:有趣的是,如果我将应用程序打包在可运行的 JAR 文件中,它会设置应用程序名称,但如果我从 Eclipse 运行它,则不会。
回答by uthark
You should do the following during app initialization, before GUI is built:
在构建 GUI 之前,您应该在应用程序初始化期间执行以下操作:
// take the menu bar off the jframe
System.setProperty("apple.laf.useScreenMenuBar", "true");
// set the name of the application menu item
System.setProperty("com.apple.mrj.application.apple.menu.about.name", "AppName");
// set the look and feel
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
UPDATE. Above code works in Java 1.5, this code may not work in 1.6
更新。以上代码适用于 Java 1.5,此代码可能不适用于 1.6
For new java see documentation:
对于新的 Java,请参阅文档:
- Either use
-Xdock:name
command-line property: -Xdock:name=YourAppName
- Or set
CFBundleName
in information property list file (plist)
- 要么使用
-Xdock:name
命令行属性: -Xdock:name=YourAppName
- 或
CFBundleName
在信息属性列表文件(plist)中设置
回答by PeterVermont
On Mac 10.7.5, programatically setting the property worked with Java 1.6 but not with Java 1.7
在 Mac 10.7.5 上,以编程方式设置属性适用于 Java 1.6 但不适用于 Java 1.7