嗨,我正在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);
}
}
1条答案
按热度按时间bwleehnv1#
在“外部”容器中,即中定义的网格窗格中
Test
,你有两排。顶行固定为25像素,而第二行设置为容器高度的100%。这些显然是矛盾的。我认为,第二行的内容被分配了与网格窗格本身相同的高度(因为percentHeight
设置为100%),但由于没有足够的空间来显示所有内容,因此会对其进行剪裁。与其设置高度百分比,不如设置
vgrow
就像你在这里做的那样TestPane
: