0

我正在创建一个在 Mac OSX El Capitan 中运行的 javaFx 应用程序。当我从我的 jar 文件创建一个包到 .app 文件时,应用程序窗口尺寸会发生变化。

我希望捆绑前后的窗口尺寸相同。任何帮助表示赞赏。

4

1 回答 1

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);
        ...
于 2016-04-26T15:36:05.120 回答