简体   繁体   中英

Java TrayIcon displayMessage 'may trigger ActionEvent' when does it/doesn't it?

When using TrayIcon.displayMessage to show a popup notification, the Java 6 documentation states that 'Clicking on the message may trigger an ActionEvent.'

http://docs.oracle.com/javase/6/docs/api/java/awt/TrayIcon.html#displayMessage%28java.lang.String,%20java.lang.String,%20java.awt.TrayIcon.MessageType%29

'May'? Thanks, documentation.

On my Windows 2000 test VM, clicking on the message does not appear to trigger an ActionEvent (unfortunately I don't own any newer Windows licenses to test), while the same code does trigger one in Ubuntu and OS X.

Note: Clicking on the icon itself does trigger an event on the mouse listener.

So anyway, my specific questions are:

  1. Am I correct that clicking the notification does not trigger an ActionEvent in Windows 2000, or is there something I'm doing wrong?

  2. Does it work to trigger an ActionEvent in Windows XP or Windows 7?

Minimal sample code is below. When I run this using java Test in Windows 2000, clicking on the notification does not generate any command-line output.

import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;

import javax.swing.SwingUtilities;

public class Test
{
    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                TrayIcon icon = new TrayIcon(
                    new BufferedImage(16, 16, BufferedImage.TYPE_INT_RGB));
                icon.addActionListener(new ActionListener()
                {
                    @Override
                    public void actionPerformed(ActionEvent arg0)
                    {
                        System.err.println("ActionEvent: " + arg0);
                    }
                });
                try
                {
                    SystemTray.getSystemTray().add(icon);
                }
                catch(AWTException e)
                {
                    e.printStackTrace();
                }
                icon.displayMessage("New message", "Can you click on this?",
                    TrayIcon.MessageType.INFO);
            }
        });
    }
}

您正在处理的问题是跨平台植入问题,这就是它“可能”触发事件的原因

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM