[英]JavaFX FlowPane within ScrollPane, dynamically adjust scrollpane's content and size
[英]How to dynamically add gap to flowpane embedded in scrollpane if user resize scrollpane
如果用戶調整ScrollPane的大小,如何為嵌入ScrollPane的FlowPane動態添加間隙?
我有一個帶有GridPane的窗口。 在此GridPane中,我有一個ScrollPane。 在此ScrollPane中,我有一個VBox。
我在FlowPane中添加了一個圓形數組,並將此FlowPane添加到了VBox中。
我想根據窗口或ScrollPane的大小動態設置圓之間的間隔。
目前,我將間隙設置為5,但是如果由於FlowPane而將窗口的大小調整得很大,則內容將保留在左側。 我希望內容占據所有水平空間。
我嘗試使用綁定和changeListener,但沒有找到解決方案。
對於恆定數量的列和相等大小的GridPane
的特殊情況,我建議使用帶有適當ColumnConstraints
的GridPane
。
除了指定列中節點的對齊方式之外,這還可以確保列寬相同:
@Override
public void start(Stage primaryStage) throws Exception {
final int columns = 2;
ColumnConstraints columnConstraints = new ColumnConstraints();
columnConstraints.setPercentWidth(100d/columns);
columnConstraints.setHalignment(HPos.CENTER);
GridPane grid = new GridPane();
for (int i = 0; i < columns; i++) {
grid.getColumnConstraints().add(columnConstraints);
}
for (int i = 0; i < 100; i++) {
grid.add(new Circle(50), i % columns, i / columns);
}
ScrollPane scrollPane = new ScrollPane(new VBox(grid));
scrollPane.setFitToWidth(true); // make sure GridPane content resizes with viewport
Scene scene = new Scene(scrollPane, 300, 300);
primaryStage.setScene(scene);
primaryStage.show();
}
修改GridPane
的子級列表變得更加復雜,因為行/列索引需要調整/設置,但這並不難。 只需確保索引i
處子級的列索引為i % columns
,行索引i / columns
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.