Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
175 views
in Technique[技术] by (71.8m points)

Show a live duration (stopwatch) in a label when clicking a button and stop it when clicking another button using JavaFX-8

I would like to achieve the following using JavaFX-8:

  1. Press a button "Call"
  2. A live duration (stopwatch) starts from 00:00:00 and appears in a label, and keeps running every second
  3. Press another button "End Call"
  4. The live duration should stop
  5. Press again the button "Call"
  6. The live duration should reset to 00:00:00 and keeps running every second

The idea is to simulate a duration of a phone call from pressing "Call" to pressing "End Call".

public class Main extends Application {
    @Override
    public void start(Stage primaryStage) {

        Button callBtn = new Button("Call");
        Button endCallBtn = new Button("End Call");

        Label duration = new Label("Duration");
        Label callDuration = new Label("00:00:00");

        VBox vBox = new VBox(callBtn, duration, callDuration, endCallBtn);
        vBox.setPadding(new Insets(10));
        vBox.setSpacing(10);

        Scene scene = new Scene(vBox);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}
question from:https://stackoverflow.com/questions/65933699/show-a-live-duration-stopwatch-in-a-label-when-clicking-a-button-and-stop-it-w

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Below is one approach to address your requirement. Having said that, there may be other better approaches, but this can give the idea of how to start with.

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

import java.util.concurrent.TimeUnit;

public class StopClock_Demo extends Application {
    private long time;

    @Override
    public void start(Stage primaryStage) {
        Label callDuration = new Label("00:00:00");
        Timeline clock = new Timeline(new KeyFrame(Duration.ZERO, e -> {
            long millis = System.currentTimeMillis() - time;
            String txt = String.format("%02d:%02d:%02d",
                    TimeUnit.MILLISECONDS.toHours(millis),
                    TimeUnit.MILLISECONDS.toMinutes(millis) -
                            TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)),
                    TimeUnit.MILLISECONDS.toSeconds(millis) -
                            TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)));
            callDuration.setText(txt);
        }), new KeyFrame(Duration.seconds(1)));
        clock.setCycleCount(Animation.INDEFINITE);

        Button callBtn = new Button("Call");
        callBtn.setOnAction(e -> {
            if (clock.getStatus() != Animation.Status.RUNNING) {
                time = System.currentTimeMillis();
                clock.play();
            }
        });
        Button endCallBtn = new Button("End Call");
        endCallBtn.setOnAction(e -> {
            if (clock.getStatus() == Animation.Status.RUNNING) {
                clock.stop();
            }
        });
        Label duration = new Label("Duration");

        VBox vBox = new VBox(callBtn, duration, callDuration, endCallBtn);
        vBox.setPadding(new Insets(10));
        vBox.setSpacing(10);

        Scene scene = new Scene(vBox);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...