我已使用this answer在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);
}
}
答案 0 :(得分:2)
我认为这是由Windows本身或JVM的本机实现管理的。至少,公共API不提供在屏幕上为通知设置特定时间的选项。
除非您需要坚持使用操作中心,否则您可以考虑使用外部库进行桌面通知,例如:
JOptionpane.showMessageDialog()
相同的方式使用,也可以在手动发布之前构建和自定义通知对象。可以自定义颜色主题,图标,屏幕上的时间和动作等属性,还可以提供库存主题和图标。您可以免费获取并试用其中任何一种。