JavaFXのタイムラインが終了した後の値を返します。

user3776738:

私はのカウントダウンを行うためのJavaFXでタイムラインを使用していますLabel

timeline.setCycleCount(6);
timeline.play();

そして私は、タイムラインが終了した後の値を返すようにしたいです:

return true;

しかし、価値がすぐに返さなっているようですし、タイムラインが平行に走ります。タイムラインは、そのカウントダウンが終了するまで、どのように私は待つことができ、その後、タイムラインをブロックせずに値を返しますか?

EDIT:

それをより明確にするために、私はすでに試しました:

new Thread(() -> {
    timeline.play();
}).start();

while(!finished){ // finished is set to true, when the countdown is <=0

}
return true;

(このソリューションは、カウントダウンを更新しません。)

EDIT 2:

ここでは、最小限の完全かつ検証可能な例は次のとおりです。

import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.util.Duration;


public class CountdownTest extends Application {

    private Label CountdownLabel;
    private int Ctime;

    @Override
    public void start(Stage primaryStage) {

        CountdownLabel=new Label(Ctime+"");

        StackPane root = new StackPane();
        root.getChildren().add(CountdownLabel);

        Scene scene = new Scene(root, 300, 250);

        primaryStage.setTitle("Countdown Test");
        primaryStage.setScene(scene);
        primaryStage.show();

        Ctime=5;

        if(myCountdown()){
            CountdownLabel.setText("COUNTDOWN FINISHED");
        }
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

    public boolean myCountdown(){
         final Timeline timeline = new Timeline(
                            new KeyFrame(
                                    Duration.millis(1000),
                                    event -> {
                                    CountdownLabel.setText(Ctime+"");
                                    Ctime--;

                                    }

                            )
                    );
         timeline.setCycleCount(6);
         timeline.play();
    return true;
    }

}

あなたはそれが最初のショー「COUNTDOWN FINISHED」とカウント0までの代わりに「FINISHED COUNTDOWN」までのカウントダウンとカウントで始まることを見ることができます。

FThompson:

通りTimelineからの継承Animationは、使用することができますsetOnFinishedタイムラインの終わりに発生するアクションを定義します。

timeline.setCycleCount(6);
timeline.play();
timeline.setOnFinished(event -> countdownLabel.setText("COUNTDOWN FINISHED"));

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=178778&siteId=1