0
我正在創建一個在Mac OSX El Capitan中運行的javaFx應用程序。當我從我的jar文件創建一個包到.app文件時,應用程序窗口維度發生變化。JavaFx應用程序打包後El Capitan中的窗口尺寸更改
我希望窗口尺寸在打包前後保持一致。任何幫助表示讚賞。
我正在創建一個在Mac OSX El Capitan中運行的javaFx應用程序。當我從我的jar文件創建一個包到.app文件時,應用程序窗口維度發生變化。JavaFx應用程序打包後El Capitan中的窗口尺寸更改
我希望窗口尺寸在打包前後保持一致。任何幫助表示讚賞。
正如我所知,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);
...