我已经成功地在 Windows 10 操作中心中使用此答案创建了一个通知弹窗。问题是,通知会停留5秒钟,然后一旦它消失,就会从操作中心中删除。我该如何使操作中心保留通知直到用户关闭它?以下是代码:
import java.awt.*;
import java.awt.TrayIcon.MessageType;
import javax.swing.JOptionPane;
public class Win10Notif {
public static void main(String[] args) throws AWTException, java.net.MalformedURLException {
if (SystemTray.isSupported()) {
Win10Notif td = new Win10Notif();
td.displayTray();
} else {
System.err.println("System tray not supported!");
}
}
public void displayTray() throws AWTException, java.net.MalformedURLException {
//Obtain only one instance of the SystemTray object
SystemTray tray = SystemTray.getSystemTray();
//If the icon is a file
Image image = Toolkit.getDefaultToolkit().createImage("icon.png");
//Alternative (if the icon is on the classpath):
//Image image = Toolkit.getToolkit().createImage(getClass().getResource("icon.png"));
TrayIcon trayIcon = new TrayIcon(image, "Tray Demo");
//Let the system resizes the image if needed
trayIcon.setImageAutoSize(true);
//Set tooltip text for the tray icon
trayIcon.setToolTip("System tray icon demo");
tray.add(trayIcon);
trayIcon.displayMessage("Hello, World", "notification demo", MessageType.INFO);
}
}