JavaFX - 如何在单击按钮时以编程方式滚动 ScrollPane?

JavaFX - How to programmatically scroll a ScrollPane upon button click?

我想知道是否有任何 "easy" 方法可以将垂直滚动条设置为 "scrolled" 按下按钮时的特定数量,或者实际上是任何事件。

例如,我有一个有侧边栏的应用程序,侧边栏上的选项之一是“设置”。设置有子项,我想添加单击其中一个子项的功能,它将打开“设置”页面并自动向下滚动到该子项所在页面上的特定点。

查看解决方案:

Bounds bounds = pane.getViewportBounds();
pane.setVvalue(box.getChildren().get(x).getLayoutY() * 
                             (1/(box.getHeight()-bounds.getHeight())));

pane 是 ScrollPane。 box 是包含要滚动到的节点的窗格。 x 将由子节点滚动到。

要以编程方式垂直滚动 ScrollPane,只需操作 vvalue 属性。这是 属性 的文档:

The current vertical scroll position of the ScrollPane. This value may be set by the application to scroll the view programatically. The ScrollPane will update this value whenever the viewport is scrolled or panned by the user. This value must always be within the range of vmin to vmax. When vvalue equals vmin, the contained node is positioned so that its layoutBounds minY is visible. When vvalue equals vmax, the contained node is positioned so that its layoutBounds maxY is visible. When vvalue is between vmin and vmax, the contained node is positioned proportionally between layoutBounds minY and layoutBounds maxY.