2

嗨,我目前正在使用 JavaFX 中的 GridPane 并偶然发现了一个问题...我想创建一个包含三行的布局,其中中间的行会增长并占用所有可用空间,但我就是无法让它工作. 中间行变得太大,并将窗口下方的底行“推”到不再可见的位置......我如何使底行始终位于底部并让中间行占用可用空间介于两者之间,但没有更多...我将在下面粘贴我的代码。

谢谢!

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

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);

更新!添加“最小、完整且可验证的示例”

测试.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);
    }
}

测试窗格.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);
    }
}
4

1 回答 1

1

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

无需设置百分比高度,只需vgrow像在中一样设置属性TestPane

RowConstraints menuRow = new RowConstraints(25);

RowConstraints regRow = new RowConstraints();
//  regRow.setPercentHeight(100);
regRow.setVgrow(Priority.ALWAYS);
于 2016-03-28T17:24:30.680 回答