简体   繁体   中英

Change Java application icon?

Does anyone know how to change the default java application app icon? I am using a mac if it makes a difference. Any ideas are good!

You can use JFrame.setIconImage .

Here is an example .

Haven't seen any issues on the mac.

import javax.swing.JFrame;
import javax.swing.ImageIcon;

class JFrameTest
{
    public static void main(String _[])
    {
        JFrame jFrame = new JFrame("Hello World!!");
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jFrame.setIconImage(new ImageIcon("c:/home/ravi/creampink.png").getImage());
        jFrame.setSize(400,400);
        jFrame.setVisible(true);
    }
}

Just search for Download 128X128 icon in google to get some sample icons

The JFrame.setIconImage is where you want to work. It seems to be working on all operating systems.

Place the icon in your resources folder ( src/main/resources for a Maven project with default settings, or alongside the main window class file for an Eclipse project with default settings). (The accepted example uses a hard-coded path in the file system—that is OK in a lab setting, but will break on deployment.)

Assuming theWindow is the main window of your application, do the following:

theWindow.setIconImage(Toolkit.getDefaultToolkit().getImage(
    ClassLoader.getSystemResource("someicon.png")));

If you are subclassing JFrame , you can do this in the constructor (in that case, drop the theWindow. prefix).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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