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

system tray icon not displaying on startup of program

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

    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);
    }
}

可能是您的图标图片比分配的图片大 space。尝试使用 public void setImageAutoSize(boolean autosize) 方法作为 icn.setImageAutoSize(true)。这将重新调整图像的大小。