这是根据需要为一个简单的秒表JavaFX的一类,风格标签对象JavaFX的秒表计时
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;
在类
初始化瓦尔
//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(时间)函数以适应应用程序
问题是什么? – Enrique 2012-02-20 05:37:24
恩里克 - 毫无疑问,我一直在寻找这样的东西,并没有真正找到适合我需求的东西,所以这就是我最终的目标。如果其他人想要改进或使用它,那也没关系......关心 – KEV 2012-02-22 23:36:00