Java系统托盘隐藏和还原应用程序

时间:2019-01-02 17:05:43

标签: java swing javafx java-8 system-tray

嘿,我这里有这段Java代码,它通过遵循THIS示例在系统框架中创建图标:

    public static void createTray() {
            //Check the SystemTray is supported
            if (!SystemTray.isSupported()) {
                JOptionPane.showMessageDialog(null, "SystemTray is not supported", "DOH! An error", JOptionPane.ERROR_MESSAGE);
                return;
            }

            URL imageURL = winBuilder.class.getResource("/bulb.gif");
            trayIcon = new TrayIcon((new ImageIcon(imageURL, "")).getImage());
            tray = SystemTray.getSystemTray();
            trayIcon.setImageAutoSize(true);

            // Create a pop-up menu components
            MenuItem aboutItem = new MenuItem("About");
            MenuItem exitItem = new MenuItem("Exit");

            //Add components to pop-up menu
            popup.add(aboutItem);
            popup.addSeparator();
            popup.add(exitItem);

            trayIcon.setPopupMenu(popup);

            try {
                tray.add(trayIcon);
            } catch (AWTException e) {
                JOptionPane.showMessageDialog(null, "TrayIcon could not be added: " + e.getLocalizedMessage(), "DOH! An error", JOptionPane.ERROR_MESSAGE);
            }

            trayIcon.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    //Double-click action on icon
                    frmUftWebui.setVisible(true);
                    frmUftWebui.repaint();
                    SystemTray.getSystemTray().remove(trayIcon);
                }
            });

            aboutItem.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    JOptionPane.showMessageDialog(null,
                            "This dialog box is run from the About menu item");
                }
            });

            exitItem.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    trayIcon.displayMessage("Exit", "The cool program has closed.", TrayIcon.MessageType.INFO); //ERROR | WARNING | INFO | NONE
                tray.remove(trayIcon);
                System.exit(0);
            }
        });
    }

这确实可以正常工作,并在系统灰阶中显示图标,但是一旦我单击两次图标,它会弹出一个新窗口代替我的原始窗口?

这是我的主要代码:

try {
    UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
} catch (UnsupportedLookAndFeelException ex) {
    ex.printStackTrace();
} catch (IllegalAccessException ex) {
    ex.printStackTrace();
} catch (InstantiationException ex) {
    ex.printStackTrace();
} catch (ClassNotFoundException ex) {
    ex.printStackTrace();
}

/* Turn off metal's use of bold fonts */
UIManager.put("swing.boldMetal", Boolean.FALSE);

try {
    frmUftWebui.setVisible(false);
    frmUftWebui.getContentPane().setLayout(null);
    frmUftWebui.setState(frmUftWebui.ICONIFIED);

    // Web HTML panel
    jfxPanel = new JFXPanel();
    jfxPanel.setBackground(Color.WHITE);
    jfxPanel.setBounds(0, 0, 1098, 523);
    frmUftWebui.getContentPane().add(jfxPanel);

    javafx.application.Platform.runLater(new Runnable() {
@SuppressWarnings({ "unchecked", "unused" })
public void run() {
    createTray();
    ............................
} catch (Exception e) {
     JOptionPane.showMessageDialog(null, "Error: " + e.getLocalizedMessage(), "DOH! An error", JOptionPane.ERROR_MESSAGE);
}
}

单击系统任务栏中的图标后,要打开原始窗口而不是创建一个新窗口,我会缺少什么?

0 个答案:

没有答案