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