【问题标题】:JFrame - Icon not showing on Sys tray when JFrame has been minimisedJFrame - 当 JFrame 最小化时,图标未显示在系统托盘上
【发布时间】:2013-07-01 14:13:23
【问题描述】:

我目前正在处理将 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();
                }
            }
        });
    }

问题是最小化时,我在打开系统托盘时看不到图像/图标,即我可以看到工具提示,但看不到与我的程序相关的图标 - 见下图

关于我缺少什么的任何想法?我觉得这可能是一些基本的东西。

【问题讨论】:

  • 您确定您的 bosten.png 文件已正确加载吗?
  • 如需尽快获得更好的帮助,请发帖SSCCE
  • 嗨 Grammin,感谢您的回复.. 图像加载正确.. 我可以通过在控制台上打印图像位置来确认这一点,即 rfc95Panel.getClass().getClassLoader().getResource()是:file:/C:/Users/TestProject/bin/boston.png ..我也可以在程序打开时看到任务栏上的图像..
  • 有什么想法吗?感谢您的帮助
  • Any ideas anyone? - 见安德鲁的评论。

标签: java swing icons jframe system-tray


【解决方案1】:

我遇到的上述问题的简单答案是我打算用作系统托盘图标的图像的大小。基本上我的尺寸是 64 x 64,但理想情况下应该是尺寸更小,我将图像缩小到 16 x 16。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-27
    • 2014-10-02
    • 1970-01-01
    相关资源
    最近更新 更多