macos 在 OS X 上的 Java swing 中设置默认应用程序图标图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11253772/
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 the default application icon image in Java swing on OS X
提问by rhombidodecahedron
I'm trying to set the icon image for a Jar file:
我正在尝试为 Jar 文件设置图标图像:
setIconImage(new ImageIcon(getClass().getResource("logo.png")).getImage());
When running in Mac OS X 10.7.4 I get the following error:
在 Mac OS X 10.7.4 中运行时,出现以下错误:
Jun 28 15:21:40 (my dhcp) java[73383] <Error>: CGContextGetCTM: invalid context 0x0
Jun 28 15:21:40 (my dhcp) java[73383] <Error>: CGContextSetBaseCTM: invalid context 0x0
Jun 28 15:21:40 (my dhcp) java[73383] <Error>: CGContextGetCTM: invalid context 0x0
Jun 28 15:21:40 (my dhcp) java[73383] <Error>: CGContextSetBaseCTM: invalid context 0x0
Jun 28 15:21:40 (my dhcp) java[73383] <Error>: CGContextGetCTM: invalid context 0x0
Jun 28 15:21:40 (my dhcp) java[73383] <Error>: CGContextSetBaseCTM: invalid context 0x0
回答by Keith Randall
setIconImage
does not set the jar icon. It will set the icon for what the minimized window for that JFrame
will look like. The jar icon (which controls the finder icon and the dock application icon) cannot be set in the jar file itself. You just get the default icon provided by the OS. You will need to wrap it using something like JarBundlerfor OS X or Launch4J for Windows.
setIconImage
不设置 jar 图标。它将为最小化窗口的JFrame
外观设置图标。jar 图标(控制finder 图标和dock 应用程序图标)不能在jar 文件本身中设置。您只需获得操作系统提供的默认图标。您将需要使用诸如适用于 OS X 的 JarBundler 或适用于 Windows 的 Launch4J 之类的东西来包装它。
You can set the application dock icon while your application is running, see com.apple.eawt.Application.setDockIconImage
. It isn't perfect though, because when you double-click on your jar, it starts up in the dock using the generic java icon and only switches to your custom icon after a bounce or two when the java code starts running. Also, I don't think it would set the dock icon for an jar which isn't running (not that you can drag a jar file into the dock anyway - doesn't seem to work for me).
您可以在应用程序运行时设置应用程序停靠栏图标,请参阅com.apple.eawt.Application.setDockIconImage
。但是它并不完美,因为当你双击你的 jar 时,它会在 Dock 中使用通用 java 图标启动,并且只有在 java 代码开始运行时弹跳一两次后才会切换到你的自定义图标。另外,我认为它不会为未运行的 jar 设置停靠栏图标(不是说无论如何您都可以将 jar 文件拖入停靠栏 - 似乎对我不起作用)。
Here's some code that demonstrates the different images you can set:
下面是一些演示您可以设置的不同图像的代码:
import com.apple.eawt.Application;
import javax.swing.*;
class SetIcon extends JFrame {
SetIcon() {
setIconImage(new ImageIcon("doc.png").getImage());
Application.getApplication().setDockIconImage(
new ImageIcon("app.png").getImage());
}
public static void main(String args[]) {
SetIcon s = new SetIcon();
s.setVisible(true);
}
}
回答by mvreijn
Just to add my final solution for adding a MacOSX Dock icon in a pure-java application:
只是添加我在纯 Java 应用程序中添加 MacOSX Dock 图标的最终解决方案:
public boolean exists(String className)
{
try {
Class.forName( className, false, null );
return true;
}
catch (ClassNotFoundException exception) {
return false;
}
}
public void setIcon( BufferedImage icn )
{
if ( exists( "com.apple.eawt.Application" ) )
{
com.apple.eawt.Application.getApplication().setDockIconImage( icn );
}
}
This silently ensures the class is available and the executes the setDockIconImage()
method. Works very well for me and it does not interfere with other platforms.
这以静默方式确保该类可用并执行该setDockIconImage()
方法。对我来说效果很好,它不会干扰其他平台。
回答by trashgod
You can place your .icns
file in the application bundle's Contents/Resources
directory and reference it in your Info.plist
file. For example, a file named ApplicationName.icns
would be referenced by a <dict>
entry of this form:
您可以将您的.icns
文件放在应用程序包的Contents/Resources
目录中并在您的Info.plist
文件中引用它。例如,以下形式ApplicationName.icns
的<dict>
条目将引用名为的文件:
<key>CFBundleIconFile</key>
<string>ApplicationName.icns</string>
Some related details are mentioned here.
回答by Alex Teixeira
java -Xdock:icon=/path/myIcon.png myApp
java -Xdock:icon=/path/myIcon.png myApp