how to set icon for JFrame window and tray

2020-08-01 11:21发布

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

screenshot of 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,

标签: java swing
4条回答
▲ chillily
2楼-- · 2020-08-01 11:31

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 e.g.

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.

查看更多
萌系小妹纸
3楼-- · 2020-08-01 11:37

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

查看更多
Fickle 薄情
4楼-- · 2020-08-01 11:39
来,给爷笑一个
5楼-- · 2020-08-01 11:55

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
    }
}
查看更多
登录 后发表回答