java win10 通知,如何使用Java AWT创建和显示Windows 10通知
在Java中, 如何生成不同類型的通知或警報(bào)非常令人困惑。一些開發(fā)人員更喜歡使用JOptionPane, 但是當(dāng)你在固定環(huán)境中工作時(shí)(例如在Windows 10中), 使用Windows的默認(rèn)通知樣式非常好, 因此這就是為什么我們向你展示一個(gè)簡短的摘要來顯示Java AWT輕松實(shí)現(xiàn)Windows 10通知。
以下代碼在系統(tǒng)托盤中生成所需的通知, 因此你可以簡單地為其創(chuàng)建一個(gè)方法, 將其包裝在代碼中, 或者僅更改警報(bào)的文本即可:
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);
}
請(qǐng)注意, 代碼的執(zhí)行需要通過Try-Catch語句完成, 該語句可以捕獲代碼拋出的2個(gè)異常(AWTException, MalformedURLException)或常規(guī)異常(如上所示)。
結(jié)構(gòu)化的例子
下面的示例顯示了一個(gè)非常簡單的應(yīng)用程序類, 該類在Frame中繪制了一個(gè)簡單的按鈕。單擊該按鈕時(shí), 將出現(xiàn)一個(gè)托盤通知:
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);
}
}
前面的代碼將生成以下框架并顯示通知:
編碼愉快!
總結(jié)
以上是生活随笔為你收集整理的java win10 通知,如何使用Java AWT创建和显示Windows 10通知的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 绝地求生7月5日服务器维护,绝地求生7月
- 下一篇: java observer模式_Java