【问题标题】:Why TrayIcon doesn't appear?为什么 TrayIcon 没有出现?
【发布时间】:2014-02-13 11:43:31
【问题描述】:

我正在尝试在已经出现的托盘中添加一个 TrayIcon。我是 Java 新手,所以我可能会调用错误的方法。有人可以帮我吗?我使用的代码是:

if (!SystemTray.isSupported()) {
    System.out.println("SystemTray is not supported");
    return;
}

final SystemTray tray = SystemTray.getSystemTray();
Image image = Toolkit.getDefaultToolkit().getImage("systemtray.png");
PopupMenu popup = new PopupMenu();
final TrayIcon trayIcon = new TrayIcon(image, "The Tip Text", popup);
trayIcon.setImageAutoSize(true);
try {
    tray.add(trayIcon);
} catch (AWTException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

ps.:图片在同一个代码包中。

【问题讨论】:

  • Toolkit 是否与此代码放在同一个包中?如果没有,Java 可能会在错误的位置进行搜索。
  • 不高兴没有例外@Sanjeev
  • 我不知道 Toolkit 在哪里。我只是按照 Oracle 示例中所示的方式调用它。我需要做点别的吗? @疯狂
  • 好的,Toolkit 是 Oracle 的类。然后它肯定会相对于它自己的目录搜索图像 - 如果你可以将它传递给文件的绝对路径,或者传递给 Toolkit 类的相对路径,它就会起作用。但这也会使您的代码复杂化,因为您需要先找出正确的路径,然后才能使用它的 getImage(String) 方法。

标签: java image swing system-tray trayicon


【解决方案1】:

这是一个例子。看看你可能哪里出错了。

  • 获取图片

    Image image= ImageIO.read(getClass().getResource("/resources/stackoverflow1.png"));
    

  • 文件结构


  • 完整的运行程序

这里

import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.UIManager;

public class HideToSystemTray extends JFrame {

    TrayIcon trayIcon;
    SystemTray tray;

    HideToSystemTray() throws IOException {
        super("SystemTray test");
        System.out.println("creating instance");
        try {
            System.out.println("setting look and feel");
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {
            System.out.println("Unable to set LookAndFeel");
        }
        if (SystemTray.isSupported()) {
            System.out.println("system tray supported");
            tray = SystemTray.getSystemTray();

            Image image = ImageIO.read(getClass().getResource("/resources/stackoverflow1.png"));
            ActionListener exitListener = new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    System.out.println("Exiting....");
                    System.exit(0);
                }
            };
            PopupMenu popup = new PopupMenu();
            MenuItem defaultItem = new MenuItem("Exit");
            defaultItem.addActionListener(exitListener);
            popup.add(defaultItem);
            defaultItem = new MenuItem("Open");
            defaultItem.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    setVisible(true);
                    setExtendedState(JFrame.NORMAL);
                }
            });
            popup.add(defaultItem);
            trayIcon = new TrayIcon(image, "SystemTray Demo", popup);
            trayIcon.setImageAutoSize(true);
        } else {
            System.out.println("system tray not supported");
        }
        addWindowStateListener(new WindowStateListener() {
            public void windowStateChanged(WindowEvent e) {
                if (e.getNewState() == ICONIFIED) {
                    try {
                        tray.add(trayIcon);
                        setVisible(false);
                        System.out.println("added to SystemTray");
                    } catch (AWTException ex) {
                        System.out.println("unable to add to tray");
                    }
                }
                if (e.getNewState() == 7) {
                    try {
                        tray.add(trayIcon);
                        setVisible(false);
                        System.out.println("added to SystemTray");
                    } catch (AWTException ex) {
                        System.out.println("unable to add to system tray");
                    }
                }
                if (e.getNewState() == MAXIMIZED_BOTH) {
                    tray.remove(trayIcon);
                    setVisible(true);
                    System.out.println("Tray icon removed");
                }
                if (e.getNewState() == NORMAL) {
                    tray.remove(trayIcon);
                    setVisible(true);
                    System.out.println("Tray icon removed");
                }
            }
        });
        setIconImage(ImageIO.read(getClass().getResource("/resources/stackoverflow1.png")));

        setVisible(true);
        setSize(300, 200);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) throws IOException {
        new HideToSystemTray();
    }
}

  • 结果

【讨论】:

    【解决方案2】:

    您的代码绝对没问题,可能找不到您的图片。 如果您使用的是 eclipse,它应该位于您的项目文件夹中。

    【讨论】:

      【解决方案3】:

      如果找不到图像(@Leo Pflug 注意到您的代码看起来不错),请尝试我一直在使用的这个小技巧:

      // place this class in the same directory as your image
      public class Resources {
          private Resources() {}
      }
      
      // then to obtain Image
      Image image = javax.imageio.ImageIO.read(
          Resources.class.getResourceAsStream("systemtray.png"));
      

      这将保证 Java 将在与 Resources.java (Resoures.class) 文件完全相同的目录中搜索 systemtray.png。

      我怀疑Toolkit 在您将图像与您的代码放在同一目录中时,会在其自己的目录中搜索它,因此会出现问题。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-02-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多