如何使 JavaFX ScrollPane 自动缓慢滚动到底部?

How to make JavaFX ScrollPane autoscroll slowly to the bottom?

我有一个 Scrollpane,其内容足够大,可以激活垂直滚动条。 Scrollpanefx:idmyScrollPane。 我还有一个叫 Scroll To The Bottombutton。 我已经在 fxml 控制器中设置了 Scroll To The Bottom buttonAction Event

@FXML
private voide myButtonOnAction(ActionEvent evt) {
    myScrollPane.setVvalue(1.0);
}

然而,这个滚动到底部非常 fast.It 也无法判断它是否被滚动。我想知道一种让 Scrollpane 缓慢滚动的方法。

为了快速起见,我将使用一个 Timer 并将值逐渐递增到 1。您可以参考 this 线程以了解如何在 Java

中使用 Timer

不要为此使用计时器,除非您准备在对 Platform.runLater 的调用中包装滚动条值的每个更新。

正确的做法是使用Timeline动画:

static void slowScrollToBottom(ScrollPane scrollPane) {
    Animation animation = new Timeline(
        new KeyFrame(Duration.seconds(2),
            new KeyValue(scrollPane.vvalueProperty(), 1)));
    animation.play();
}
@FXML
private void myButtonOnAction(ActionEvent evt) {
    double vVal = myScrollPane.getVvalue();
    double d = (1.0-vVal)/100;
    Timer timer = new Timer();
    timer.schedule(new TimerTask() {
        boolean isTheStart = true;
        double difference;
        @Override
        public void run() {
            double currentVVal = myScrollPane.getVvalue();
            if (isTheStart) {
                isTheStart = false;
                difference = (1.0 - currentVVal)/100;
            }
            myScrollPane.setVvalue(currentVVal+difference);
            if (currentVVal >= 1.0) {
                this.cancel();
            }
        }
    }, 1*500, 10);
}