将JavaFx fxml或JavaFx swing应用程序隐藏到系统托盘

12

我希望开发一个网站的客户端应用程序。

我希望当应用程序被最小化时,它可以停留在系统托盘中

我不知道如何完成这个任务。

有没有类似操作的例子?


1
正确答案在这里 - Aubin
2个回答

37

关键是将隐式退出设置为false Platform.setImplicitExit(false);,此外,在一个新线程中显示和隐藏舞台也很重要。

 Platform.runLater(new Runnable() {
    @Override
    public void run() {
        stage.show();
    }
 });

 Platform.runLater(new Runnable() {
    @Override
    public void run() {
        stage.hide();
    }
 });

接下来,是整个代码:

import java.awt.AWTException;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.TrayIcon;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.URL;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;
import javax.imageio.ImageIO;

/**
 *
 * @author alvaro
 */
public class TrayTest extends Application {

    private boolean firstTime;
    private TrayIcon trayIcon;


    public static void main(String[] args)
    {
        launch(args);
    }

    @Override
    public void start(Stage stage) throws Exception {
        createTrayIcon(stage);
        firstTime = true;
        Platform.setImplicitExit(false);
        Scene scene = new Scene(new Group(), 800, 600);
        stage.setScene(scene);
        stage.show();

    }

    public void createTrayIcon(final Stage stage) {
        if (SystemTray.isSupported()) {
            // get the SystemTray instance
            SystemTray tray = SystemTray.getSystemTray();
            // load an image
            java.awt.Image image = null;
            try {
                URL url = new URL("http://www.digitalphotoartistry.com/rose1.jpg");
                image = ImageIO.read(url);
            } catch (IOException ex) {
                System.out.println(ex);
            }


            stage.setOnCloseRequest(new EventHandler<WindowEvent>() {
                @Override
                public void handle(WindowEvent t) {
                    hide(stage);
                }
            });
            // create a action listener to listen for default action executed on the tray icon
            final ActionListener closeListener = new ActionListener() {
                @Override
                public void actionPerformed(java.awt.event.ActionEvent e) {
                    System.exit(0);
                }
            };

            ActionListener showListener = new ActionListener() {
                @Override
                public void actionPerformed(java.awt.event.ActionEvent e) {
                    Platform.runLater(new Runnable() {
                        @Override
                        public void run() {
                            stage.show();
                        }
                    });
                }
            };
            // create a popup menu
            PopupMenu popup = new PopupMenu();

            MenuItem showItem = new MenuItem("Show");
            showItem.addActionListener(showListener);
            popup.add(showItem);

            MenuItem closeItem = new MenuItem("Close");
            closeItem.addActionListener(closeListener);
            popup.add(closeItem);
            /// ... add other items
            // construct a TrayIcon
            trayIcon = new TrayIcon(image, "Title", popup);
            // set the TrayIcon properties
            trayIcon.addActionListener(showListener);
            // ...
            // add the tray image
            try {
                tray.add(trayIcon);
            } catch (AWTException e) {
                System.err.println(e);
            }
            // ...
        }
    }

    public void showProgramIsMinimizedMsg() {
        if (firstTime) {
            trayIcon.displayMessage("Some message.",
                    "Some other message.",
                    TrayIcon.MessageType.INFO);
            firstTime = false;
        }
    }

    private void hide(final Stage stage) {
        Platform.runLater(new Runnable() {
            @Override
            public void run() {
                if (SystemTray.isSupported()) {
                    stage.hide();
                    showProgramIsMinimizedMsg();
                } else {
                    System.exit(0);
                }
            }
        });
    }
}

1
JavaFx 8已经发布了。现在可以在没有AWT的情况下使用系统托盘吗?JavaFx看起来更漂亮。 - qed
在我的OS X上这个不起作用。当我悬停在托盘图标上时,它会卡住。 - Ascherer
@Ascherer,你在 OS X 上找到解决方案了吗? - thanili
当我关闭应用程序(隐藏)后,应用程序线程(图形界面)关闭,此后无法从系统托盘中恢复。我正在使用Java 8u66。我认为stage.hide()方法破坏了操作。 - Guerino Rodella
我找到了解决我的问题的方法。这里有一个stackoverflow链接。 https://dev59.com/pY7ea4cB1Zd3GeqPErWz - Guerino Rodella
显示剩余3条评论

0
据我所知,这将在JFX 8中实现。目前最好的解决方案是将您的应用程序嵌入到AWT中,并隐藏AWT窗口本身。

1
@alscu的回答就是你所解释的..谢谢 - Mohammad Sadiq Shaikh
JavaFx 8已经发布了。现在可以在没有AWT的情况下使用系统托盘吗?JavaFx看起来更漂亮。 - qed

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接