创建一个以hh:mm:ss格式显示时间的计时器JavaFX [英] Creating a timer that shows the time in hh:mm:ss format JavaFX

查看:100
本文介绍了创建一个以hh:mm:ss格式显示时间的计时器JavaFX的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

修改

感谢 Sedrick 的帮助,我取得了进步.

Thanks to Sedrick help, I progressed.

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;
    }
}

我不知道如何更改代码,因此计时器将在特定操作下启动. 在我运行它的那一刻,计时器开始计时.

I don't understand how to change the code so the timer will start on specific action. At the moment I ran it, the timer starts.

我想显示03:00:00,并且在执行特定操作后计时器将启动.

I want to show 03:00:00 and after specific action the timer will start.

我该怎么做?

谢谢.

推荐答案

当您说 特定动作" 时,我假设您的意思是按下按钮.在链接的代码中,TimerGUI具有与之关联的三个按钮. 开始暂停重置.使用它们来处理不同的情况.下面的代码演示了这些按钮.

When you say "specific action", I am assuming you mean something like a button press. In the linked code the TimerGUI has three buttons associated with it. Start, Pause, and Reset. Use those to handle different situations. The code below demos those buttons.

TimerGUI类

TimerGUI Class

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));
    }
}

这篇关于创建一个以hh:mm:ss格式显示时间的计时器JavaFX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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