简体   繁体   English

Java OS X Lion 设置应用程序名称不起作用

[英]Java OS X Lion Set application name doesn't work

I'm trying to change the application name displayed into the menu bar of OS X but i can't succeed with that.我正在尝试更改显示在 OS X 菜单栏中的应用程序名称,但我无法成功。 I have tried settings as the first statement in the main method with the following code :我已经尝试使用以下代码将设置作为主方法中的第一条语句:

System.setProperty("apple.laf.useScreenMenuBar", "true");
System.setProperty("com.apple.mrj.application.apple.menu.about.name", "Alessio");
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

but it doesn't work, in the menu bar is displayed main :但它不起作用,在菜单栏中显示 main :

截屏

I'm on Mac OS X Lion (10.7.2).我在 Mac OS X Lion (10.7.2) 上。

Is there a way to change the application name in the menu bar?有没有办法在菜单栏中更改应用程序名称? If so, how?如果是这样,如何?

Using JDK8, you can set the apple.awt.application.name property to affect the Application menu name.使用 JDK8,您可以设置apple.awt.application.name属性来影响应用程序菜单名称。

However, Martijn Courteaux's warning still applies: you must do this before any AWT classes are loaded.但是,Martijn Courteaux 的警告仍然适用:您必须在加载任何 AWT 类之前执行此操作。 And AWT classes will be loaded before your main() method runs if it lives in a subclass of JFrame .如果 AWT 类位于JFrame的子类中,则它会在main()方法运行之前加载。

Reference:参考:

http://hg.openjdk.java.net/jdk8u/jdk8u/jdk/file/5c1d06cd7d7b/src/macosx/native/sun/osxapp/NSApplicationAWT.m#l157 http://hg.openjdk.java.net/jdk8u/jdk8u/jdk/file/5c1d06cd7d7b/src/macosx/native/sun/osxapp/NSApplicationAWT.m#l157

Apparently, you can do it adding the following when you add the following options to the command line:显然,您可以在将以下选项添加到命令行时添加以下内容:

-Xdock:name="Alessio" -Xdock:name="阿莱西奥"

While com.apple.mrj.application.apple.menu.about.name is the right property name, I think you'll be setting it too late.虽然com.apple.mrj.application.apple.menu.about.name是正确的属性名称,但我认为您设置它为时已晚。 Have you tried it on the command line as:您是否在命令行上尝试过:

-Dcom.apple.mrj.application.apple.menu.about.name=Alessio -Dcom.apple.mrj.application.apple.menu.about.name=Alessio

For more information about writing Java Apps for OS X: http://www.oracle.com/technetwork/articles/javase/javatomac-140486.html有关为 OS X 编写 Java 应用程序的更多信息: http : //www.oracle.com/technetwork/articles/javase/javatomac-140486.html

It may also be worth looking at something like this: http://launch4j.sourceforge.net/也可能值得一看这样的东西: http : //launch4j.sourceforge.net/

I'm not sure if this also works for OS X Lion , but I'm on Mountain Lion .我不确定这是否也适用于 OS X Lion ,但我在Mountain Lion 上

After some testing, my conclusion is that you can use the old approach if and only if you don't do anything with经过一些测试,我的结论是,当且仅当您不做任何事情时,您才能使用旧方法

  • java.awt.Toolkit
  • Setting look and feel设置外观和感觉

before setting the app name.设置应用程序名称之前

Some things that use Toolkit are: (Feel free to edit this answer and add items)使用 Toolkit 的一些事情是:(随意编辑此答案并添加项目)

  • Loading a java.awt.Font (@see static initializer of Font)加载java.awt.Font (@see Font 的静态初始化程序)
  • Acquiring the Screen resolution.获取屏幕分辨率。
  • Loading a class that extends JFrame (such as if the class contains your main method)加载扩展 JFrame 的类(例如,如果该类包含您的main方法)

I've had the same problem and discovered this: if your main method is a member of one of your GUI classes (for example, one derived from JFrame), when the JVM loads your class it will also need to load some other AWT classes.我遇到了同样的问题并发现了这一点:如果您的main方法是您的一个 GUI 类(例如,从 JFrame 派生的一个)的成员,当 JVM 加载您的类时,它还需要加载一些其他 AWT 类. These can interact with java.awt.Toolkit in static initializers, which, as Martijn observed, causes the property to be checked before your main method has had a chance to set it.这些可以与静态初始值设定项中的 java.awt.Toolkit 交互,正如 Martijn 所观察到的,这会导致在您的主方法有机会设置它之前检查该属性。

Try moving the main method into a separate class that doesn't extend any Swing or AWT classes and see if it works.尝试将 main 方法移动到不扩展任何 Swing 或 AWT 类的单独类中,看看它是否有效。

try to put System.setProperty("com.apple.mrj.application.apple.menu.about.name", "Alessio");尝试把System.setProperty("com.apple.mrj.application.apple.menu.about.name", "Alessio"); on your main code before anything else在你的main代码之前

创建只有 main 方法的类允许您更改应用程序名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM