JavaFX ScrollPane 不使用 VBox 滚动

JavaFX ScrollPane not scrolling with VBox

我的屏幕上有一个滚动窗格,里面有一个垂直框。我在这个 vbox 中添加了许多复选框(我看到它在扩展)但是滚动窗格似乎不知道当内容超过该窗格的高度时它应该开始显示滚动条。我已经尝试更改滚动条策略,但它只显示一个滚动条,我实际上无法滚动。我该如何解决这个问题?

private void addCheckbox(String checkbox){
        CheckBox c = new CheckBox(checkbox);
        c.setPadding(this.paddingCheckBoxes);
        c.setSelected(true);
        this.vBoxFilters.getChildren().add(c);
    }

这是 fxml:

<StackPane>
     <children>
          <ScrollPane fx:id="scrollPaneFilters" prefHeight="878.0" prefWidth="260.0">
            <content>
              <VBox fx:id="vBoxFilters" minHeight="0.0" minWidth="0.0" prefHeight="200.0" prefWidth="200.0" />
            </content>
          </ScrollPane>
     </children>
  </StackPane>

尝试在您的 fxml 中删除 VBox 的 prefHeight

对于正在搜索相同问题的其他人,如果将 VBox 放在 ScrollPane 中的 AnchorPane 中,也会出现相同的问题。如果您使用 "Scroll Pane" 而不是 "Scroll Pane (empty)".

,这将在 SceneBuilder 中发生

首先确保您没有在主容器“VBox”上设置定义的高度,然后为了让滚动窗格工作,您必须设置一个高度。 示例:

ScrollPane scrollPane = new ScrollPane(); scrollPane.setContent(滚动组合); scrollPane.setMaxHeight(740);