繁体   English   中英

创建一个以 hh:mm:ss 格式显示时间的计时器 JavaFX

[英]Creating a timer that shows the time in hh:mm:ss format 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并且在特定操作后计时器将启动。

我怎样才能做到这一点?

谢谢。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM