13

Jarファイルのアイコン画像を設定しようとしています。

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

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
4

5 に答える 5

30

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.

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);
    }
}
于 2012-07-01T04:22:16.760 に答える
9

Just to add my final solution for adding a MacOSX Dock icon in a pure-java application:

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.

于 2015-04-15T14:13:36.310 に答える
6

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:

<key>CFBundleIconFile</key>
<string>ApplicationName.icns</string>

Some related details are mentioned here.

于 2012-06-29T00:32:46.877 に答える
3

java -Xdock:icon=/path/myIcon.png myApp

于 2016-08-03T02:05:26.123 に答える
1

as i have gone through your error its related to MacOS jdk which has started appearing after upgrade to 10.7.4

go through this if this helps you :

于 2012-07-01T17:47:27.177 に答える