0

我正在编写一个利用 JavaTrayIcon类的应用程序,但我似乎无法让它在 Windows XP/Vista 上显示消息。这是一个已知问题还是我遗漏了什么?(消息在 Windows 7 上应有的显示)

代码:

public class SysTray {
    public static void main(String[] args) throws Exception {
        TrayIcon icon = new TrayIcon(getImage());

        icon.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(null, "Good");
            }
        });
        SystemTray.getSystemTray().add(icon);

        icon.displayMessage("Attention", "Please click here", 
            TrayIcon.MessageType.WARNING);
    }

    private static Image getImage() throws HeadlessException {
        Icon defaultIcon = MetalIconFactory.getTreeHardDriveIcon();
        Image img = new BufferedImage(defaultIcon.getIconWidth(), 
            defaultIcon.getIconHeight(), BufferedImage.TYPE_4BYTE_ABGR);
        defaultIcon.paintIcon(new Panel(), img.getGraphics(), 0, 0);

        return img;
    }
}

编辑:计算机都使用最新版本的 Java

编辑二:在各种计算机上的测试结果:

  • Windows XP,J7_25 (1.7.0_25-b16):工作
  • Windows 7,J7_25 (1.7.0_25-b17):工作
  • Windows XP、J7_25 (1.7.0_25-b17):不工作
4

1 回答 1

1

see whats happened (Java6_022 and Java7_21, tested on both JDKs)

WinXP - -> enter image description here

Win8 - -> enter image description here

from code

import java.awt.AWTException;
import java.awt.EventQueue;
import java.awt.HeadlessException;
import java.awt.Image;
import java.awt.SystemTray;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import javax.swing.Icon;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.plaf.metal.MetalIconFactory;

public class SysTray {

    public SysTray() {
        TrayIcon icon = new TrayIcon(getImage());
        icon.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(null, "Good");
            }
        });
        try {
            SystemTray.getSystemTray().add(icon);
        } catch (AWTException ex) {
        }
        icon.displayMessage("Attn","Click here",TrayIcon.MessageType.WARNING);
    }

    private Image getImage() throws HeadlessException {
        Icon defaultIcon = MetalIconFactory.getTreeHardDriveIcon();
        Image img = new BufferedImage(defaultIcon.getIconWidth(),
                defaultIcon.getIconHeight(), BufferedImage.TYPE_4BYTE_ABGR);
        defaultIcon.paintIcon(new JPanel(), img.getGraphics(), 0, 0);
        return img;
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                SysTray sysTray = new SysTray();
            }
        });
    }
}
于 2013-07-22T07:23:13.000 回答