JavaFX - Gridpane 增长问题

JavaFX - Gridpane Growing Issue

您好,我目前正在使用 JavaFX 中的 GridPane 并偶然发现了一个问题...我想创建一个包含三行的布局,其中中间一行增长并占用所有可用 space 但是我就是无法让它工作。中间的行变得太大,"pushes" window 下方的底行到了不再可见的地步...我该怎么做才能使底行始终位于底部并让中间的行行占据中间可用的space,但不再......我将在下面粘贴我的代码。

谢谢!

(注意:为清楚起见,代码略有改动,但工作方式相同)

RowConstraints row1 = new RowConstraints(25);
row1.setVgrow(Priority.NEVER);

RowConstraints row2 = new RowConstraints();
row2.setVgrow(Priority.ALWAYS);

RowConstraints row3 = new RowConstraints(25);
row3.setVgrow(Priority.NEVER);

ColumnConstraints column1 = new ColumnConstraints();
column1.setPercentWidth(100);

gridPane.getRowConstraints().add(0, row1);
gridPane.getRowConstraints().add(1, row2);
gridPane.getRowConstraints().add(2, row3);

gridPane.getColumnConstraints().add(0, column1);

gridPane.add(node1, 0, 0);
gridPane.add(node2, 0, 1);
gridPane.add(node3, 0, 2);

更新!添加一个 "Minimal, Complete, and Verifiable example"

Test.java

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.MenuBar;
import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.RowConstraints;
import javafx.stage.Stage;

public class Test extends Application {

    @Override
    public void start(Stage primaryStage) {
        GridPane grid = new GridPane();

        MenuBar menuBar = new MenuBar();
        TestPane pane = new TestPane();

        RowConstraints menuRow = new RowConstraints(25);

        RowConstraints regRow = new RowConstraints();
        regRow.setPercentHeight(100);

        ColumnConstraints regColumn = new ColumnConstraints();
        regColumn.setPercentWidth(100);

        grid.getColumnConstraints().addAll(regColumn);
        grid.getRowConstraints().addAll(menuRow, regRow);

        grid.add(menuBar, 0, 0);
        grid.add(pane, 0, 1);

        Scene scene = new Scene(grid, 500, 350);

        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

TestPane.java

import javafx.scene.control.ListView;
import javafx.scene.control.TextField;
import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Priority;
import javafx.scene.layout.RowConstraints;

public class TestPane extends GridPane {

    public TestPane() {
        RowConstraints fixedRow = new RowConstraints(25);
        fixedRow.setVgrow(Priority.NEVER);

        RowConstraints growingRow = new RowConstraints();
        growingRow.setVgrow(Priority.ALWAYS);

        ColumnConstraints column = new ColumnConstraints();
        column.setPercentWidth(100);

        this.getRowConstraints().addAll(fixedRow, growingRow, fixedRow);
        this.getColumnConstraints().addAll(column);

        TextField field1 = new TextField();
        ListView list = new ListView<>();
        TextField field2 = new TextField();

        this.add(field1, 0, 0);
        this.add(list, 0, 1);
        this.add(field2, 0, 2);
    }
}

在您的 "outer" 容器中,即 Test 中定义的网格窗格,您有两行。第一行固定为 25 像素,而您将第二行设置为容器高度的 100%。这些显然是矛盾的。 (我认为)发生的是第二行的内容被分配了与网格窗格本身相同的高度(因为您将 percentHeight 设置为 100%),但它被剪裁了,因为没有足够的空间显示全部。

不要设置百分比高度,只需像在 TestPane 中那样设置 vgrow 属性:

RowConstraints menuRow = new RowConstraints(25);

RowConstraints regRow = new RowConstraints();
//  regRow.setPercentHeight(100);
regRow.setVgrow(Priority.ALWAYS);