日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

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

發(fā)布時間:2025/3/11 54 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java win10 通知,如何使用Java AWT创建和显示Windows 10通知 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

在Java中, 如何生成不同類型的通知或警報非常令人困惑。一些開發(fā)人員更喜歡使用JOptionPane, 但是當你在固定環(huán)境中工作時(例如在Windows 10中), 使用Windows的默認通知樣式非常好, 因此這就是為什么我們向你展示一個簡短的摘要來顯示Java AWT輕松實現(xiàn)Windows 10通知。

以下代碼在系統(tǒng)托盤中生成所需的通知, 因此你可以簡單地為其創(chuàng)建一個方法, 將其包裝在代碼中, 或者僅更改警報的文本即可:

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);

}

請注意, 代碼的執(zhí)行需要通過Try-Catch語句完成, 該語句可以捕獲代碼拋出的2個異常(AWTException, MalformedURLException)或常規(guī)異常(如上所示)。

結(jié)構(gòu)化的例子

下面的示例顯示了一個非常簡單的應(yīng)用程序類, 該類在Frame中繪制了一個簡單的按鈕。單擊該按鈕時, 將出現(xiàn)一個托盤通知:

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)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。