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

}

}

前面的代码将生成以下框架并显示通知:

56788e72e3e0b89063f99bf119289250.png

编码愉快!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值