Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/390.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaFx在用户调整大小后保持窗口大小_Java_Javafx_Javafx 2_Fxml_Scene - Fatal编程技术网

JavaFx在用户调整大小后保持窗口大小

JavaFx在用户调整大小后保持窗口大小,java,javafx,javafx-2,fxml,scene,Java,Javafx,Javafx 2,Fxml,Scene,我目前使用以下代码在场景之间切换。我想在窗口之间切换并保持窗口大小(因此,当用户通过调整窗口大小手动更改窗口大小时,即使在场景更改后,它也将保持在选定的大小) 场景=新场景(窗格);//新场景(窗格,userChosenWidth??,userChosenHeight; 舞台场景; stage.sizeToScene(); 有人知道如何做到这一点吗?只要在当前的场景中调用getWidth()和getHeight()。如果没有对当前场景的引用,可以使用stage.getScene()获得一个引

我目前使用以下代码在场景之间切换。我想在窗口之间切换并保持窗口大小(因此,当用户通过调整窗口大小手动更改窗口大小时,即使在场景更改后,它也将保持在选定的大小)


场景=新场景(窗格);//新场景(窗格,userChosenWidth??,userChosenHeight;
舞台场景;
stage.sizeToScene();

有人知道如何做到这一点吗?

只要在当前的
场景中调用
getWidth()
getHeight()
。如果没有对当前场景的引用,可以使用
stage.getScene()

获得一个引用。我创建了这个类:

public class AppSize {

    private static final String HEIGHT = "Height";
    private static final String WIDTH = "Width";
    private static final String Y = "Y";
    private static final String X = "X";

    private final static Preferences prefs =  Preferences.userNodeForPackage(AppSize.class);

    public static void setSize(Stage stage) {

    Optional.ofNullable(prefs.getDouble(X, -1)).filter(x->x>=0).ifPresent(stage::setX);
    Optional.ofNullable(prefs.getDouble(Y, -1)).filter(y->y>=0).ifPresent(stage::setY);
    Optional.ofNullable(prefs.getDouble(WIDTH, -1)).filter(width->width>=0).ifPresent(stage::setWidth);
    Optional.ofNullable(prefs.getDouble(HEIGHT, -1)).filter(height->height>=0).ifPresent(stage::setHeight);

    stage.setOnCloseRequest(
            e->{

                if(stage.isFullScreen()) {
                    return;
                }

                prefs.putDouble(X, stage.getX());
                prefs.putDouble(Y, stage.getY());
                prefs.putDouble(WIDTH, stage.getWidth());
                prefs.putDouble(HEIGHT, stage.getHeight());
            });
    }
}
其用途:

 public void start(Stage stage) throws Exception {
    //...
    AppSize.setSize(stage);
    stage.show();
}

签出此库它可以对某个阶段执行您想要执行的所有操作: