gpt4 book ai didi

java - 如何用 Java 制作 Windows 通知

转载 作者:IT老高 更新时间:2023-10-28 20:21:05 26 4
gpt4 key购买 nike

在 Windows 10 中,屏幕右下方会打开一个通知,我发现它们非常有用。

有没有什么方法可以在 Java 中创建 Windows 通知?这就是他们的样子:

sample of windows notification desired

最佳答案

我可以使用这个非常简单的示例代码成功地产生这个结果:

screenshot

import java.awt.*;
import java.awt.TrayIcon.MessageType;

public class TrayIconDemo {

public static void main(String[] args) throws AWTException {
if (SystemTray.isSupported()) {
TrayIconDemo td = new TrayIconDemo();
td.displayTray();
} else {
System.err.println("System tray not supported!");
}
}

public void displayTray() throws AWTException {
//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.getDefaultToolkit().createImage(getClass().getResource("icon.png"));

TrayIcon trayIcon = new TrayIcon(image, "Tray Demo");
//Let the system resize 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);
}
}

关于java - 如何用 Java 制作 Windows 通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34490218/

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