JavaFX 秒表计时器

标签 javafx timer stopwatch

这是一个用于 JavaFX 的简单秒表的类,根据需要设置 Label 对象的样式

package aaa;

import java.text.SimpleDateFormat;
import java.util.Date;
import javafx.beans.property.SimpleStringProperty;

/**
 *
 * @author D07114915
 */
public class KTimer extends Thread {

private Thread thread = null;
private SimpleDateFormat sdf = new SimpleDateFormat("mm:ss:S");
private String[] split;
private SimpleStringProperty min, sec, millis, sspTime;
private long time;

public static void main(String[] args) {
    KTimer t = new KTimer();
    t.startTimer(00);
}

public KTimer() {
    min = new SimpleStringProperty("00");
    sec = new SimpleStringProperty("00");
    millis = new SimpleStringProperty("00");
    sspTime = new SimpleStringProperty("00:00:00");
}

public void startTimer(long time) {
    this.time = time;
    thread = new Thread(this);
    thread.setPriority(Thread.MIN_PRIORITY);
    thread.start();
}

public void stopTimer(long time) {
    if (thread != null) {
        thread.interrupt();
    }
    this.time = time;
    setTime(time);
}

public void setTime(long time) {
    this.time = time;
    split = sdf.format(new Date(time)).split(":");
    min.set(split[0]);
    sec.set(split[1]);

    if (split[2].length() == 1) {
        split[2] = "0" + split[2];
    }
    millis.set(split[2].substring(0, 2));

    sspTime.set(min.get() + ":" + sec.get() + ":" + millis.get());
}

public long getTime() {
    return time;
}

public SimpleStringProperty getSspTime() {
    return sspTime;
}

@Override
public void run() {
    try {
        while (!thread.isInterrupted()) {
            setTime(time);
            sleep(10);
            time = time + 10;
        }
    } catch (Exception e) {
    }

}
}//end of class

现在只需为您的 GUI 获取属性的监听器

添加变量
    KTimer ktimer;
    Label timeLabel;

在你的类(class)中初始化 vars
    //Clock
    ktimer = new KTimer();
    timeLabel = new Label(ktimer.getSspTime().get());
    ktimer.getSspTime().addListener(new InvalidationListener() {

        @Override
        public void invalidated(Observable observable) {
            timeLabel.setText(ktimer.getSspTime().get());
        }
    });

然后调用该方法在您需要的任何地方启动和停止

停止和复位是
                ktimer.stopTimer(0);

开始和暂停计时器是
               ktimer.startTimer(ktimer.getTime());

任何改进都值得赞赏,因为该类有点 CPU 消耗量...,但您可以调整运行线程和 setTime(time) 函数以适应应用程序

最佳答案

这是一个略有不同的版本(也许更好),我不确定同步方法是否真的必要

package aaa;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import javafx.beans.property.SimpleStringProperty;

/**
 *
 * @author D07114915
 */
public class KTimer {

private SimpleDateFormat sdf = new SimpleDateFormat("mm:ss:S");
private String[] split;
private SimpleStringProperty sspTime;
private long time;
private Timer t = new Timer("Metronome", true);
private TimerTask tt;
boolean timing = false;

public KTimer() {
    sspTime = new SimpleStringProperty("00:00:00");
}

public void startTimer(final long time) {
    this.time = time;
    timing = true;
    tt = new TimerTask() {

        @Override
        public void run() {
            if (!timing) {
                try {
                    tt.cancel();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else {
                updateTime();
            }
        }
    };
    t.scheduleAtFixedRate(tt, 10, 10);
}

public synchronized void stopTimer() {
    timing = false;
}

public synchronized void updateTime() {
    this.time = this.time + 10;
    split = sdf.format(new Date(this.time)).split(":");
    sspTime.set(split[0] + ":" + split[1] + ":" + (split[2].length() == 1 ? "0" + split[2] : split[2].substring(0, 2)));
}

public synchronized void moveToTime(long time) {
    stopTimer();
    this.time = time;
    split = sdf.format(new Date(time)).split(":");
    sspTime.set(split[0] + ":" + split[1] + ":" + (split[2].length() == 1 ? "0" + split[2] : split[2].substring(0, 2)));
}

public synchronized long getTime() {
    return time;
}

public synchronized SimpleStringProperty getSspTime() {
    return sspTime;
}
}

关于JavaFX 秒表计时器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9355303/

相关文章:

JavaFX 2 调试 CSS

javafx 如何设置文本或标签的中心位置?

每秒更新 GUI 的 Java 秒表?

c# - 如何在 Windows Phone 中暂停秒表?

netbeans - 在 JavaFX 中创建多个阶段

java - 从 JavaFX TextArea 获取滚动条/滚动 Pane 位置

python - 作为子流程倒计时的函数

swift - 我应该使用未命名的 Timer 还是 Dispatch asyncAfter 来延迟 1 次?

perl - AnyEvent 计时器问题

javascript - 秒表计时器无法正常工作