java swing工具提示与不同的消息

NoN*_*aMe 3 java swing system-tray tooltip

在java swing中有没有办法用不同的消息显示工具提示,实际上我正在处理一个应用程序,我必须在系统任务栏中的工具提示上更新应用程序的当前状态.提前致谢.

Dav*_*amp 7

给Dan和Guillaume Polet +1.只需setToolTipText()trayIcon组件上使用即可.

我为你做了一个简短的例子.

它将创建一个TrayIcon并将其添加到SystemTray.还有之后ToolTipTrayIcon将被更新,每5秒:

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.MalformedURLException;
import java.net.URL;
import javax.swing.SwingUtilities;
import javax.swing.Timer;

public class SystemTrayExample {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new SystemTrayExample().createAndAddTrayIcon();
            }
        });
    }

    private void createAndAddTrayIcon() {
        try {
            initComponents();
        } catch (MalformedURLException ex) {
            ex.printStackTrace();
        }
    }

    private void initComponents() throws MalformedURLException {

        //Check the SystemTray is supported
        if (!SystemTray.isSupported()) {
            System.out.println("SystemTray is not supported");
            return;
        }
        final PopupMenu popup = new PopupMenu();
        final TrayIcon trayIcon = new TrayIcon(Toolkit.getDefaultToolkit().createImage(new URL("http://docs.oracle.com/javase/tutorial/uiswing/examples/misc/TrayIconDemoProject/src/misc/images/bulb.gif")));
        trayIcon.setToolTip("I am the initial message");

        final SystemTray tray = SystemTray.getSystemTray();

        // Create a pop-up menu components
        MenuItem exitItem = new MenuItem("Exit");
        exitItem.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });

        //Add components to pop-up menu
        popup.add(exitItem);

        //set popmenu
        trayIcon.setPopupMenu(popup);

        try {
            tray.add(trayIcon);
        } catch (AWTException e) {
            System.out.println("TrayIcon could not be added.");
        }

        int delay = 5000; //milliseconds
        final Timer timer = new Timer(delay, new ActionListener() {

            int count = 1;

            @Override
            public void actionPerformed(ActionEvent evt) {

                System.out.println("Updating on EDT " + (SwingUtilities.isEventDispatchThread() ? "Yes" : "No"));

                if (count == 3) {
                    trayIcon.setToolTip("I am the last message");
                    ((Timer) evt.getSource()).stop();//stop timer
                }
                if (count == 2) {//check if we should change tooltip
                    trayIcon.setToolTip("I am the second message");
                }
                if (count == 1) {
                    trayIcon.setToolTip("I am the  first message");
                }

                count++;

            }
        });

        timer.start();//start timer to change tooltip
    }
}
Run Code Online (Sandbox Code Playgroud)