JavaFX LocalTime构建秒表

时间:2015-11-10 20:16:00

标签: javafx localtime

下面的代码用于构建一个秒表,当按下一个按钮并且从AnimationTimer调用该方法时,变量sMIN和sSEC被设置

我想知道是否有一种更简单的方法来编写这个例程,因为它似乎像意大利面一样代码

    private void elapsedTIME(){

    eSEC = LocalTime.now().getSecond();

    if(eSEC > sSEC){
        showSEC = eSEC - sSEC;
    }
    if(eSEC <= sSEC){
        showSEC = eSEC + (60-sSEC);     
    }

    if(showSEC == 60 && elapsedTimeTest == true){
        elapsedTimeTest = false;
    }
    if(showSEC == 60 && elapsedTimeTest == false ){
        eMIN = LocalTime.now().getMinute();
        if(eMIN > sMIN){
            showMIN = eMIN - sMIN;
        }
        if(eMIN == 0 && eMIN != sMIN){
            showMIN = 60 - sMIN;
        }
        if(eMIN < sMIN && eMIN != 0){
            showMIN = (60-sMIN) + eMIN;
        }
    }
    msg.setText("Elapsed Time "+showMIN+" Minutes "+(showSEC)+" Seconds"); 
}

1 个答案:

答案 0 :(得分:0)

我假设sSECsMIN是开始时的秒和分钟。

不要单独存储它们,只需在开始时存储LocalTime即可。然后你可以做类似的事情:

LocalTime startTime ;

// ...

private void elapsedTIME(){

    // Assumes startTime is set somewhere before this is invoked

    long elapsedSeconds = Duration.between(startTime, LocalTime.now()).getSeconds();
    long minutes = elapsedSeconds / 60 ;
    long seconds = elapsedSeconds % 60 ;
    msg.setText("Elapsed Time "+minutes+" Minutes "+seconds+" Seconds"); 
}

SSCCE:

import java.time.Duration;
import java.time.LocalTime;

import javafx.animation.AnimationTimer;
import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class Stopwatch extends Application {

    @Override
    public void start(Stage primaryStage) {
        Label stopwatch = new Label();
        BooleanProperty running = new SimpleBooleanProperty(false);

        AnimationTimer timer = new AnimationTimer() {

            private LocalTime startTime ;

            @Override
            public void handle(long now) {
                long elapsedSeconds = Duration.between(startTime, LocalTime.now()).getSeconds();
                long minutes = elapsedSeconds / 60 ;
                long seconds = elapsedSeconds % 60 ;
                stopwatch.setText("Time: "+minutes +" minutes "+seconds + " seconds");
            }
            @Override
            public void start() {
                running.set(true);
                startTime = LocalTime.now();
                super.start();
            }
            @Override
            public void stop() {
                running.set(false);
                super.stop();
            }
        };

        Button startStop = new Button();
        startStop.textProperty().bind(Bindings.when(running).then("Stop").otherwise("Start"));
        startStop.setOnAction(e -> {
            if (running.get()) {
                timer.stop();
            } else {
                timer.start();
            }
        });

        VBox root = new VBox(10, stopwatch, startStop);
        root.setPadding(new Insets(24));
        root.setMinWidth(240);
        root.setAlignment(Pos.CENTER);
        primaryStage.setScene(new Scene(root));
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

注意如果在秒表运行时午夜时间过去,这可能无法正常工作;您可以使用LocalDateTime来解决此问题。