Set the default application icon image in Java swing on OS X

I'm setting up an icon image for a jar file:

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

When on Mac OS X 10.7 When running in 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

Solution

Seticonimage does not set the jar icon It will set the icon of the minimized window of the JFrame Jar file itself cannot set jar icon (it controls Finder Icon and dock application icon) You only need to get the default icon provided by the operating system You will need to use something like jarbundler for OS X or launch4j for windows

You can set the application dock icon when the application is running, see com apple. eawt. Application. setDockIconImage. However, it is not perfect, because when you double-click your jar, it will start in the dock using the general Java icon, and only switch to your custom rebound icon or when two Java codes start running In addition, I don't think it will set a dock icon without running jars (not that you can drag a jar file to the dock - it doesn't seem to apply to me)

Here are some codes that can set different images:

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);
    }
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>