用Java开发C/S应用(三):计时器稍加改动,改为一个动态时钟
这里直接贴代码了,在上一篇文章的基础上,就不做解释了:
Runnable runnable = new Runnable() {
public void run() {
while (true) {
Display.getDefault().asyncExec(new Runnable() {
public void run() {
java.text.DateFormat df = new java.text.SimpleDateFormat( "yyyy-MM-dd HH:mm:ss");
String s = df.format(new java.util.Date());
text.setText(s);
}
});
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
}
}
};
Thread clock = new Thread(runnable);
clock.start();
public void run() {
while (true) {
Display.getDefault().asyncExec(new Runnable() {
public void run() {
java.text.DateFormat df = new java.text.SimpleDateFormat( "yyyy-MM-dd HH:mm:ss");
String s = df.format(new java.util.Date());
text.setText(s);
}
});
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
}
}
};
Thread clock = new Thread(runnable);
clock.start();