在Java中, 如何生成不同类型的通知或警报非常令人困惑。一些开发人员更喜欢使用JOptionPane, 但是当你在固定环境中工作时(例如在Windows 10中), 使用Windows的默认通知样式非常好, 因此这就是为什么我们向你展示一个简短的摘要来显示Java AWT轻松实现Windows 10通知。
以下代码在系统托盘中生成所需的通知, 因此你可以简单地为其创建一个方法, 将其包装在代码中, 或者仅更改警报的文本即可:
import java.awt.*;
import java.awt.event.*;
import java.awt.TrayIcon.MessageType;
import java.net.MalformedURLException;
try{
//Obtain only one instance of the SystemTray object
SystemTray tray = SystemTray.getSystemTray();
// If you want to create an icon in the system tray to preview
Image image = Toolkit.getDefaultToolkit().createImage("some-icon.png");
//Alternative (if the icon is on the classpath):
//Image image = Toolkit.getDefaultToolkit().createImage(getClass().getResource("icon.png"));
TrayIcon trayIcon = new TrayIcon(image, "Java AWT 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);
// Display info notification:
trayIcon.displayMessage("Hello, World", "Java Notification Demo", MessageType.INFO);
// Error:
// trayIcon.displayMessage("Hello, World", "Java Notification Demo", MessageType.ERROR);
// Warning:
// trayIcon.displayMessage("Hello, World", "Java Notification Demo", MessageType.WARNING);
}catch(Exception ex){
System.err.print(ex);
}
请注意, 代码的执行需要通过Try-Catch语句完成, 该语句可以捕获代码抛出的2个异常(AWTException, MalformedURLException)或常规异常(如上所示)。
结构化的例子
下面的示例显示了一个非常简单的应用程序类, 该类在Frame中绘制了一个简单的按钮。单击该按钮时, 将出现一个托盘通知:
package sandbox;
import java.awt.*;
import java.awt.event.*;
import java.awt.TrayIcon.MessageType;
import java.net.MalformedURLException;
public class Sandbox {
/**
* Parsing a JSONObject string
*
* @param args
*/
public static void main(String[] args) {
Sandbox app = new Sandbox();
}
public Sandbox(){
Frame f = new Frame("Button Example");
Button btn = new Button("Click Here");
btn.setBounds(50, 100, 80, 30);
f.add(btn);
f.setSize(400, 400);
f.setLayout(null);
f.setVisible(true);
Sandbox _this = this;
btn.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
if (SystemTray.isSupported()) {
try{
_this.displayTray();
}catch(AWTException ex){
}catch(MalformedURLException ex){
}
} else {
System.err.println("System tray not supported!");
}
}
});
}
public void displayTray() throws AWTException, 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.getDefaultToolkit().createImage(getClass().getResource("icon.png"));
TrayIcon trayIcon = new TrayIcon(image, "Java AWT 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", "Java Notification Demo", MessageType.INFO);
}
}
前面的代码将生成以下框架并显示通知:
编码愉快!