首页 > 解决方案 > 创建一个以 hh:mm:ss 格式显示时间的计时器 JavaFX

问题描述

==================================================== =

编辑

感谢Sedrick 的帮助,我进步了。

public class Main extends Application {

    @Override // Override the start method in the Application class
    public void start(Stage primaryStage) {
        FakeCustomNode clockNode = new FakeCustomNode(180);
        Scene scene = new Scene(clockNode, 300, 300);
        primaryStage.setTitle("Timer"); // Set the stage title
        primaryStage.setScene(scene); // Place the scene in the stage
        primaryStage.show(); // Display the stage
        primaryStage.setResizable(false);
    }

    /**
     * The main method is only needed for the IDE with limited JavaFX support. Not
     * needed for running from the command line.
     */
    public static void main(String[] args) {
        launch(args);
    }
}

==================================================== ======================

final public class FakeCustomNode extends VBox {

    TimerGUI stopWatchGUI;

    public FakeCustomNode(int minutes) {
        stopWatchGUI = new TimerGUI(minutes);
        getChildren().addAll(stopWatchGUI.getStopWatch());
    }
}

==================================================== ===================

public class TimerGUI {
    Text display;
    Button start;
    Button pause;
    Button reset;
    VBox vbox = new VBox();

    int second;

public class TimerGUI {
    Text display;
    VBox vbox = new VBox();
    int second;

    public TimerGUI(int time) {
        this.second = time * 60;
        display = new Text(String.format("%02d:%02d:%02d", second / 3600, (second % 3600) / 60, second % 60));

        Timeline stopWatchTimeline = new Timeline(new KeyFrame(Duration.seconds(1), (ActionEvent event) -> {
            if (second-- > 0)
                display.setText(String.format("%02d:%02d:%02d", second / 3600, (second % 3600) / 60, second % 60));
        }));
        stopWatchTimeline.setCycleCount(Timeline.INDEFINITE);
        stopWatchTimeline.play();

        vbox.getChildren().addAll(display);
    }

    public VBox getStopWatch() {
        return vbox;
    }
}

我不明白如何更改代码,因此计时器将从特定操作开始。在我运行它的那一刻,计时器开始了。

我想显示03:00:00,在特定操作后计时器将启动。

我怎样才能做到这一点?

谢谢。

标签: javajavafxtimer

解决方案


当您说“特定操作”时,我假设您的意思是按下按钮之类的东西。在链接代码中,TimerGUI 有三个与之关联的按钮。Start, Pause, 和Reset. 使用它们来处理不同的情况。下面的代码演示了这些按钮。

TimerGUI 类

import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.event.ActionEvent;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
import javafx.util.Duration;

public class TimerGUI {
    Text display;
    Button start;
    Button pause;
    Button reset;
    VBox vbox = new VBox();

    int second;

    public TimerGUI(int minutes) {
        this.second = minutes * 60;
        display = new Text(String.format("%02d:%02d:%02d", second / 3600, (second % 3600) / 60, second % 60));
        // display = new Text();
        start = new Button("Start");        
        pause = new Button("Pause");
        reset = new Button("Stop");
        
        Timeline stopWatchTimeline = new Timeline(new KeyFrame(Duration.seconds(1), (ActionEvent event) -> {
            second--;
            display.setText(String.format("%02d:%02d:%02d", second / 3600, (second % 3600) / 60, second % 60));
        }));
        stopWatchTimeline.setCycleCount(Timeline.INDEFINITE);
        
        start.setOnAction((event) -> {
            stopWatchTimeline.play();
        });
        pause.setOnAction((event) -> {
            stopWatchTimeline.pause();
        });
        reset.setOnAction((event) -> {
            stopWatchTimeline.stop();
            this.second = minutes * 60;
            display.setText(String.format("%02d:%02d:%02d", second / 3600, (second % 3600) / 60, second % 60));
        });

        vbox.getChildren().addAll(display, new HBox(start, pause, reset));
    }

    public VBox getStopWatch() {
        return vbox;
    }
    
    
    public void setTimer(int minutes)
    {
        this.second = minutes * 60;
        display = new Text(String.format("%02d:%02d:%02d", second / 3600, (second % 3600) / 60, second % 60));
    }
}

推荐阅读