日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > windows >内容正文

windows

java win10 通知,如何使用Java AWT创建和显示Windows 10通知

發布時間:2025/3/11 windows 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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通知的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。