动画闪烁问题

时间:2017-05-12 12:11:46

标签: java javafx

当我运行以下代码时,我发现在setOnfinished()中的动画运行之前,按钮对象会突然闪烁,我无法弄清楚原因。(因为我无法制作一个gif到显示结果,所以也许你需要在你的电脑上运行。请提前感谢!)

public void start(Stage primaryStage){
        Pane pane=new Pane();
        Button bt=new Button("");
        bt.setPrefSize(40, 40);
        bt.setLayoutX(0);
        bt.setLayoutY(0);

        Line line1=new Line(20,20,20,100);
        PathTransition pt1=new PathTransition();
        pt1.setPath(line1);
        pt1.setDuration(Duration.seconds(2));

        Line line2=new Line(20,100,100,100);
        PathTransition pt2=new PathTransition();
        pt2.setPath(line2);
        pt2.setDuration(Duration.seconds(2));

        SequentialTransition st=new SequentialTransition(bt,pt1,pt2);
        st.play();
        st.setOnFinished(e->{
            bt.setLayoutX(80);
            bt.setLayoutY(80);
            System.out.println("X coordinate"+bt.getLayoutX());
            System.out.println("Y coordinate"+bt.getLayoutY());
            Line line3=new Line(20,20,20,90);
            PathTransition pt3=new PathTransition();
            pt3.setPath(line3);
            pt3.setNode(bt);
            pt3.setDuration(Duration.seconds(2));
            pt3.play();
            primaryStage.show();

        });

        pane.getChildren().add(bt);
        Scene scene=new Scene(pane,250,250);
        primaryStage.setScene(scene);
        primaryStage.show();

        System.out.println("X coordinate"+bt.getLayoutX());
        System.out.println("Y coordinate"+bt.getLayoutY());
    }

    public static void main(String[] args){
        Application.launch(args);
    }

1 个答案:

答案 0 :(得分:0)

就像Thomas说的那样,你在onFinish方法中设置了错误的坐标。你应该在按钮的最后位置拿起并从那里移动。

    Pane pane=new Pane();
    Button bt=new Button("");
    bt.setPrefSize(40, 40);
    bt.setLayoutX(0);
    bt.setLayoutY(0);

    Line line1=new Line(20,20,20,100);
    PathTransition pt1=new PathTransition();
    pt1.setPath(line1);
    pt1.setDuration(Duration.seconds(2));

    Line line2=new Line(20,100,100,100);
    PathTransition pt2=new PathTransition();
    pt2.setPath(line2);
    pt2.setDuration(Duration.seconds(2));

    SequentialTransition st=new SequentialTransition(bt,pt1,pt2);
    st.play();
    st.setOnFinished(e->{
        System.out.println("X coordinate"+bt.getLayoutX());
        System.out.println("Y coordinate"+bt.getLayoutY());
        Line line3=new Line(100,100,100, 190);
        PathTransition pt3=new PathTransition();
        pt3.setPath(line3);
        pt3.setNode(bt);
        pt3.setDuration(Duration.seconds(2));
        pt3.play();
        primaryStage.show();

    });

    pane.getChildren().add(bt);
    Scene scene=new Scene(pane,250,250);
    primaryStage.setScene(scene);
    primaryStage.show();

    System.out.println("X coordinate"+bt.getLayoutX());
    System.out.println("Y coordinate"+bt.getLayoutY());