gpt4 book ai didi

Java 更改托盘图标

转载 作者:太空宇宙 更新时间:2023-11-04 06:44:59 25 4
gpt4 key购买 nike

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

这是我当前的托盘图标代码。

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());
}
}
}

最佳答案

您只想使用 TrayIcon#setImage这将更改 TrayIcon

显示的图标

关于Java 更改托盘图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24128284/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com