Java 动画闪现问题

Java 动画闪现问题,java,javafx,Java,Javafx,当我运行以下代码时,我发现在setOnfinished()中的动画运行之前,button对象会突然闪烁,我不知道原因。(因为我无法制作gif来显示结果,所以可能您需要麻烦在PC上运行。提前感谢!) 正如Thomas所说,在onFinish方法中设置了错误的坐标。您应该在按钮的最后一个位置拾取并从那里移动 Pane pane=new Pane(); Button bt=new Button(""); bt.setPrefSize(40, 40); bt.setLay

当我运行以下代码时,我发现在setOnfinished()中的动画运行之前,button对象会突然闪烁,我不知道原因。(因为我无法制作gif来显示结果,所以可能您需要麻烦在PC上运行。提前感谢!)


正如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());

我不是动画专家,但手动设置坐标似乎很奇怪。我也怀疑是设置坐标导致了问题,但是我不知道为什么…~~~好吧,我假设由于动画的原因,按钮会有不同的坐标,所以当你改变它们时,你会使按钮至少在一帧内出现在错误的位置(这可以解释闪烁)。我认为这很可能是原因。但事实上,起点坐标应该是节点中心点的坐标,终点也是。按钮的坐标被定义为左上角顶点的坐标。所以我计算了它们并设置了这些数字。我认为他们没有错。这正是我感到困惑的地方。但仍然感谢你的回答^_^
    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());