用swt做一个单机程序,已经建了一个shell,上面添加了一个文本框,我希望文本框的内容过一段时间换一下,比如过500毫秒;更换几次后,给出一个输入框,让用户输入一些反馈。以前没有做过这方面的开发,希望各位给个思路,或代码片段。非常感谢!

解决方案 »

  1.   

    Timer 和 TimerTask
    看看这两个类
      

  2.   

    我用Time实现过一个,在Timer里修改Text控件,但是报Invalid thread access错。
    这是我的代码片段
    timer.schedule(
    new TimerTask() {
    int i=0;
    public void run() {
    helloworldText.setText("hello"+i);
    i++;
    if(i==10) timer.cancel();

    }, 0, 500);//timer是我在类里定义的静态Timer属性
      

  3.   

    import java.util.Timer;
    import java.util.TimerTask;
    public class sql
    {
        static Timer timer=new Timer();    public static void main(String[] args) {
            timer.schedule(new task(),0,100);
        }
        
        static class task extends TimerTask{
            int i=0;
            public void run()
            {
                System.out.println(i++);
                if (i==10)
                {
                    timer.cancel();
                }
            }
        }
    }我这样写没错
      

  4.   

    Invalid thread access错
    是因为SWT禁止非本体线程对控件的显示内容进行更改,如果你必须这样做
    则需要调用以下方法,在该方法内对控件进行操作:
    Display.getDefault().syncExec( 
    new Runnable()
    {
    public void run()
    {
    //do your job
    }
    });定时器我是这么写的,供你参考:
    timer = new Timer(
    CAPTURE_PER_MILLISECOND, 
    new ActionListener() 
    {
       public void actionPerformed(ActionEvent evt) 
       {
        Display.getDefault().syncExec(new Runnable()
    {
    public void run()
    {}
                                }
                           }
              });
    timer.start();