在 Java Spring Boot 中使用系统托盘图标需要使用 Java AWT 和 Java Swing 库。以下是实现步骤: 1. 导入 Java AWT 和 Java Swing 库。 ```java import java.awt.*; import java.awt.event.*; import javax.swing.*; ``` 2. 创建一个系统托盘图标。 ```java if (SystemTray.isSupported()) { SystemTray tray = SystemTray.getSystemTray(); Image image = Toolkit.getDefaultToolkit().getImage("icon.png"); PopupMenu popup = new PopupMenu(); MenuItem exitItem = new MenuItem("Exit"); exitItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.exit(0); } }); popup.add(exitItem); TrayIcon trayIcon = new TrayIcon(image, "My Application", popup); trayIcon.setImageAutoSize(true); try { tray.add(trayIcon); } catch (AWTException e) { System.err.println("TrayIcon could not be added."); } } ``` 3. 添加一个双击系统托盘图标的事件。 ```java trayIcon.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { // Do something } }); ``` 4. 在应用程序退出时移除系统托盘图标。 ```java Runtime.getRuntime().addShutdownHook(new Thread() { public void run() { SystemTray tray = SystemTray.getSystemTray(); tray.remove(trayIcon); } }); ``` 完整代码示例: ```java import java.awt.*; import java.awt.event.*; import javax.swing.*; public class TrayIconExample { public static void main(String[] args) { if (SystemTray.isSupported()) { SystemTray tray = SystemTray.getSystemTray(); Image image = Toolkit.getDefaultToolkit().getImage("icon.png"); PopupMenu popup = new PopupMenu(); MenuItem exitItem = new MenuItem("Exit"); exitItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.exit(0); } }); popup.add(exitItem); TrayIcon trayIcon = new TrayIcon(image, "My Application", popup); trayIcon.setImageAutoSize(true); try { tray.add(trayIcon); } catch (AWTException e) { System.err.println("TrayIcon could not be added."); } trayIcon.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JOptionPane.showMessageDialog(null, "Hello World!"); } }); Runtime.getRuntime().addShutdownHook(new Thread() { public void run() { tray.remove(trayIcon); } }); } } } ```