這是根據需要爲一個簡單的秒錶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