程序启动时不显示系统托盘图标

时间:2015-10-17 11:46:04

标签: java system-tray

我使用以下代码显示系统托盘图标和消息。显示启动消息和工具提示消息。但托盘图标未显示。图标位于图像文件夹中。我该如何解决这个问题?

    public void systemTray() {
    try {
        SystemTray tray = SystemTray.getSystemTray();
        ImageIcon icon=new ImageIcon(getClass().getResource("/images/Reg Member MO.png"));
        Image image = icon.getImage();
        TrayIcon icn = new TrayIcon(image, "This is demonstration system tray");
        icn.setToolTip("Now you can see system tray\ntooltip here\nThis is demonstration system tray ToolTip");
        tray.add(icn);     
        icn.displayMessage("This is demonstration System Tray message", "You can add some text to\ndisplay here as System Tray Message", TrayIcon.MessageType.INFO);
    } catch (AWTException ex) {
        Logger.getLogger(StartupSystemTray.class.getName()).log(Level.SEVERE, null, ex);
    }
}

1 个答案:

答案 0 :(得分:0)

可能是您的图标图像太大而不是分配的空间。尝试将public void setImageAutoSize(boolean autosize)方法用作icn.setImageAutoSize(true)。这将重新调整图像大小。