【问题标题】:To Hide JavaFx fxml or JavaFx swing application to System Tray将 JavaFx fxml 或 JavaFx swing 应用程序隐藏到系统托盘
【发布时间】:2013-01-15 14:23:33
【问题描述】:

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

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

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

他们有这种操作的例子吗?

【问题讨论】:

  • 正确答案是here

标签: javafx-2 system-tray fxml


【解决方案1】:

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

【讨论】:

  • @alscu 的回答就是你所解释的......谢谢
  • JavaFx 8 已经发布。现在可以在没有 AWT 的情况下使用系统托盘吗? JavaFx 看起来好多了。
【解决方案2】:

这里的关键是将隐式退出设置为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 8 已经发布。现在可以在没有 AWT 的情况下使用系统托盘吗? JavaFx 看起来好多了。
  • 这在 OS X 上对我不起作用。当我将鼠标悬停在托盘图标上时,它会挂起。
  • @Ascherer 你找到 OS X 的解决方案了吗?
  • 在我关闭应用程序(隐藏)后,应用程序线程(图形)关闭,之后无法从系统托盘恢复。我正在使用java 8u66。我认为 stage.hide() 方法正在破坏操作。
  • 我找到了解决问题的方法。后面有一个stackoverflow链接。 stackoverflow.com/questions/32355335/…
猜你喜欢
  • 2012-09-16
  • 2016-09-03
  • 2011-07-23
  • 1970-01-01
  • 1970-01-01
  • 2013-05-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多