【问题标题】:JavaFX stop TimelineJavaFX 停止时间轴
【发布时间】:2013-01-02 23:42:00
【问题描述】:

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

谢谢!

FXML:

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

gui控制器:

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

   }

【问题讨论】:

  • timeline.stop() - 我的猜测是您已经知道这一点,但需要在您的问题中提供更多信息(例如简短的可执行示例代码和描述),以说明您的真正问题是什么,以便有人可以提供更多帮助。
  • 我试过了。我使用 FXML,所以有一个 guiControll,它处理例如按钮操作。并且有一个创建时间线的主类。而且我无法从 guiController 启动/停止/重新启动动画。如果时间轴已启动(无限时间),我无法通过按钮停止它。
  • 请在您的问题中包含您的 FXML、控制器类和应用程序类的可执行代码,以便可以复制问题。谢谢。
  • 我已经复制了相关部分的代码

标签: animation javafx timeline


【解决方案1】:

您需要在控制器中引用在应用程序的 start 方法中创建的原始动画。这将允许您在控制器中编写按钮事件处理程序以停止动画。

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,这类问题更容易快速正确地回答。

【讨论】:

  • 非常感谢!下次我会照你说的做。但是在 guiControll 有一条错误消息,带有: public setAnimation(Timeline animation) { this.animation = animation;它说无效的方法声明。需要返回类型。
  • 是的,我错过了返回类型,现在已在答案中修复 - 我也应该编译我的答案代码 ;-)
  • 太棒了! :) 非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多