下面这个程序我用SWT写就不行,但是用swing就可以,为什么啊?
多谢高手们了
import java.util.Timer;
import java.util.TimerTask;import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;public class MyTimerTest { /**
 * Launch the application
 * @param args
 */
public static void main(String[] args) {
final Display display = Display.getDefault();
final Shell shell = new Shell();
shell.setLayout(new FillLayout());
shell.setSize(500, 375);
shell.setText("SWT Application");
// shell.open(); final Composite composite = new Composite(shell, SWT.NONE); final Label label = new Label(composite, SWT.NONE);
// label.setText("Label");
Timer timer=new Timer();
TimerTask myTimer=new MyTimer(label);
timer.schedule(myTimer, 0, 1000);
label.setBounds(33, 159, 120, 30);
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
}}import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimerTask;import org.eclipse.swt.widgets.Label;public class MyTimer extends TimerTask{
Label label;
/**
 * @param args
 */
public MyTimer(Label label){
this.label=label;
}
@Override
public void run() {
while(true){
try{
Date date=new Date();
SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
label.setText(df.format(date));
}catch(Exception e){
e.printStackTrace();
}
}
}
}

解决方案 »

  1.   

    这是swt的安全机制,附线程不能改变主线程中的widget,但是其他java数据却可以,所以你只能通过附线程改变主线程的数据,不能改变widget,widget只有主线程能控制。
      

  2.   

    是一楼说的原因。另外,他说的这个主线程通常叫做用户线程,在这个线程中可以进行UI更新,如果非要在非用户线程中操作图形控件,可以使用Display提供的两个方法,syncExec(Runnable)和asyncExec(Runnable),分别是同步异步执行一个线程。
      

  3.   

    比如,在main中:
    Display.getCurrent().asyncExec(new Runnable() {
         //定义一个myTimerThread
         public void run() {
             while(true){
                  Date date=new Date();
                   ……
                  label.setText(……);
             }
         }
    });
      

  4.   

    Display.getDefault().asyncExec(new Runnable(){
    public void run() {
    while(true){
    Date date=new Date();
    SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    label_3.setText(df.format(date));
    try{
    Thread.sleep(1000);
    }catch(Exception e){
    e.printStackTrace();
    }
    }
    }

    });
    我直接这么写的行不行啊?
    运行以后就没有反映了,不过非用户线程还是创建了
      

  5.   

    写好了,在Eclipse3.2.1下正常运行。我等着收分,呵呵。package zhangshu.test.swt.window;import java.text.SimpleDateFormat;
    import java.util.Date;
    import org.eclipse.swt.SWT;
    import org.eclipse.swt.layout.FillLayout;
    import org.eclipse.swt.widgets.Composite;
    import org.eclipse.swt.widgets.Display;
    import org.eclipse.swt.widgets.Label;
    import org.eclipse.swt.widgets.Shell;public class MyTimerTest { /**
     * Launch the application
     * @param args
     */
    public static void main(String[] args) {
    final Display display = Display.getDefault();
    final Shell shell = new Shell();
    shell.setLayout(new FillLayout());
    shell.setSize(500, 375);
    shell.setText("SWT Application");
    shell.open(); final Composite composite = new Composite(shell, SWT.NONE); final Label label = new Label(composite, SWT.NONE);
    label.setBounds(33, 159, 200, 30);
    shell.layout();

    while (!shell.isDisposed()) {
    display.syncExec(new Thread(){
    public void run() {
    Date date=new Date();
    SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    label.setText(df.format(date));
    }
    });
    if (!display.readAndDispatch()){
    try {
    Thread.sleep(0);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    }
    }
    }
      

  6.   

    用你这方法是可以实现,但是新创建的线程CPU占用90%左右啊 
    有没有好点的办法啊
    只是显示一个时间就那样不好吧
      

  7.   

    再问一下非UI线程要改变UI线程的控件的话只能这么做吗?
    谢谢了
    可以把QQ留下吗
    我是新学的
      

  8.   

    我觉得SWT还不成熟,我一直都是用Swing.