想做电梯模拟,不知道怎么实现像VB中enabled触发Timer控件那种效果?
用线程Thread也可以。

解决方案 »

  1.   

    定义了5个Timer,要在SWT上显示,但运行时提示org.eclipse.swt.SWTException: Invalid thread access,为什么阿?
    public Timer doorocTimer = new Timer();
    public Timer doorTimer = new Timer();
    public Timer elemoveTimer = new Timer();
    public Timer eleTimer = new Timer();
    public Timer waitTimer = new Timer();
    public TimerTask doorocTimerTask=new TimerTask(){
    public void run(){
    while(dooroc==true){
    door=false;
    switch(whichclick){
    case 1:
    if(imagex<=4){
    dooroc=false;
    door=true;
    wait3sec();
    return;
    }
    imagex=imagex-1;
    imagew=imagew+2;
    doorimage.setBounds(imagex,6 , imagew, 125);
    case -1:
    if(imagex>=65){
    dooroc=false;
    door=true;
    return;
    }
    imagex=imagex+1;
    imagew=imagew-2;
    doorimage.setBounds(imagex,6 , imagew, 125);
    }
    }
    }
    };
      

  2.   

    这是初始代码段:
                                  dooroc=false;
    doorocTimer.scheduleAtFixedRate(doorocTimerTask,0,40);
    door=false;
    doorTimer.scheduleAtFixedRate(doorTimerTask,0,500);
    ele=false;
    eleTimer.scheduleAtFixedRate(eleTimerTask,0,100);
    wait=false;
    waitTimer.scheduleAtFixedRate(waitTimerTask,0,3000);
    elem=false;
    elemoveTimer.scheduleAtFixedRate(elemoveTimerTask,0,40);
    大家帮忙啊
      

  3.   

    开一个线程,
    让他每sleep(milseconds)后做一件事情,
    不就和那个VB/Delphi一样了么?
    当然Java也有这种现成的类。
    想问一句:哥们有没有研究两个电梯的调度?
      

  4.   

    用javax.swing.Timer类
    它每隔一段时间产生一个ActionEvent
    Timer timer = new Timer(1000,this);//1000是时间间隔, this是ActionEvent的Listener
    timer.setInitialDelay(1000);
    timer.start();
      

  5.   

    public Thread elevatorThread=new Thread(){
    public void run(){
    while(true){
    if(dooroc==true)
    doorocf();
    if(door==true)
    doorf();
    if(ele==true)
    elef();
    if(elem==true)
    elemf();
    if(wait==true)
    waitf();
    }
    }
    };
    public void doorocf(){
    while(dooroc==true){
    door=false;
    switch(whichclick){
    case 1:
    if(imagex<=4){
    dooroc=false;
    door=true;
    wait3sec();
    return;
    }
    imagex=imagex-1;
    imagew=imagew+2;
    doorimage.setBounds(imagex,6,imagew,125);
    try{
    Thread.sleep(40);
    }catch(Exception e){}
    break;
    case -1:
    if(imagex>=65){
    dooroc=false;
    door=true;
    return;
    }
    imagex=imagex+1;
    imagew=imagew-2;
    doorimage.setBounds(imagex,6 , imagew, 125);

    try{
    Thread.sleep(40);
    }catch(Exception e){}
    break;
    }
    }
    }
    我这样做了还是不行啊,提示org.eclipse.swt.SWTException: Invalid thread access
      

  6.   

    问下XXKKFF(讠古 钅) :
    java.swing.Timer类和java.util.Timer有什么具体区别?为什么老是提示org.eclipse.swt.SWTException: Invalid thread access
      

  7.   

    javax.swing.Timer类产生ActionEvent,所以代码是在事件分发线程执行的
    只有gui程序才能用
      

  8.   

    大家帮看一下这是什么错?org.eclipse.swt.SWTException: Invalid thread access
    at org.eclipse.swt.SWT.error(SWT.java:3374)
    at org.eclipse.swt.SWT.error(SWT.java:3297)
    at org.eclipse.swt.SWT.error(SWT.java:3268)
    at org.eclipse.swt.widgets.Widget.error(Widget.java:435)
    at org.eclipse.swt.widgets.Widget.checkWidget(Widget.java:331)
    at org.eclipse.swt.widgets.Control.setBounds(Control.java:2109)
    at Elevator$1.run(Elevator.java:58)
    //注:58行代码:doorimage.setBounds(imagex,6,imagew,125);不管加什么样的代码此行都提示错误。
      

  9.   

    doorocThread=new Thread(){
    public void run(){
    while(true){
    if(dooroc==true){
    // door=false;
    switch(whichclick){
    case 1:
    if(imagex<=4){
    dooroc=false;
    // door=true;
    //wait3sec();
    return;
    }
    imagex=imagex-1;
    imagew=imagew+2;
    doorimage.setBounds(imagex,6,imagew,125);
    try{
    Thread.sleep(40);
    }catch(Exception e){}
    break;
    case -1:
    if(imagex>=65){
    dooroc=false;
    door=true;
    return;
    }
    imagex=imagex+1;
    imagew=imagew-2;
    doorimage.setBounds(imagex,6 , imagew, 125);
    try{
    Thread.sleep(40);
    }catch(Exception e){}
    break;
    }
    }
    }
    }
    };
    doorocThread.start();
    这样也不行!!!