2016-04-26 45 views

回答

0

正如我所知,JavaFX不會自動保存/恢復窗口位置/維度。

因此,您應該在顯示窗口之前恢復窗口位置/尺寸並在窗口隱藏之前保存。比如我使用這樣的幫手:

import javafx.stage.Stage; 

import java.util.prefs.Preferences; 

public class StagePositionManager { 

    public static final String IS_FIRST_RUN_KEY = "isFirstRun"; 
    public static final String X_KEY = "x"; 
    public static final String Y_KEY = "y"; 
    public static final String WIDTH_KEY = "width"; 
    public static final String HEIGHT_KEY = "height"; 

    private final Stage stage; 
    private final Class<?> windowClass; 

    public StagePositionManager(Stage stage, Class<?> windowClass) { 
     this.stage = stage; 
     this.windowClass = windowClass; 

     restoreStagePosition(); 
     stage.setOnHidden(event -> saveStagePosition()); 
    } 

    private void saveStagePosition() { 
     Preferences preferences = Preferences.userNodeForPackage(windowClass); 

     preferences.putBoolean(IS_FIRST_RUN_KEY, false); 

     preferences.putDouble(X_KEY, stage.getX()); 
     preferences.putDouble(Y_KEY, stage.getY()); 
     preferences.putDouble(WIDTH_KEY, stage.getWidth()); 
     preferences.putDouble(HEIGHT_KEY, stage.getHeight()); 
    } 

    private void restoreStagePosition() { 
     Preferences preferences = Preferences.userNodeForPackage(windowClass); 

     if (!preferences.getBoolean(IS_FIRST_RUN_KEY, true)) { 
      stage.setX(preferences.getDouble(X_KEY, 0)); 
      stage.setY(preferences.getDouble(Y_KEY, 0)); 
      stage.setWidth(preferences.getDouble(WIDTH_KEY, 1024)); 
      stage.setHeight(preferences.getDouble(HEIGHT_KEY, 768)); 
     } 
    } 

} 

後應用程序啓動叫它:

public class MyApplication extends Application { 

    @Override 
    public void start(Stage primaryStage) throws Exception { 
     ... 
     new StagePositionManager(primaryStage, Main.class); 
     ...