目标是在调整窗口大小时调整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);
您添加了太多ColumnConstraint
和RowConstraint
。您需要字符. long
(行数)RowConstraint
和字符[0].long
(列数)ColumnConstraint
。相反,您在内部循环中添加了两者,因此您添加了每个的(字符.长度)*(字符[0].长度)
。
将RowConstraint
创建移到内部循环之外,并创建一个用于添加ColumnConstraint
的新循环:
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);
}