2017-05-12 217 views
0

當我運行以下代碼時,我發現在setOnfinished()中的動畫運行之前,按鈕對象會突然閃爍,我找不到原因(因爲我無法制作一個GIF顯示結果,所以也許你需要打擾到您的PC上運行。在此先感謝!)動畫閃爍問題

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); 
    } 
+0

我不是動畫專家,但手動設置座標似乎很奇怪。 – Thomas

+0

我也懷疑它是設置座標導致的問題,但我不知道爲什麼......〜_〜 – zhangzhan

+1

嗯,我會假設按鈕將有不同的座標,因爲動畫,所以當你改變它們時,你讓該按鈕出現在錯誤的地方至少一幀(這將解釋閃爍)。 – Thomas

回答

0

像托馬斯說,你在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());