JavaFX:如何在达到零时停止时间轴倒数计时器 [英] JavaFX: How to stop Timeline countdown timer when zero is reached

查看:43
本文介绍了JavaFX:如何在达到零时停止时间轴倒数计时器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这段代码显示从10开始倒数​​:

I have this code that displays count down from 10 downward:

但是,到达零后,它将继续计数为负数.达到零后如何停止?因此,要在标签上显示的最后一个文本是 0 .

However it continues counting into negative numbers after zero is reached. How do I stop it after zero is reached? So that the last text to be displayed on the label is 0.

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

public class TestCountdown extends Application {
    private GridPane gridPane;
    private Scene scene;

    private Button button = new Button("Start");
    private CountdownTimer timer = new CountdownTimer();

    @Override
    public void start(Stage applicationStage) {
        gridPane = new GridPane();
        scene = new Scene(gridPane);

        gridPane.add(timer, 0, 1);
        gridPane.add(button, 0, 2);

        timer.setStyle("-fx-font-size: 50;");
        button.setStyle("-fx-font-size: 50;");

        button.setOnAction(actionEvent -> timer.start());

        applicationStage.setScene(scene);
        applicationStage.setFullScreen(true);
        applicationStage.show();
    }
}

class CountdownTimer extends Label {
    private int i = 10;

    private boolean started;

    public CountdownTimer() {
        setText("10");
    }

    public void start() {
        if (started)  {
            return;
        }

        Timeline timeline = new Timeline(new KeyFrame(Duration.seconds(0),
                event -> {
                    setText(String.valueOf(i--));

                    if (i <= 0)  {
                        timeline.stop(); //ERROR: variable timeline might not have been initialized
                    }
                }),
                new KeyFrame(Duration.seconds(1)));

        timeline.setOnFinished(event -> System.out.println("Done!"));
        timeline.setCycleCount(Animation.INDEFINITE);
        timeline.play();
 
        started = true;
    }
}

编辑:

编辑:上面的代码已更新:

EDIT: Code above is updated:

 if (i <= 0)  {
                        timeline.stop(); //ERROR: variable timeline might not have been initialized
                    }


if-block提供

if-block gives

ERROR: variable timeline might not have been initialized

错误

推荐答案

而不是添加构造函数,请尝试在时间轴初始化后添加KeyFrame.

Instead of adding in constructor, try adding KeyFrames after Timeline is initialized.

Timeline timeline = new Timeline();
KeyFrame kf = new KeyFrame(Duration.seconds(0),
        event -> {
            setText(String.valueOf(i--));
            if (i <= 0) {
                timeline.stop();
            }
        });
timeline.getKeyFrames().addAll(kf, new KeyFrame(Duration.seconds(1)));

这篇关于JavaFX:如何在达到零时停止时间轴倒数计时器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆