Setting the width and height of the scene

I am trying to set the width and height of a scene outside the constructor, and that was useless. Looking through the API Scene, I saw a method that allows you to get the height and width, but not one, to set the ..: s method (possibly a design flaw).

After further research, I came across SceneBuilderand found methods that could change the height and width. However, I do not know how to apply it to an already created scene object or how to create an object SceneBuilderthat can be used instead of a scene object.

+6
source share
3 answers

Stage.setWidth Sceneand assigning it Stage, you can use Stage.setWidthand Stage.setHeightto simultaneously change the scene and the scene size.

SceneBuilder , .

+9

, , .

http://docs.oracle.com/javase/8/javafx/api/javafx/scene/Scene.html

setWidth() setHeight(), ReadOnly,

Constructors

Scene(Parent root)
Creates a Scene for a specific root Node.

Scene(Parent root, double width, double height)
Creates a Scene for a specific root Node with a specific size.

Scene(Parent root, double width, double height, boolean depthBuffer)
Constructs a scene consisting of a root, with a dimension of width and height, and specifies whether a depth buffer is created for this scene.

Scene(Parent root, double width, double height, boolean depthBuffer, SceneAntialiasing antiAliasing)
Constructs a scene consisting of a root, with a dimension of width and height, specifies whether a depth buffer is created for this scene and specifies whether scene anti-aliasing is requested.

Scene(Parent root, double width, double height, Paint fill)
Creates a Scene for a specific root Node with a specific size and fill.

Scene(Parent root, Paint fill)
Creates a Scene for a specific root Node with a fill.

, , .

SceneBuilder, , , . , , , , .

setWidth()/setHeight() Stage.

+2

, Scene .

Setting the size Stagemeans setting the window size, which includes the size of the decoration. Thus, the size is Scenesmaller if Stagenot decorated.

My solution is to calculate the size of the decoration during initialization and add it to the size Stagewhen resizing:

private Stage stage;
private double decorationWidth;
private double decorationHeight;

public void start(Stage stage) throws Exception {
    this.stage = stage;

    final double initialSceneWidth = 720;
    final double initialSceneHeight = 640;
    final Parent root = createRoot();
    final Scene scene = new Scene(root, initialSceneWidth, initialSceneHeight);

    this.stage.setScene(scene);
    this.stage.show();

    this.decorationWidth = initialSceneWidth - scene.getWidth();
    this.decorationHeight = initialSceneHeight - scene.getHeight();
}

public void resizeScene(double width, double height) {
    this.stage.setWidth(width + this.decorationWidth);
    this.stage.setHeight(height + this.decorationHeight);
}
+1
source

All Articles