import java.util.Timer;
import java.util.Date;
import java.util.TimerTask;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import java.text.SimpleDateFormat;
import com.swtdesigner.SWTResourceManager;public class CountTime { protected Shell shell; /**
* Launch the application
* @param args
*/
public static void main(String[] args) {
try {
CountTime window = new CountTime();
window.open();
} catch (Exception e) {
e.printStackTrace();
}
} /**
* Open the window
*/
public void open() {
final Display display = Display.getDefault();
createContents();
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
} /**
* Create contents of the window
*/
protected void createContents() {
shell = new Shell();
shell.setSize(500, 375);
shell.setText("SWT Application"); final Label label = new Label(shell, SWT.NONE);
label.setFont(SWTResourceManager.getFont("", 14, SWT.BOLD));
label.setBounds(21, 103, 461, 40);
Timer t = new Timer();//生成一个定时器
t.schedule(new MyTimerTask(label), 0, 1000);//延迟0毫秒开始执行任务,之后每隔1秒执行一次
//
}
class MyTimerTask extends TimerTask {
private Label l;
public MyTimerTask(Label l) {
this.l = l;
} @Override
public void run() {
// TODO Auto-generated method stub
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
Date dd = sdf.parse("2008-08-08 20:08:08");
long day = (dd.getTime()-System.currentTimeMillis())/(24*3600*1000l);
long hour = (dd.getTime()-System.currentTimeMillis()-day*24*3600*1000l)/(3600*1000l);
long minutes = (dd.getTime()-System.currentTimeMillis()-day*24*3600*1000l-hour*3600*1000l)/(60*1000l);
long seconds = (dd.getTime()-System.currentTimeMillis()-day*24*3600*1000l-hour*3600*1000l-minutes*60*1000l)/1000l;
String text = "距2008年奥运会还有"+day+"天"+hour+"时"+minutes+"分"+seconds+"秒";
l.setText(text);
} catch(Exception e) {
}
}
}}这是我的代码,为什么不能显示出时间呢,请高手帮忙!
import java.util.Date;
import java.util.TimerTask;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import java.text.SimpleDateFormat;
import com.swtdesigner.SWTResourceManager;public class CountTime { protected Shell shell; /**
* Launch the application
* @param args
*/
public static void main(String[] args) {
try {
CountTime window = new CountTime();
window.open();
} catch (Exception e) {
e.printStackTrace();
}
} /**
* Open the window
*/
public void open() {
final Display display = Display.getDefault();
createContents();
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
} /**
* Create contents of the window
*/
protected void createContents() {
shell = new Shell();
shell.setSize(500, 375);
shell.setText("SWT Application"); final Label label = new Label(shell, SWT.NONE);
label.setFont(SWTResourceManager.getFont("", 14, SWT.BOLD));
label.setBounds(21, 103, 461, 40);
Timer t = new Timer();//生成一个定时器
t.schedule(new MyTimerTask(label), 0, 1000);//延迟0毫秒开始执行任务,之后每隔1秒执行一次
//
}
class MyTimerTask extends TimerTask {
private Label l;
public MyTimerTask(Label l) {
this.l = l;
} @Override
public void run() {
// TODO Auto-generated method stub
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
Date dd = sdf.parse("2008-08-08 20:08:08");
long day = (dd.getTime()-System.currentTimeMillis())/(24*3600*1000l);
long hour = (dd.getTime()-System.currentTimeMillis()-day*24*3600*1000l)/(3600*1000l);
long minutes = (dd.getTime()-System.currentTimeMillis()-day*24*3600*1000l-hour*3600*1000l)/(60*1000l);
long seconds = (dd.getTime()-System.currentTimeMillis()-day*24*3600*1000l-hour*3600*1000l-minutes*60*1000l)/1000l;
String text = "距2008年奥运会还有"+day+"天"+hour+"时"+minutes+"分"+seconds+"秒";
l.setText(text);
} catch(Exception e) {
}
}
}}这是我的代码,为什么不能显示出时间呢,请高手帮忙!
l.getDisplay().asyncExec(new Runnable(){ public void run() {
l.setText(text);
}
});