Hunter0x7c7
2023-09-08 c64f00a1f9a91cb82bd1b8308e0c9b29ef5de76f
src/main/java/com/github/hunter0x7c7/sync/utils/TrayUtil.java
@@ -2,17 +2,15 @@
import com.github.hunter0x7c7.sync.model.interfaces.Callback;
import com.github.hunter0x7c7.sync.model.storage.Session;
import javafx.application.Platform;
import javafx.stage.Stage;
import java.util.List;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import static com.github.hunter0x7c7.sync.model.global.Parameters.APPID;
import static com.github.hunter0x7c7.sync.model.global.Parameters.AppNameString;
public class TrayUtil {
@@ -64,7 +62,7 @@
        TrayIcon trayIcon = new TrayIcon(img, tooltip, popup);
        //设置图标尺寸自动适应
        trayIcon.setImageAutoSize(true);
        //trayIcon.setImageAutoSize(true);
        trayIcon.setActionCommand(APPID);
        trayIcon.addActionListener(new ActionListener() {
            @Override
@@ -102,8 +100,7 @@
            if (tray != null) {
                tray.remove(trayIcon);
            }
            //退出
            Platform.exit();
            exitApp();
        });
        //点击关闭按钮时隐藏场景
        stage.setOnCloseRequest(event -> {
@@ -114,4 +111,23 @@
            });
        });
    }
    //退出
    public void exitApp() {
        //UI线程
        Platform.runLater(new Runnable() {
            @Override
            public void run() {
                //退出前先关闭窗口
                Stage stage = Session.getInstance().getPrimaryStage();
                if (stage != null) {
                    stage.close();
                }
                //退出
                Platform.exit();
            }
        });
    }
}