animation - JavaFX 停止时间轴

标签 animation javafx timeline

我使用 FXML。我创建了一个按钮来停止/重新启动实时图表。对于动画,我使用了时间轴。我想从 guiController(来自其他类)控制它,但它不起作用。如何从其他类(class)中停止时间轴?

谢谢!

文件格式:

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

gui Controller :
@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();
       } 

   }

最佳答案

您需要的是您的 Controller 中对在您的应用程序的 start 方法中创建的原始动画的引用。这将允许您在 Controller 中编写按钮事件处理程序以停止动画。
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,这些类型的问题更容易快速正确地回答。 .

关于animation - JavaFX 停止时间轴,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14131012/

相关文章:

javascript - d3js 刻度定位

javascript - d3.js : time scale and data : gape of some pixel between tick and data

objective-c - 动画的 UIView subview

reactjs - 如何为 SVG 圆圈内的图像添加边框

javascript - 在不重新绘制 map 的情况下重新放置多个 Google map 标记

没有窗口的javafx图形

java - TableView JavaFX 中的上下文菜单可见性

JavaFX 标签未使用 setText() 更新

html - 纯 css 中的可折叠文本高度

javascript - 简单的浏览器内时间轴