How to make VBox fill the size of its parent

2019-04-21 00:24发布

问题:

Is this the correct way to make VBox fill its parent:

    final Group root = new Group();
    final Scene scene = new Scene(root, 1000, 800, Color.BLACK);

    final VBox c = new VBox();
    c.setAlignment(Pos.TOP_CENTER);
    c.setSpacing(10); 
    c.setFillWidth(true);
    c.getChildren().add(...);
    c.getChildren().add(...);
    c.getChildren().add(...);

    c.prefWidthProperty().bind(scene.widthProperty());
    c.prefHeightProperty().bind(scene.heightProperty());

    root.getChildren().add(c);

    stage.setTitle("blah"); //$NON-NLS-1$
    stage.setScene(scene);
    stage.show();

回答1:

You can achieve the same functionality without using bind, using only BorderPane instead of Group

final BorderPane root = new BorderPane();
// construct your VBox
root.setCenter(vbox);   


回答2:

You are making a common mistake.

It's not necessary to create a Group as the root, just directly use VBox as the root and add it to the scene.

final Scene scene = new Scene(c, 1000, 800, Color.BLACK);


标签: javafx-2