Java更改托盘图标

时间:2014-06-09 20:18:34

标签: java dynamic replace icons tray

您好我正在尝试动态更改我在java中的托盘图标。例如,他们的图标是灰色方块。当用户点击托盘菜单中的项目时,我希望灰色方块将图像切换为红色方块。

Here is my current code for the tray icon.

'public class Utils {
    private static Image iconImage;
    private static Image iconImage2;
    private static SystemTray sysTray;
    private static PopupMenu menu;
    private static MenuItem item1;
    private static MenuItem item2;
    private static MenuItem item0;
    private static TrayIcon trayIcon;
    private static TrayIcon trayIcon2;
    public static void loadTrayIcon() {
        JFrame frame = new JFrame("AC Tray Frame");

        frame.setLayout(new GridLayout(1, 3)); 

        if (SystemTray.isSupported()) {
            sysTray = SystemTray.getSystemTray();

            iconImage  = Toolkit.getDefaultToolkit().getImage("osx_tray_icon.png");
            iconImage2  = Toolkit.getDefaultToolkit().getImage("loading.gif");

            menu = new PopupMenu();

            item0 = new MenuItem("This is a test");
            item0.setEnabled(false);
            item1 = new MenuItem("Test");
            item2 = new MenuItem("Exit");

            menu.add(item0);
            menu.addSeparator();
            menu.add(item1);
            menu.addSeparator();
            menu.add(item2);

            item1.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    try {
                        system.out.println("hi");
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                }
            });

            item2.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    System.exit(0);
                }
            });

            trayIcon = new TrayIcon(iconImage, "Test Title", menu);

            try {
                sysTray.add(trayIcon);
            }

            catch(AWTException e) {
                System.out.println(e.getMessage());
            }
        }
    }'

1 个答案:

答案 0 :(得分:1)

您只想使用TrayIcon#setImage来更改TrayIcon

显示的图标