目标是在调整窗口大小时调整GridPane中特定区域(仅与左上角区域相关)的大小。相关区域应始终适合窗口大小。调整大小机制工作正常,但GridPane看起来要大得多,这使得重要区域变小。
有没有办法切断GridPane中未使用的单元格?
或者有更好的解决方案吗?
以下问题的代码和屏幕截图:
for (int row = 0; row <= characters.length - 1; row++) {
for (int column = 0; column <= characters[0].length - 1; column++) {
ClockElement n = new ClockElement(characters[row][column], false);
grid.add(n.getLabel(), column, row);
GridPane.setHalignment(n.getLabel(), HPos.CENTER);
ColumnConstraints c = new ColumnConstraints();
c.setHgrow(Priority.ALWAYS);
//c.setMinWidth(40);
c.setPercentWidth(50); //old 40
grid.getColumnConstraints().add(c);
RowConstraints r = new RowConstraints();
r.setVgrow(Priority.ALWAYS);
//r.setMinHeight(40);
r.setPercentHeight(50);
grid.getRowConstraints().add(r);
arrClockElement[row][column] = n;
}
}
grid.setPadding(new Insets(0, 0, 0, 0));
grid.setPrefSize(580, 680); // Default width and height
grid.setMaxSize(Region.USE_COMPUTED_SIZE, Region.USE_COMPUTED_SIZE);
grid.setGridLinesVisible(true);
答案 0 :(得分:1)
您添加的ColumnConstraint
和RowConstraint
太多了。您需要characters.length
(行数)RowConstraints
和characters[0].length
(列数)ColumnConstraints
。相反,您要在内部循环中添加两者,因此您要添加每个(characters.length)*(characters[0].length)
。
将RowConstraints
创建移到内部循环之外,并创建一个新循环以添加ColumnConstraints
:
for (int row = 0; row <= characters.length - 1; row++) {
RowConstraints r = new RowConstraints();
r.setVgrow(Priority.ALWAYS);
//r.setMinHeight(40);
r.setPercentHeight(50);
grid.getRowConstraints().add(r);
for (int column = 0; column <= characters[0].length - 1; column++) {
ClockElement n = new ClockElement(characters[row][column], false);
grid.add(n.getLabel(), column, row);
GridPane.setHalignment(n.getLabel(), HPos.CENTER);
arrClockElement[row][column] = n;
}
}
for (int column = 0 ; column < characters[0].length; column++) {
ColumnConstraints c = new ColumnConstraints();
c.setHgrow(Priority.ALWAYS);
//c.setMinWidth(40);
c.setPercentWidth(50); //old 40
grid.getColumnConstraints().add(c);
}