猿问

ScrollPane javafx 自动滚动(将 vvalue 设置为 1.0)

所以,情况是......我在滚动窗格中有一个 vbox。我将 hbox 添加到 vbox 中,然后vbox.setVvalue(1.0)在每次插入后调用。

然而,假设有 5 个 hbox,滚动条只会使最后一个可见项目是第 4 个 hbox - 在当前看到的下方有一个 hbox(需要向下滚动才能看到)。

我找到了一个解决方案,它将滚动窗格的 vvalue 属性绑定到 vbox 的 height 属性,如下所示:scrollPane.vvalueProperty().bind(vbox.heightProperty())我假设每次更改 vbox 高度时(即添加新的 hbox 时)都会将 vvalue 更改为最大值。

但是,我仍然想提高我的知识以及为什么第一个(在每次插入后设置滚动窗格的 vvalue)与绑定属性不同。谢谢!


万千封印
浏览 492回答 1
1回答

德玛西亚99

设置 newvvalue发生在由修改引起的布局传递之前VBox,但在布局传递之前应用的结果。由于视口中显示的顶部 y 坐标的公式为top = max(0, vvalue * (contentHeight - viewportHeight))并且在布局过程中,内容的左上角保持原位,您会在视口底部看到旧内容的底部。要解决此问题,您可以在ScrollPane使用时手动触发布局传递scrollPane.applyCss();scrollPane.layout();例子@Overridepublic void start(Stage primaryStage) {&nbsp; &nbsp; VBox content = new VBox();&nbsp; &nbsp; ScrollPane scrollPane = new ScrollPane(content);&nbsp; &nbsp; VBox.setVgrow(scrollPane, Priority.ALWAYS);&nbsp; &nbsp; Button button = new Button("fill");&nbsp; &nbsp; button.setOnAction(evt -> {&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < 20; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; content.getChildren().add(new Text(Integer.toString(i)));&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("content size before layout: " + content.getHeight());&nbsp; &nbsp; &nbsp; &nbsp; // manually layout scrollPane&nbsp; &nbsp; &nbsp; &nbsp; scrollPane.applyCss();&nbsp; &nbsp; &nbsp; &nbsp; scrollPane.layout();&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("content size after layout: " + content.getHeight());&nbsp; &nbsp; &nbsp; &nbsp; scrollPane.setVvalue(1d);&nbsp; &nbsp; });&nbsp; &nbsp; Scene scene = new Scene(new VBox(button, scrollPane), 200, 200);&nbsp; &nbsp; primaryStage.setScene(scene);&nbsp; &nbsp; primaryStage.show();}
随时随地看视频慕课网APP

相关分类

Java
我要回答