javafx系統托盤SystemTray實現

直接調用 MySystemTray.getInstance().listen(stage);  即可

如果你關閉 aStage 打開了 bStage , 系統托盤需要重新監聽bStage:   MySystemTray.getInstance().listen(bStage);

關閉窗口的時候可以調用 MySystemTray.getInstance().hide(stage);

 

簡單粗暴,直接上代碼:

/**
 * 自定義系統托盤(單例模式)
 */
public class MySystemTray {

    private static MySystemTray instance;
    private static MenuItem showItem;
    private static MenuItem exitItem;
    private static TrayIcon trayIcon;
    private static ActionListener showListener;
    private static ActionListener exitListener;
    private static MouseListener mouseListener;
    private Logger logger = LoggerFactory.getLogger(MySystemTray.class);

    static{
        //執行stage.close()方法,窗口不直接退出
        Platform.setImplicitExit(false);
        //菜單項(打開)中文亂碼的問題是編譯器的鍋,如果使用IDEA,需要在Run-Edit Configuration在LoginApplication中的VM Options中添加-Dfile.encoding=GBK
        //如果使用Eclipse,需要右鍵Run as-選擇Run Configuration,在第二欄Arguments選項中的VM Options中添加-Dfile.encoding=GBK
        showItem = new MenuItem("打開");
        //菜單項(退出)
        exitItem = new MenuItem("退出");
        //此處不能選擇ico格式的圖片,要使用16*16的png格式的圖片
        URL url = MySystemTray.class.getResource("/static/images/my.png");
        Image image = Toolkit.getDefaultToolkit().getImage(url);
        //系統托盤圖標
        trayIcon = new TrayIcon(image);
        //初始化監聽事件(空)
        showListener = e -> Platform.runLater(() -> {});
        exitListener = e -> {};
        mouseListener = new MouseAdapter() {};
    }

    public static MySystemTray getInstance(){
        if(instance == null){
            instance = new MySystemTray();
        }
        return instance;
    }

    private MySystemTray(){
        try {
            //檢查系統是否支持托盤
            if (!SystemTray.isSupported()) {
                //系統托盤不支持
                logger.info(Thread.currentThread().getStackTrace()[ 1 ].getClassName() + ":系統托盤不支持");
                return;
            }
            //設置圖標尺寸自動適應
            trayIcon.setImageAutoSize(true);
            //系統托盤
            SystemTray tray = SystemTray.getSystemTray();
            //彈出式菜單組件
            final PopupMenu popup = new PopupMenu();
            popup.add(showItem);
            popup.add(exitItem);
            trayIcon.setPopupMenu(popup);
            //鼠標移到系統托盤,會顯示提示文本
            trayIcon.setToolTip("提示文本");
            tray.add(trayIcon);
        } catch (Exception e) {
            //系統托盤添加失敗
            logger.error(Thread.currentThread().getStackTrace()[ 1 ].getClassName() + ":系統添加失敗", e);
        }
    }

    /**
     * 更改系統托盤所監聽的Stage
     */
    public void listen(Stage stage){
        //防止報空指針異常
        if(showListener == null || exitListener == null || mouseListener == null || showItem == null || exitItem == null || trayIcon == null){
            return;
        }
        //移除原來的事件
        showItem.removeActionListener(showListener);
        exitItem.removeActionListener(exitListener);
        trayIcon.removeMouseListener(mouseListener);
        //行爲事件: 點擊"打開"按鈕,顯示窗口
        showListener = e -> Platform.runLater(() -> showStage(stage));
        //行爲事件: 點擊"退出"按鈕, 就退出系統
        exitListener = e -> {
            System.exit(0);
        };
        //鼠標行爲事件: 單機顯示stage
        mouseListener = new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                //鼠標左鍵
                if (e.getButton() == MouseEvent.BUTTON1) {
                    showStage(stage);
                }
            }
        };
        //給菜單項添加事件
        showItem.addActionListener(showListener);
        exitItem.addActionListener(exitListener);
        //給系統托盤添加鼠標響應事件
        trayIcon.addMouseListener(mouseListener);
    }

    /**
     * 關閉窗口
     */
    public void hide(Stage stage){
        Platform.runLater(() -> {
            //如果支持系統托盤,就隱藏到托盤,不支持就直接退出
            if (SystemTray.isSupported()) {
                //stage.hide()與stage.close()等價
                stage.hide();
            } else {
                System.exit(0);
            }
        });
    }

    /**
     * 點擊系統托盤,顯示界面(並且顯示在最前面,將最小化的狀態設爲false)
     */
    private void showStage(Stage stage){
        //點擊系統托盤,
        Platform.runLater(() -> {
            if(stage.isIconified()){ stage.setIconified(false);}
            if(!stage.isShowing()){ stage.show(); }
            stage.toFront();
        });
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章