33import com .jfoenix .controls .JFXDecorator ;
44import com .xwintop .xJavaFxTool .controller .IndexController ;
55import com .xwintop .xJavaFxTool .utils .Config ;
6+ import com .xwintop .xJavaFxTool .utils .Config .Keys .MainWindow ;
67import com .xwintop .xJavaFxTool .utils .StageUtils ;
78import com .xwintop .xJavaFxTool .utils .XJavaFxSystemUtil ;
89import com .xwintop .xcore .util .javafx .AlertUtil ;
910import com .xwintop .xcore .util .javafx .JavaFxViewUtil ;
11+ import java .util .ResourceBundle ;
12+ import java .util .prefs .Preferences ;
1013import javafx .application .Application ;
11- import javafx .event .EventHandler ;
14+ import javafx .application .Platform ;
15+ import javafx .event .Event ;
1216import javafx .fxml .FXMLLoader ;
1317import javafx .scene .Parent ;
1418import javafx .scene .Scene ;
1519import javafx .scene .image .Image ;
1620import javafx .stage .Stage ;
17- import javafx .stage .WindowEvent ;
1821import lombok .extern .slf4j .Slf4j ;
1922
20- import java .util .ResourceBundle ;
21-
2223/**
2324 * @ClassName: Main
2425 * @Description: 启动类
2728 */
2829@ Slf4j
2930public 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 }
0 commit comments