2013-01-02 61 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