【问题标题】:System tray displayMessage() not showing系统托盘 displayMessage() 未显示
【发布时间】:2013-06-29 19:12:02
【问题描述】:

所以我想要的是当我的程序运行时(它是一个系统托盘),其中一个小通知问题会出现在屏幕的右下角。我试过了。

trayIcon = new TrayIcon(image, "Title", popup);
trayIcon.setImageAutoSize(true);
trayIcon.displayMessage("Title", "MESSAGE HERE", TrayIcon.MessageType.ERROR) //THIS IS THE LINE THAT SHOULD SHOW THE MESSAGE

当程序运行时它应该在哪里运行,这是具有正确参数的正确方法吗?

【问题讨论】:

    标签: java swing system-tray


    【解决方案1】:

    你读过How to Use the System Tray吗?

    然而

    检查这个最小的例子:

    tray.add(trayIcon); 之后显示您的消息。

    import java.awt.AWTException;
    import java.awt.Graphics2D;
    import java.awt.SystemTray;
    import java.awt.TrayIcon;
    import java.awt.image.BufferedImage;
    import java.net.URL;
    import javax.imageio.ImageIO;
    import javax.swing.SwingUtilities;
    
    public class Test {
    
        public Test() throws Exception {
            initComponents();
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        new Test();
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }
                }
            });
        }
    
        private void initComponents() throws Exception {
            createAndShowTray();
        }
    
        private void createAndShowTray() throws Exception {
            //Check the SystemTray is supported
            if (!SystemTray.isSupported()) {
                System.out.println("SystemTray is not supported");
                return;
            }
    
            //retieve icon form url and scale it to 32 x 32
            final TrayIcon trayIcon = new TrayIcon(resizeImage(ImageIO.read(
                    new URL("http://www.optical-illusions.in/illusions/blue_rotation_optical_illusion.jpg")), BufferedImage.TYPE_INT_ARGB, 32, 32));
    
            //get the system tray
            final SystemTray tray = SystemTray.getSystemTray();
    
            try {
                tray.add(trayIcon);
            } catch (AWTException e) {
                System.out.println("TrayIcon could not be added.");
            }
    
            trayIcon.displayMessage("Title", "MESSAGE HERE", TrayIcon.MessageType.ERROR); //THIS IS THE LINE THAT SHOULD SHOW THE MESSAGE
    
        }
    
        private static BufferedImage resizeImage(BufferedImage originalImage, int type, int IMG_WIDTH, int IMG_HEIGHT) {
            BufferedImage resizedImage = new BufferedImage(IMG_WIDTH, IMG_HEIGHT, type);
            Graphics2D g = resizedImage.createGraphics();
            g.drawImage(originalImage, 0, 0, IMG_WIDTH, IMG_HEIGHT, null);
            g.dispose();
    
            return resizedImage;
        }
    }
    

    【讨论】:

    • 哦,我知道我做错了什么,我必须在tray.add(trayIcon)之后添加它尝试 catch 语句。有道理,谢谢。
    • 我这样做了,但仍然无法正常工作,奇怪的是,我从另一个可以工作的项目中获取代码,所以我不明白我在做什么错
    【解决方案2】:

    进入小动作中心,点击“管理通知” 然后在设置中向下滚动,直到到达

    当我将它放在循环中时它不起作用,所以我只是尝试在循环之外添加一个附加语句,然后它就起作用了。耶!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-10-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多