Windows中的弹出窗口"Safe To Remove Hardware"类型



我正在处理一个java项目,我想在弹出窗口中显示一条消息,就像我们单击USB驱动器的弹出图标时在窗口中出现的"安全删除硬件"弹出窗口一样。我想用java代码在同样的弹出窗口中显示我的消息。

使用SystemTray类。

要创建一个带有工具提示的图标,请使用以下内容:

SystemTray tray = SystemTray.getSystemTray();
TrayIcon icon = new TrayIcon(....);
icon.setToolTip("I have finished my work");
icon.setActionListener(this);
tray.add(trayIcon);

然后,在显示工具提示的类中,实现ActionListener接口,以便在用户单击图标和/或工具提示时得到通知(这就是setActionListener()的作用)

有关更多详细信息,请参阅SystemTray、TrayIcon和ActionListener 的Javadocs

您只需要使用TrayIcon类的displayMessage(…)方法。试试这个代码,这就是你想要的:

import java.awt.*;
import java.net.URL;
import javax.swing.*;
public class BalloonExample
{
    private void createAndDisplayGUI()
    {   
        TrayIcon trayIcon = new TrayIcon(createImage(
                        "/image/caIcon.png", "tray icon"));
        SystemTray tray = SystemTray.getSystemTray();               
        try 
        {
            tray.add(trayIcon);
        } 
        catch (AWTException e) 
        {
            System.out.println("TrayIcon could not be added.");
            return;
        }
        trayIcon.displayMessage("Balloon", "My First Balloon", TrayIcon.MessageType.INFO);
    }
    //Obtain the image URL
    protected static Image createImage(String path, String description) {
        URL imageURL = BalloonExample.class.getResource(path);
        if (imageURL == null) {
            System.err.println("Resource not found: " + path);
            return null;
        } else {
            return (new ImageIcon(imageURL, description)).getImage();
        }
    }
    public static void main(String... args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                new BalloonExample().createAndDisplayGUI();
            }
        });
    }
}

看看我的问题。基本上,该工具提示是一个气球提示,您可以使用ShellNotifyIcon创建一个。

最新更新