Javafx 2 将JavaFXML或JavaFXSwing应用程序隐藏到系统托盘

Javafx 2 将JavaFXML或JavaFXSwing应用程序隐藏到系统托盘,javafx-2,system-tray,fxml,Javafx 2,System Tray,Fxml,我想为网站开发一个客户端应用程序 当最小化时,我希望应用程序驻留在系统托盘中。 我不知道如何完成这项任务 就我所知,这在JFX8中是可能的。现在最好的解决方案是将应用程序嵌入AWT并隐藏AWT窗口本身。这里的关键是将隐式退出设置为falsePlatform.setImplicitExit(false) 在新线程中显示和隐藏阶段也很重要 Platform.runLater(new Runnable() { @Override public void run() {

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

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

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


就我所知,这在JFX8中是可能的。现在最好的解决方案是将应用程序嵌入AWT并隐藏AWT窗口本身。

这里的关键是将隐式退出设置为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);
                }
            }
        });
    }
}
import java.awt.AWTException;
导入java.awt.MenuItem;
导入java.awt.PopupMenu;
导入java.awt.SystemTray;
导入java.awt.TrayIcon;
导入java.awt.event.ActionListener;
导入java.io.IOException;
导入java.net.URL;
导入javafx.application.application;
导入javafx.application.Platform;
导入javafx.event.EventHandler;
导入javafx.scene.Group;
导入javafx.scene.scene;
导入javafx.stage.stage;
导入javafx.stage.WindowEvent;
导入javax.imageio.imageio;
/**
*
*@作者阿尔瓦罗
*/
公共类TrayTest扩展了应用程序{
第一次私人布尔运算;
私人运输公司;
公共静态void main(字符串[]args)
{
发射(args);
}
@凌驾
public void start(Stage)引发异常{
createTrayIcon(阶段);
第一次=正确;
Platform.setImplicitExit(false);
场景=新场景(新组(),800600);
舞台场景;
stage.show();
}
公共空间createTrayIcon(最后阶段){
if(SystemTray.issuported()){
//获取SystemTray实例
SystemTray=SystemTray.getSystemTray();
//加载图像
java.awt.Image Image=null;
试一试{
URL=新URL(“http://www.digitalphotoartistry.com/rose1.jpg");
image=ImageIO.read(url);
}捕获(IOEX异常){
系统输出打印项次(ex);
}
stage.setOnCloseRequest(新的EventHandler(){
@凌驾
公共无效句柄(WindowEvent t){
隐藏(阶段);
}
});
//创建一个操作侦听器,以侦听在托盘图标上执行的默认操作
最终ActionListener closeListener=新ActionListener(){
@凌驾
public void actionPerformed(java.awt.event.ActionEvent e){
系统出口(0);
}
};
ActionListener showListener=新建ActionListener(){
@凌驾
public void actionPerformed(java.awt.event.ActionEvent e){
Platform.runLater(新的Runnable(){
@凌驾
公开募捐{
stage.show();
}
});
}
};
//创建一个弹出菜单
PopupMenu popup=新建PopupMenu();
MenuItem showItem=新MenuItem(“显示”);
showItem.addActionListener(showListener);
添加(showItem);
MenuItem closeItem=新MenuItem(“关闭”);
closeItem.addActionListener(closeListener);
弹出。添加(关闭项);
///…添加其他项目
//建造一个火车站
trayIcon=新trayIcon(图像,“标题”,弹出窗口);
//设置TrayIcon属性
trayIcon.addActionListener(showListener);
// ...
//添加托盘图像
试一试{
托盘。添加(trayIcon);
}捕获(awtexe){
系统错误println(e);
}
// ...
}
}
公共void showProgramIsMinimizedMsg(){
如果(第一次){
trayIcon.displayMessage(“某些消息”),
“其他信息。”,
TrayIcon.MessageType.INFO);
第一次=错误;
}
}
私人空间隐藏(最后阶段){
Platform.runLater(新的Runnable(){
@凌驾
公开募捐{
if(SystemTray.issuported()){
stage.hide();
showProgramIsMinimizedMsg();
}否则{
系统出口(0);
}
}
});
}
}

@alscu的答案就是你解释的。。谢谢,AvaFX8已经发行了。现在是否可以在没有AWT的情况下使用systray?JavaFx看起来好多了。JavaFX8已经上市了。现在是否可以在没有AWT的情况下使用systray?JavaFx看起来好多了。这对我在OS X上不起作用。当我将鼠标悬停在托盘图标上时,它会挂起。@a您找到OS X的解决方案了吗?在我关闭应用程序(隐藏)后,应用程序线程(图形线程)会关闭,之后无法从系统托盘还原。我正在使用Java8U66。我认为stage.hide()方法破坏了操作。我找到了解决问题的方法。下面有一个stackoverflow链接。正确答案是