java win10 通知,如何使用Java AWT创建和显示Windows 10通知
在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);
}
}
前面的代碼將生成以下框架并顯示通知:
編碼愉快!
總結
以上是生活随笔為你收集整理的java win10 通知,如何使用Java AWT创建和显示Windows 10通知的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 绝地求生7月5日服务器维护,绝地求生7月
- 下一篇: java observer模式_Java