2013-01-02 62 views
2

我使用FXML。我創建了一個按鈕來停止/重新啓動實時圖表。對於我使用時間軸的動畫。我想要從guiController(來自其他類)控制它,但它不起作用。我怎樣才能阻止其他課程的時間線?JavaFX停止時間線

謝謝!

FXML:

  <Button id="button" layoutX="691.0" layoutY="305.0" mnemonicParsing="false" onAction="#btn_startmes" prefHeight="34.0" prefWidth="115.0" text="%start" /> 

guiController:

@FXML  
private void btn_stopmes(ActionEvent event) { 
    MotionCFp Stopping = new MotionCFp(); 
Stopping.animation.stop(); 
} 

MotionCFp.java:

@Override 
    public void start(final Stage stage) throws Exception { 
     else{ 
     ResourceBundle motionCFp = ResourceBundle.getBundle("motionc.fp.Bundle", new Locale("en", "EN")); 
     AnchorPane root = (AnchorPane) FXMLLoader.load(MotionCFp.class.getResource("gui.fxml"), motionCFp); 
     final guiController gui = new guiController(); 
     Scene scene = new Scene(root); 
     stage.setTitle(motionCFp.getString("title")); 
     stage.setResizable(false); 
     stage.setScene(scene);   
     root.getChildren().add(gui.createChart()); 
       animation = new Timeline(); 
       animation.getKeyFrames().add(new KeyFrame(Duration.millis(1000/60), new EventHandler<ActionEvent>() { 
      @Override public void handle(ActionEvent actionEvent) { 
       // 6 minutes data per frame 
       for(int count=0; count < 6; count++) { 
        gui.nextTime(); 
        gui.plotTime(); 
        animation.pause(); 
        animation.play(); 
       } 
      } 
     })); 
     animation.setCycleCount(Animation.INDEFINITE); 
     stage.show(); 
     animation.play(); 
     } 

    } 
+0

[timeline.stop()](http://docs.oracle.com/javafx/2/api/javafx/animation/Timeline.html#stop%28%29) - 我的猜測是你已經知道這個,但需要在你的問題中提供更多的信息(例如簡短的可執行示例代碼加描述),它說明了你的真正問題是什麼,以便有人可以提供更多的幫助。 – jewelsea

+0

我試過了。我使用FXML,所以有一個guiControll,它處理例如按鈕操作。還有一個創建時間軸的主類。我無法從guiController啓動/停止/重新啓動動畫。如果時間軸開始(無限期),我無法用按鈕停止它。 – Miki

+0

請在您的問題中包含FXML,控制器類和應用程序類的可執行代碼,以便可以複製問題。謝謝。 – jewelsea

回答

3

你需要的是在您的控制器在開始時創建的原創動畫參考你的應用程序的方法。這將允許您編寫控制器中的按鈕事件處理程序來停止動畫。

MotionCFp類可以包含代碼:

final FXMLLoader loader = new FXMLLoader(
    getClass().getResource("gui.fxml"), 
    ResourceBundle.getBundle("motionc.fp.Bundle", new Locale("en", "EN")) 
); 
final Pane root = (Pane) loader.load(); 
final GuiController controller = loader.<GuiController>getController(); 
... 
animation = new Timeline(); 
controller.setAnimation(animation); 

而且GuiController類可以包含的代碼:

private Timeline animation; 

public void setAnimation(Timeline animation) { 
    this.animation = animation; 
} 

@FXML private void btn_stopmes(ActionEvent event) { 
    if (animation != null) { 
    animation.stop(); 
    } 
} 

MotionCFp是你的應用程序類。你只需要它的一個實例。該實例是由JavaFX啓動程序創建的,因此您絕不應該使用new MotionCFp()

請注意,如果問題中的代碼是simple, complete, compilable and executable,這些類型的問題更容易快速準確地回答。

+0

非常感謝!我會照你下次說的去做。但在guiControll上有一個錯誤消息,其中: public setAnimation(時間軸動畫){this.animation = animation; } 它說無效的方法聲明。需要返回類型。 – Miki

+0

是的,我錯過了返回類型,現在在答案中修復 - 我應該編譯我的答案代碼;-) – jewelsea

+0

太棒了! :) 非常感謝你! – Miki