如何使VBox填充其父级的大小

cdc*_*cdc 11 javafx-2

这是使VBox填充其父级的正确方法:

    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();
Run Code Online (Sandbox Code Playgroud)

ndr*_*drw 22

您可以在不使用bind的情况下实现相同的功能,仅使用BorderPane而不是Group

final BorderPane root = new BorderPane();
// construct your VBox
root.setCenter(vbox);   
Run Code Online (Sandbox Code Playgroud)