在OS X上以Java swing设置默认应用程序图标图像 [英] Setting the default application icon image in Java swing on OS X

查看:142
本文介绍了在OS X上以Java swing设置默认应用程序图标图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为Jar文件设置图标图像:

I'm trying to set the icon image for a Jar file:

setIconImage(new ImageIcon(getClass().getResource("logo.png")).getImage());

在Mac OS X 10.7.4中运行时出现以下错误:

When running in Mac OS X 10.7.4 I get the following error:

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


推荐答案

setIconImage 未设置jar图标。它将为 JFrame 的最小化窗口设置图标。 jar图标(控制finder图标和dock应用程序图标)无法在jar文件中设置。您只需获取操作系统提供的默认图标。您需要使用 JarBundler 之类的内容进行包装OS X或Launch4J for Windows。

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 JarBundler for OS X or Launch4J for Windows.

您可以在应用程序运行时设置应用程序停靠栏图标,请参阅com.apple.eawt.Application。 setDockIconImage 。但它并不完美,因为当你双击你的jar时,它会使用通用的java图标在Dock中启动,并且只有在java代码开始运行时弹跳或跳转后才会切换到自定义图标。另外,我认为它不会为未运行的jar设置停靠图标(不是说你可以将jar文件拖到停靠栏中 - 似乎对我不起作用)。

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).

这里有一些代码可以演示您可以设置的不同图像:

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);
    }
}

这篇关于在OS X上以Java swing设置默认应用程序图标图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆