Javafx 2 如何使VBox填充其父对象的大小

Javafx 2 如何使VBox填充其父对象的大小,javafx-2,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.getCh

这是使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();

您可以在不使用绑定的情况下实现相同的功能,只使用而不使用组

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

你犯了一个常见的错误

无需创建组作为根,只需直接使用VBox作为根并将其添加到场景中即可

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