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

时间:2015-07-20 11:49:58

标签: javafx javafx-8

我的JavaFX应用程序的概念:

Concept

一个loginScreen中的所有屏幕。

例如,如果用户点击按钮"选项"在ViewPort我希望为optionsScreen的Y值设置动画,将其移至viewport

如何以编程方式将ScrollPane的function httpResponse(request, response) { fs.readFile('views/main.ejs', 'utf-8', function readFile(error, file) { if (error) { response.writeHead(200, {"Content-Type": "text/plain"}); response.write('EJS ERROR'); response.end(); } else { // get domains data from db needed for main page dbHandle.queryDB({}, "domains", function dbQueryDB(error, result) { var ejsData = {name: "Cav"}; if (error) { response.write('DB ERROR'); response.end(); } else { ejsData.domains = result; // get machine type data from db needed for main page dbHandle.queryDB({}, "type", function dbQueryDB(error, result) { if (error) { response.write('DB ERROR'); response.end(); } else { ejsData.type = result; //respond index.html response.writeHead(200, {"Content-Type": "text/html"}); response.end(ejs.render(file, ejsData)); } }); } }); } }); } 移动到平滑动画?

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

1 个答案:

答案 0 :(得分:5)

  

如何以编程方式使用平滑动画移动ScrollPane的视口?

您可以使用TimelinevvalueProperty of ScrollPane的组合来执行平滑的滚动动画。

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

  • 中心

我正在按钮的操作上通过vvalue更改ScrollPane的Timeline

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

KeyValuescrollRoot.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);
    }
}

<强>输出

enter image description here