JavaFX - Gridpane不断增长的问题

时间:2016-03-28 14:27:56

标签: java layout javafx gridpane

您好我正在玩JavaFX中的GridPane并偶然发现一个问题......我想创建一个有三行的布局,其中中间的一个增长并占用所有可用空间但我无法获得它工作。中间行增长得太大,并且“推动”窗口下方的底行到不再可见的位置......如何使底行始终位于底部并让中间行占用可用空间inbetween,但不多......我会在下面粘贴我的代码。

谢谢!

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

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

UPDATE!添加“最小,完整且可验证的示例”

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

1 个答案:

答案 0 :(得分:1)

在你的&#34;外部&#34;容器,即Test中定义的网格窗格,您有两行。顶行固定为25像素,而第二行设置为容器高度的100%。这些显然是矛盾的。 (我认为)发生的是第二行的内容被赋予与网格窗格本身相同的高度(因为您将percentHeight设置为100%),但由于没有足够的空间而被剪切全部展示。

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

RowConstraints menuRow = new RowConstraints(25);

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