在一个SWT对话框中,我将添加登录和时间检查功能。
public class LoginDialog extends Dialog {
//basic code
/**
* Create contents of the dialog.
*/
private void createContents() {
//UI code
RealtimeThread realtimeThread=new RealtimeThread(lbl_time, btn_login);
Thread thread=new Thread(realtimeThread);
thread.start();
}
//relevance methods
}
class RealtimeThread implements Runnable{
private Label lbl_time;
private Button btn_login;
public RealtimeThread(Label lbl_time, Button btn_login) {
// TODO Auto-generated constructor stub
this.lbl_time=lbl_time;
this.btn_login=btn_login;
}
@Override
public void run() {
// TODO Auto-generated method stub
Display.getDefault().asyncExec(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
while(true){
int year, month, day, hour, min, sec;
year=Integer.parseInt(String.valueOf(new SimpleDateFormat("yyyy").format(new Date())));
month=Integer.parseInt(String.valueOf(new SimpleDateFormat("MM").format(new Date())));
day=Integer.parseInt(String.valueOf(new SimpleDateFormat("dd").format(new Date())));
hour=Integer.parseInt(String.valueOf(new SimpleDateFormat("HH").format(new Date())));
min=Integer.parseInt(String.valueOf(new SimpleDateFormat("mm").format(new Date())));
sec=Integer.parseInt(String.valueOf(new SimpleDateFormat("ss").format(new Date())));
lbl_time.setText(year +"year " + month +"month " + day + "day " + hour + ": " + min + ": " + sec );
break;
}
}
});
}
}
我想实时查看时间。并且登录功能可以同时工作。但是时间会在代码执行时运行,但对话功能会冻结。 在run()while(true)函数导致冻结。 这怎么解决了?