Skip to content

Commit c86ce32

Browse files
committed
记住上次退出时的主窗体位置
1 parent 9a1c431 commit c86ce32

2 files changed

Lines changed: 91 additions & 25 deletions

File tree

src/main/java/com/xwintop/xJavaFxTool/Main.java

Lines changed: 66 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,23 @@
33
import com.jfoenix.controls.JFXDecorator;
44
import com.xwintop.xJavaFxTool.controller.IndexController;
55
import com.xwintop.xJavaFxTool.utils.Config;
6+
import com.xwintop.xJavaFxTool.utils.Config.Keys.MainWindow;
67
import com.xwintop.xJavaFxTool.utils.StageUtils;
78
import com.xwintop.xJavaFxTool.utils.XJavaFxSystemUtil;
89
import com.xwintop.xcore.util.javafx.AlertUtil;
910
import com.xwintop.xcore.util.javafx.JavaFxViewUtil;
11+
import java.util.ResourceBundle;
12+
import java.util.prefs.Preferences;
1013
import javafx.application.Application;
11-
import javafx.event.EventHandler;
14+
import javafx.application.Platform;
15+
import javafx.event.Event;
1216
import javafx.fxml.FXMLLoader;
1317
import javafx.scene.Parent;
1418
import javafx.scene.Scene;
1519
import javafx.scene.image.Image;
1620
import javafx.stage.Stage;
17-
import javafx.stage.WindowEvent;
1821
import lombok.extern.slf4j.Slf4j;
1922

20-
import java.util.ResourceBundle;
21-
2223
/**
2324
* @ClassName: Main
2425
* @Description: 启动类
@@ -27,6 +28,9 @@
2728
*/
2829
@Slf4j
2930
public class Main extends Application {
31+
32+
public static final String PREFERENCE_ROOT = "com.xwintop.xJavaFxTool";
33+
3034
private static Stage stage;
3135

3236
public static void main(String[] args) {
@@ -46,35 +50,75 @@ public void start(Stage primaryStage) throws Exception {
4650
FXMLLoader fXMLLoader = IndexController.getFXMLLoader();
4751
ResourceBundle resourceBundle = fXMLLoader.getResources();
4852
Parent root = fXMLLoader.load();
49-
JFXDecorator decorator = JavaFxViewUtil.getJFXDecorator(primaryStage, resourceBundle.getString("Title") + Config.xJavaFxToolVersions, "/images/icon.jpg", root);
50-
decorator.setOnCloseButtonAction(() -> {
51-
if (AlertUtil.showConfirmAlert("确定要退出吗?")) {
52-
System.exit(0);
53-
}
54-
});
53+
54+
JFXDecorator decorator = JavaFxViewUtil.getJFXDecorator(
55+
primaryStage,
56+
resourceBundle.getString("Title") + Config.xJavaFxToolVersions,
57+
"/images/icon.jpg",
58+
root
59+
);
60+
decorator.setOnCloseButtonAction(() -> confirmExit(null));
61+
5562
Scene scene = JavaFxViewUtil.getJFXDecoratorScene(decorator);
56-
// Scene scene = new Scene(root);
63+
5764
primaryStage.setResizable(true);
5865
primaryStage.setTitle(resourceBundle.getString("Title"));//标题
5966
primaryStage.getIcons().add(new Image("/images/icon.jpg"));//图标
6067
primaryStage.setScene(scene);
61-
primaryStage.show();
68+
primaryStage.setOnCloseRequest(this::confirmExit);
6269

63-
primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>() {
64-
@Override
65-
public void handle(WindowEvent event) {
66-
if (AlertUtil.showConfirmAlert("确定要退出吗?")) {
67-
System.exit(0);
68-
} else {
69-
event.consume();
70-
}
71-
}
72-
});
70+
loadPrimaryStageBound(primaryStage);
71+
primaryStage.show();
7372

7473
StageUtils.updateStageStyle(primaryStage);
7574
stage = primaryStage;
7675
}
7776

77+
private void loadPrimaryStageBound(Stage stage) {
78+
double left = getPreferences().getDouble(MainWindow.LEFT, -1);
79+
double top = getPreferences().getDouble(MainWindow.TOP, -1);
80+
double width = getPreferences().getDouble(MainWindow.WIDTH, -1);
81+
double height = getPreferences().getDouble(MainWindow.HEIGHT, -1);
82+
83+
if (left > 0) {
84+
stage.setX(left);
85+
}
86+
if (top > 0) {
87+
stage.setY(top);
88+
}
89+
if (width > 0) {
90+
stage.setWidth(width);
91+
}
92+
if (height > 0) {
93+
stage.setHeight(height);
94+
}
95+
}
96+
97+
private Preferences getPreferences() {
98+
return Preferences.userRoot().node(PREFERENCE_ROOT);
99+
}
100+
101+
private void confirmExit(Event event) {
102+
if (AlertUtil.showConfirmAlert("确定要退出吗?")) {
103+
savePrimaryStageBound();
104+
Platform.exit();
105+
System.exit(0);
106+
} else if (event != null) {
107+
event.consume();
108+
}
109+
}
110+
111+
private void savePrimaryStageBound() {
112+
if (stage == null || stage.isIconified()) {
113+
return;
114+
}
115+
116+
getPreferences().putDouble(MainWindow.LEFT, stage.getX());
117+
getPreferences().putDouble(MainWindow.TOP, stage.getY());
118+
getPreferences().putDouble(MainWindow.WIDTH, stage.getWidth());
119+
getPreferences().putDouble(MainWindow.HEIGHT, stage.getHeight());
120+
}
121+
78122
public static Stage getStage() {
79123
return stage;
80124
}

src/main/java/com/xwintop/xJavaFxTool/utils/Config.java

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,30 @@
66
* 配置文件
77
*/
88
public class Config {
9-
public static Locale defaultLocale = Locale.getDefault();// 设置系统语言
109

11-
public static final String xJavaFxToolVersions = "V0.2.0";// xJavaFxTool版本信息
12-
public static final int xJavaFxToolVersionsInteger = 12;// xJavaFxTool更新信息
10+
public static Locale defaultLocale = Locale.getDefault();// 设置系统语言
11+
12+
public static final String xJavaFxToolVersions = "V0.2.0";// xJavaFxTool版本信息
13+
14+
public static final int xJavaFxToolVersionsInteger = 12;// xJavaFxTool更新信息
15+
16+
/**
17+
* 集中存放配置 key 的地方
18+
*/
19+
public static class Keys {
20+
21+
/**
22+
* 窗体相关配置
23+
*/
24+
public static class MainWindow {
25+
26+
public static final String WIDTH = "main-window.width";
27+
28+
public static final String HEIGHT = "main-window.height";
29+
30+
public static final String TOP = "main-window.top";
31+
32+
public static final String LEFT = "main-window.left";
33+
}
34+
}
1335
}

0 commit comments

Comments
 (0)