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

标签 javafx-2 system-tray fxml

我想为网站开发一个客户端应用程序。

我希望应用程序驻留在 最小化时的系统托盘。

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

他们是否有任何此类操作的示例。

最佳答案

这里的关键是将隐式退出设置为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);
                }
            }
        });
    }
}

关于javafx-2 - 将 JavaFx fxml 或 JavaFx swing 应用程序隐藏到系统托盘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14626550/

相关文章:

java - 在 JavaFX 中生成子窗口

java - 如何将子菜单添加到 MenuItem

vb.net - VB.NET 中的自定义托盘面板

java - 如何测试当前在特定 ImageView 中的图像并为每个可能的图像提供定制的输出?

javafx - 为什么子窗口(子阶段)内容不会自动调整大小?

java - 如何将 JavaFX 标签绑定(bind)到 ListView 中的选定项

java - 仅对 JavaFx 中 TextArea 的第一行应用空格/填充

java - Platform.runLater 问题 - 延迟执行

java - 如何在 SWT 中禁用系统托盘图标的上下文菜单?

java - 在 .fxml 中添加自定义项目