简体   繁体   中英

how to set icon for JFrame window and tray

I would like to show my own icon instead of the Java cup in the window.

JFrame的屏幕截图

Also when minimized, I would like to display, my own image. How will I be able to do so?

And where should I position my image relative to the source file?

[UPDATE]

I tried but no luck

    TrayIcon trayIcon = new TrayIcon(Toolkit.getDefaultToolkit().createImage("image/accounting.gif"));

    //setIconImage();

    SystemTray tray = SystemTray.getSystemTray();

    try {
        tray.add(trayIcon);
    } catch (AWTException e) {
        System.out.println("TrayIcon could not be added.");
    }

Also i tried

TrayIcon trayIcon = new TrayIcon(createImage("images/bulb.gif", "tray icon"));

But seriously doubt createImage( and even if it is Object don't know what to import.

Regards,

Regarding your TrayIcon issue, you can refer below for a solution:

public static void createSystemTrayIcon() {

    if (SystemTray.isSupported()) {
        SystemTray tray = SystemTray.getSystemTray();
        Image image = Toolkit.getDefaultToolkit().getImage(
            System.getenv("MY_PROGRAM_HOME") + "game.ico"
        );

        PopupMenu popup = new PopupMenu();

        final MenuItem menuExit = new MenuItem("Quit");

        MouseListener mouseListener =
            new MouseListener() {
                public void mouseClicked(MouseEvent e) {}
                public void mouseEntered(MouseEvent e) {}
                public void mouseExited(MouseEvent e) {}
                public void mousePressed(MouseEvent e) {}
                public void mouseReleased(MouseEvent e) {}
        };

        ActionListener exitListener =
            new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    Runtime r = Runtime.getRuntime();
                    System.out.println("Exiting...");
                    r.exit(0);
                }
            };

        menuExit.addActionListener(exitListener);
        popup.add(menuExit);

        final TrayIcon trayIcon = new TrayIcon(image, "My program", popup);

        ActionListener actionListener =
            new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    trayIcon.displayMessage(
                        "My program ",
                        "version: blahblah",
                        TrayIcon.MessageType.INFO
                    );
            }
        };

        trayIcon.setImageAutoSize(true);
        trayIcon.addActionListener(actionListener);
        trayIcon.addMouseListener(mouseListener);

        try {
            tray.add(trayIcon);
        } catch (AWTException e) {
            System.err.println("TrayIcon could not be added.");
        }

    } else {
        //  System Tray is not supported
    }
}

I havent written about tray icon but Finally I found the main issue in setting the jframe icon. Here is my code. It is similar to other codes but here are few things to mind the game.

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

1) Put this code in jframe WindowOpened event

2) Put Image in main folder where all of your form and java files are created eg

src\ myproject\ myFrame.form
src\ myproject\ myFrame.java
src\ myproject\ OtherFrame.form
src\ myproject\ OtherFrame.java
src\ myproject\ Icon.png

3) And most important that name of file is case sensitive that is icon.png won't work but Icon.png.

this way your icon will be there even after finally building your project.

An example using setIconImages() : (same applies for setIconImage())

public MyFrame() {
    initComponents(); //Added by Netbeans
    List<Image> icons  = new ArrayList();
    icons.add(new ImageIcon(getClass().getResource("/com/example/icons/16/app.png")).getImage());
    icons.add(new ImageIcon(getClass().getResource("/com/example/icons/32/app.png")).getImage());
    this.setIconImages(icons);
}

The clue is in using the "getImage()" in order to return the Image (as ImageIcon can not be used directly in setIconImages() ).

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