在按钮单击上移动ScrollPane的视点

Ale*_*ndr 2 javafx javafx-8

有我的JavaFX应用程序的概念:

概念

所有屏幕合二为一ScrollPane.

例如,如果用户点击"选项"按钮,loginScreen我想要设置Y值的动画ViewPort以将其移动到optionsScreen.

如何viewport以流畅的动画以编程方式移动ScrollPane?

或者你可以提供更好的主意?

Ita*_*iha 6

如何以编程方式移动ScrollPane的视口并使用流畅的动画?

您可以使用ScrollPaneTimelinevvalueProperty的组合来执行平滑的滚动动画.

这是一个简单的应用程序,我有三个部分

  • 最佳
  • 中央
  • 底部

我正在vvalue通过Timeline按钮的动作更改ScrollPane .

vvalue的值可以介于0.0到1.0之间,因此您可能需要进行自己的计算才能找到要分配给它的确切值.

键值对所述操作scrollRoot.vvalueProperty().

完整时间轴的KeyFrame设置为500 milliseconds.您可以根据想要运行动画的时间来增加减少它.

import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import javafx.util.Duration;

public class Main extends Application {

    // For just adjusting the center rectangle w.r.t ScrollPane
    private final int ADJUSTMENT_RATIO = 175;

    @Override
    public void start(Stage stage) {
        Rectangle topRegion = new Rectangle(300, 300, Color.ALICEBLUE);
        StackPane top = new StackPane(topRegion, new Label("Top"));

        Rectangle centerRegion = new Rectangle(300, 300, Color.GOLDENROD);
        StackPane center = new StackPane(centerRegion, new Label("center"));

        Rectangle bottomRegion = new Rectangle(300, 300, Color.BISQUE);
        StackPane bottom = new StackPane(bottomRegion, new Label("bottom"));

        Button topButton = new Button("Top");
        Button centerButton = new Button("Center");
        Button bottomButton = new Button("Bottom");

        HBox buttonBox = new HBox(15, topButton, centerButton, bottomButton);
        buttonBox.setPadding(new Insets(20));
        buttonBox.setAlignment(Pos.CENTER);

        final VBox root = new VBox();
        root.setSpacing(10);
        root.setPadding(new Insets(10, 10, 10, 10));
        root.getChildren().addAll(top, center, bottom);

        ScrollPane scrollRoot = new ScrollPane(root);
        Scene scene = new Scene(new VBox(buttonBox, scrollRoot));
        stage.setTitle("Market");
        stage.setWidth(350);
        stage.setHeight(400);

        stage.setScene(scene);
        stage.show();

        topButton.setOnAction(event -> {
            final Timeline timeline = new Timeline();
            final KeyValue kv = new KeyValue(scrollRoot.vvalueProperty(), 0.0);
            final KeyFrame kf = new KeyFrame(Duration.millis(500), kv);
            timeline.getKeyFrames().add(kf);
            timeline.play();
        });

        centerButton.setOnAction(event -> {
            final Timeline timeline = new Timeline();
            final KeyValue kv = new KeyValue(scrollRoot.vvalueProperty(), (top.getBoundsInLocal().getHeight() + ADJUSTMENT_RATIO) / root.getHeight());
            final KeyFrame kf = new KeyFrame(Duration.millis(500), kv);
            timeline.getKeyFrames().add(kf);
            timeline.play();
        });

        bottomButton.setOnAction(event -> {
            final Timeline timeline = new Timeline();
            final KeyValue kv = new KeyValue(scrollRoot.vvalueProperty(), 1.0);
            final KeyFrame kf = new KeyFrame(Duration.millis(500), kv);
            timeline.getKeyFrames().add(kf);
            timeline.play();
        });
    }

    public static void main(String[] args) {
        launch(args);
    }
}
Run Code Online (Sandbox Code Playgroud)

产量

在此输入图像描述