简体   繁体   English

JFrame-最小化JFrame后,图标未显示在Sys托盘上

[英]JFrame - Icon not showing on Sys tray when JFrame has been minimised

I'm currently dealing with minimizing a JFrame to the system tray and I've done so successfully as indicated below: 我目前正在设法将JFrame最小化到系统托盘,并且已成功完成,如下所示:

    // 
    URL resource = panel.getClass().getClassLoader().getResource("boston.png");
    System.out.println("rfc95Panel.getClass().getClassLoader().getResource() is: " + rfc95Panel.getClass().getClassLoader().getResource("boston.png"));

    Image image = Toolkit.getDefaultToolkit().getImage(resource);

    //
    frame.setIconImage(image);

    // 
    if (SystemTray.isSupported()) {
        final TrayIcon icon = new TrayIcon(image);
        icon.setToolTip("Program minimised");

        // 
        icon.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                frame.setVisible(true);
                frame.setExtendedState(frame.NORMAL);
                getSystemTray().remove(icon);
            }
        });

        // Adds the specified window state listener to receive window events 
        // from this window. If l is null, no exception is thrown and no action 
        // is performed.
        frame.addWindowListener(new WindowAdapter() {

            @Override
            public void windowIconified(WindowEvent e) {
                frame.setVisible(false);
                try {
                    getSystemTray().add(icon);
                } catch (AWTException e1) {
                    e1.printStackTrace();
                }
            }
        });
    }

The problem is when minimised I can't see the image/icon when I open the system tray ie I can see the tool tip but I can't see the icon related to my program - see image below 问题是最小化时,打开系统托盘时看不到图像/图标,即可以看到工具提示,但看不到与程序相关的图标-请参见下图

系统托盘映像

Any ideas as to what I'm missing? 关于我所缺少的任何想法吗? I feel it might be something basic. 我觉得这可能是基本的东西。

The simple answer to the above problem I was encountering was the size of the image I intended to use as the sys tray icon. 我遇到的上述问题的简单答案是打算用作系统任务栏图标的图像大小。 Basically my dimensions were 64 by 64 but ideally it should be that the dimensions are smaller in my case I reduced the image to 16 by 16. 基本上我的尺寸是64 x 64,但理想情况下应该是尺寸较小,在我的情况下,我将图像缩小到16 x 16。

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

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