Java使用javaFX倒计时

时间:2016-01-19 16:59:23

标签: java javafx timer countdown resume

我在javafx中制作倒数计时器。我已经介绍了一个问题:我希望能够在点击按钮时暂停和恢复计时器。

但是,当我这样做时,就像现在的代码一样,时间轴没有恢复到以前的速度。

那我该如何制作呢?我可以点按一下按钮暂停和恢复计时器?

代码:

package gameStats.controllers;

import gameStats.Main;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Background;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.util.Duration;

import java.io.IOException;
import java.security.Timestamp;
import java.sql.Time;
import java.util.concurrent.TimeUnit;

public class MainController {

    private Timeline timeline = new Timeline();
    @FXML
    private Label timerText;
    @FXML
    private Button startTimerButton, stopTimerButton, resetTimerButton, addTeamButton, addPointButton, removePointButton, newTimeButton;
    @FXML
    private TextField teamNameTextField;
    @FXML
    private TableView teamView;

    private int min;
    private int startTimeSec, startTimeMin;
    private Parent borderPane;
    public BorderPane timeBorderPane;
    private Duration keyframeToContinuefrom;

    public void startTimer() {
        if(!timeline.getStatus().equals("paused")) {
            if (!(startTimeMin < 0)) {
                timerText.setTextFill(Color.BLACK);
                startTimeSec = 60; // Change to 60!
                startTimeMin = min - 1;
                timeline.setCycleCount(Timeline.INDEFINITE);
                timeline.getKeyFrames().add(new KeyFrame(Duration.seconds(1), new EventHandler<ActionEvent>() {
                    @Override
                    public void handle(ActionEvent event) {
                        startTimeSec--;
                        boolean isSecondsZero = startTimeSec == 0;
                        boolean timeToChangeBackground = startTimeSec == 0 && startTimeMin == 0;

                        if (isSecondsZero) {
                            startTimeMin--;
                            startTimeSec = 60;
                        }
                        if (timeToChangeBackground) {
                            timeline.stop();
                            startTimeMin = 0;
                            startTimeSec = 0;
                            timerText.setTextFill(Color.RED);

                        }

                        timerText.setText(String.format("%d min, %02d sec", startTimeMin, startTimeSec));

                    }
                }));
                timeline.playFromStart();
            } else {
                Alert alert = new Alert(Alert.AlertType.INFORMATION, "You have not entered a time!");
                alert.showAndWait();
            }
        }else {

        }

    }

    public void setTimer() {
        try {
            FXMLLoader loader = new FXMLLoader();
            loader.setLocation(Main.class.getResource("newTimeDialog.fxml"));
            Parent newTimeBorderPane = (BorderPane) loader.load();
            borderPane = newTimeBorderPane;
            Scene scene = new Scene(newTimeBorderPane);
            Stage primaryStage = new Stage();
            primaryStage.setScene(scene);
            primaryStage.showAndWait();
            if(!primaryStage.isShowing()){
                min = newTimeController.getMin();
                timerText.setText(" " + min);
            }else{

            }

        } catch (IOException e) {
            e.printStackTrace();
        }


    }

    public void stopTimer(){
        timeline.pause();
    }

    public void resetTimer(){
        timeline.stop();
        startTimeSec = 60;
        startTimeMin = min;
        timerText.setText(String.format("%d min, %02d sec", startTimeMin, startTimeSec));
    }

}

1 个答案:

答案 0 :(得分:0)

<强>更新

我的问题的解决方案:

package gameStats.controllers;

import gameStats.Main;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Background;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.util.Duration;

import java.io.IOException;
import java.security.Timestamp;
import java.sql.Time;
import java.util.concurrent.TimeUnit;

public class MainController {

    private Timeline timeline = new Timeline();
    @FXML
    private Label timerText;
    @FXML
    private Button startTimerButton, stopTimerButton, resetTimerButton, addTeamButton, addPointButton, removePointButton, newTimeButton;
    @FXML
    private TextField teamNameTextField;
    @FXML
    private TableView teamView;

    private int min;
    private int startTimeSec, startTimeMin;
    private Parent borderPane;
    public BorderPane timeBorderPane;
    private boolean isRunning;

    public void startTimer() {
        if(isRunning == false) { //Added a if statement to switch on "running"
            if (!(startTimeMin < 0)) {
                KeyFrame keyframe = new KeyFrame(Duration.seconds(1), new EventHandler<ActionEvent>() {
                    @Override
                    public void handle(ActionEvent event) {

                        startTimeSec--;
                        boolean isSecondsZero = startTimeSec == 0;
                        boolean timeToChangeBackground = startTimeSec == 0 && startTimeMin == 0;

                        if (isSecondsZero) {
                            startTimeMin--;
                            startTimeSec = 60;
                        }
                        if (timeToChangeBackground) {
                            timeline.stop();
                            startTimeMin = 0;
                            startTimeSec = 0;
                            timerText.setTextFill(Color.RED);

                        }

                        timerText.setText(String.format("%d min, %02d sec", startTimeMin, startTimeSec));

                    }
                });
                timerText.setTextFill(Color.BLACK);
                startTimeSec = 60; // Change to 60!
                startTimeMin = min - 1;
                timeline.setCycleCount(Timeline.INDEFINITE);
                timeline.getKeyFrames().add(keyframe);
                timeline.playFromStart();
                isRunning = true;
            } else {
                Alert alert = new Alert(Alert.AlertType.INFORMATION, "You have not entered a time!");
                alert.showAndWait();
            }
        }else {
            timeline.play(); //Playes from current position in in the direction indicated by rate.
        }

    }

    public void setTimer() {
        try {
            FXMLLoader loader = new FXMLLoader();
            loader.setLocation(Main.class.getResource("newTimeDialog.fxml"));
            Parent newTimeBorderPane = (BorderPane) loader.load();
            borderPane = newTimeBorderPane;
            Scene scene = new Scene(newTimeBorderPane);
            Stage primaryStage = new Stage();
            primaryStage.setScene(scene);
            primaryStage.showAndWait();
            if (!primaryStage.isShowing()) {
                min = newTimeController.getMin();
                timerText.setText(" " + min);
            } else {

            }

        } catch (IOException e) {
            e.printStackTrace();
        }


    }

    public void stopTimer() {
        timeline.pause();
    }

    public void resetTimer() {
        timeline.stop();
        startTimeSec = 60; 
        startTimeMin = min-1;
        timerText.setText(String.format("%d min, %02d sec", startTimeMin, startTimeSec));
    }

}