4

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

   }
4

1 に答える 1

4

必要なのは、アプリケーションの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はアプリケーションクラスです。必要なインスタンスは1つだけです。そのインスタンスはJavaFXランチャーによって作成されます。絶対に実行しないでくださいnew MotionCFp()

質問のコードが単純で、完全で、コンパイル可能で、実行可能である場合、これらの種類の質問にすばやく正確に答えるのははるかに簡単であることに注意してください。

于 2013-01-03T08:04:43.460 に答える