在窗格中填充宽度

Ver*_*tex 8 java layout javafx

有一个Pane根和一个GridPane孩子.根调整阶段大小.我怎样才能做到这一点的GridPane自动与根填补宽度调整?应该有任何宽度.没有财产绑定可能吗?

情况就是这样:

  1. Pane (黄色)是的根Scene,使其充满的宽度和高度Stage.
  2. 一个GridPane(绿色)有一行和三个列作为root的子.
  3. 该行应具有固定的高度.
  4. 第一列和第三列应具有恒定大小
  5. 中间列应该增长到最大
  6. GridPane应填写其父的宽度

不幸的GridPane是,没有填充宽度: GridPane应填充其父级的宽度

码:

@Override
public void start(Stage primaryStage) throws Exception {
    // Resizes with the stage
    final Pane root = new Pane();
    root.setStyle("-fx-background-color: yellow");

    // grid: 1 row, 3 colums
    // should have a constant height and should grow in width to fill parent pane
    final GridPane gridPane = new GridPane();
    root.getChildren().add(gridPane);
    gridPane.setStyle("-fx-background-color: green");
    gridPane.setGridLinesVisible(true);

    final RowConstraints row = new RowConstraints(100); // constant height = 100
    final ColumnConstraints col1 = new ColumnConstraints(100, Control.USE_COMPUTED_SIZE, Control.USE_COMPUTED_SIZE);
    // should grow as much as possible in width
    final ColumnConstraints col2 = new ColumnConstraints(200, Control.USE_COMPUTED_SIZE, Double.MAX_VALUE);
    final ColumnConstraints col3 = new ColumnConstraints(100, Control.USE_COMPUTED_SIZE, Control.USE_COMPUTED_SIZE);
    gridPane.getRowConstraints().add(row);
    gridPane.getColumnConstraints().addAll(col1, col2, col3);

    final Scene scene = new Scene(root);
    primaryStage.setScene(scene);
    primaryStage.setWidth(600);
    primaryStage.setHeight(400);
    primaryStage.show();
}
Run Code Online (Sandbox Code Playgroud)

Jam*_*s_D 13

窗格内节点的大小由单个窗格实现其布局的方式控制:即节点的大小由其父节点有效确定.因此,网格窗格的总体大小由布局实现控制Pane.

Pane实际上做了最小的布局工作:它有效地将所有内容定位在(0,0)并将其调整为其首选大小.因此,您的网格窗格将获得其首选宽度,即列的首选宽度的总和.

因此,要使网格窗格增长,您需要更改其首选宽度,或使用允许您控制其增长方式的布局窗格.

例如:

gridPane.prefWidthProperty().bind(root.widthProperty());
Run Code Online (Sandbox Code Playgroud)

将使网格窗格增长到根的宽度.这只会在所有三列的右侧添加额外的空间,因此另外设置

col2.setHgrow(Priority.ALWAYS);
Run Code Online (Sandbox Code Playgroud)

会有col2额外的宽度.

或者,使用AnchorPanefor root:

// Pane root = new Pane();
AnchorPane root = new AnchorPane();
Run Code Online (Sandbox Code Playgroud)

和设置

AnchorPane.setLeftAnchor(gridPane, 0.0);
AnchorPane.setRightAnchor(gridPane, 0.0);
Run Code Online (Sandbox Code Playgroud)

(再次与col2 hgrow集合)将具有相同的效果.

或者你可以做到

VBox root = new VBox();
root.setFillWidth(true);
Run Code Online (Sandbox Code Playgroud)

(例如:有很多解决方案).

解决方案的选择取决于您在根窗格中具有的其他节点(如果有).