简体   繁体   English

如何为JFrame窗口和任务栏设置图标

[英]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. 我想在窗口中显示自己的图标而不是Java杯子。

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. 但是严重怀疑createImage(即使它是Object也不知道要导入什么。

Regards, 问候,

Regarding your TrayIcon issue, you can refer below for a solution: 关于您的TrayIcon问题,您可以参考以下解决方案:

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. 我还没有写过有关托盘图标的文章,但是最后我发现了设置jframe图标的主要问题。 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 1)将此代码放入jframe WindowOpened事件

2) Put Image in main folder where all of your form and java files are created eg 2)将图像放在创建所有表单和java文件的主文件夹中

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. 3)最重要的是文件名区分大小写,即icon.png无效,但Icon.png无效。

this way your icon will be there even after finally building your project. 这样,即使最终构建了项目,您的图标仍会在那里。

An example using setIconImages() : (same applies for setIconImage()) 使用setIconImages()的示例:(对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() ). 提示是使用“ getImage()”来返回图像(因为ImageIcon不能直接在setIconImages()中使用)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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