我正在尝试创建一张稍后可以打印的发票。我正在使用 VBoxes 和 GridPanes 来做到这一点。中间 GridPane 的列数是可变的。我正在使用循环构建它。我尝试了许多不同的方法来使 GridPane(绿色边框)跨越整个 VBox(红色边框),但似乎没有任何效果。
设置边框后,我意识到网格实际上跨越了整个 VBox,但列并没有占据整个空间。我无法给出列限制,因为我不知道中间网格的列数,而且我必须保持在 500 像素以下。
相关代码如下。
final int width = 500;
BorderPane borderpane = new BorderPane();
VBox vboxTop = vboxForPrinting('l');
VBox vboxMiddle = vboxForPrinting('r');
VBox vboxBottom = vboxForPrinting('r');
GridPane gridpaneTop = new GridPane();
GridPane gridpaneMiddle = new GridPane();
GridPane gridpaneBottom = new GridPane();
borderpane.setPrefWidth(width);
vboxTop.setFillWidth(true);
vboxMiddle.setFillWidth(true);
vboxBottom.setFillWidth(true);
// Some code
vboxTop.getChildren().add(gridpaneTop);
vboxMiddle.getChildren().add(gridpaneMiddle);
vboxBottom.getChildren().add(gridpaneBottom);
borderpane.setTop(vboxTop);
borderpane.setCenter(vboxMiddle);
borderpane.setBottom(vboxBottom);
函数 vboxForPrinting() 如下:
public VBox vboxForPrinting(char align) {
VBox vbox = new VBox();
switch (align) {
case 'r':
case 'R':
vbox.setAlignment(Pos.CENTER_RIGHT);
break;
case 'l':
case 'L':
vbox.setAlignment(Pos.CENTER_LEFT);
break;
default:
vbox.setAlignment(Pos.CENTER);
}
vbox.setPadding(new Insets(5));
vbox.setSpacing(5);
vbox.setFillWidth(true);
return vbox;
}
最佳答案
可以申请setHgrow()
对于网格 Pane 的特定子项,它将实习增长并占用剩余的可用空间。
来自 javadocs:
Sets the horizontal grow priority for the child when contained by a gridpane. If set, the gridpane will use the priority to allocate the child additional horizontal space if the gridpane is resized larger than it's preferred width. Setting the value to null will remove the constraint.
GridPane.setHgrow(name, Priority.ALWAYS);
其中,name
是一个标签,您希望它增长并占据剩余的空白位置。
如果您希望所有字段或列都增长,您可以将此应用到所有字段或列,或者您可以创建一个 ColumnConstraints并将它们应用于一个/多个列。
下面的代码应该可以工作:
ColumnConstraints columnConstraints = new ColumnConstraints();
columnConstraints.setFillWidth(true);
columnConstraints.setHgrow(Priority.ALWAYS);
gridPane.getColumnConstraints().add(columnConstraints);
来到你的关心,
I can't give column constraints since I'm not aware of the number of columns of the middle Grid and I have to keep in under 500px.
好吧,您不需要跟踪列数,hgrow 只会在有额外可用空间时才允许子项增长。
关于java - GridPane 不跨越整个 VBox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31647593/