public void shiftRing(Tower a, Tower b) {
            done = false;
            temp = a.rings.pop();
            currentX = temp.x;
            from = a;
            to = b;
            to.rings.push(temp);
            timer = new Timer(1, new ActionListener() {                public void actionPerformed(ActionEvent e) {
                    if (done) {timer.stop();}
                    
                    int incre = 1;
                    if (to.baseX - from.baseX < 0 ) incre *= -1;
                    
                    if (temp.y <= 20) up = true;
                    if (temp.x == to.baseX + currentX - from.baseX) move = true;
                    if (temp.y == to.baseY - temp.height) down = true;                    if (!up) temp.setLocation(temp.x, temp.y - 1);
                    else if(!move) temp.setLocation(temp.x + incre , 20);
                    else if(!down) temp.setLocation(temp.x, temp.y + 1);
                    else {
                        up = false;
                        move = false;
                        down = false;
                        done = true;
                    }
                    System.out.println(temp.getLocation());
                    repaint();
                }
            });
            timer.start();
        }如题,我用了 if (done) {timer.stop();}
但是还是停止不了这个timer,我想在done为真之后就退出timer和shiftRing这个方法,求助各位~~

解决方案 »

  1.   

    timer = new Timer(1, new ActionListener() {  public void actionPerformed(ActionEvent e) {
      if (done) {timer.stop();}这里能编译成功吗 调用stop时的timer还未初始化完成呢
      

  2.   

    if (done) {
    System.out.println("Stop()"); // 随便打印点东西看一下是否真的退出
    timer.stop();
    }
      

  3.   

    是吗 你是用什么编译的 奇怪啊
    这里timer.stop()是位于Timer的构造体中,应会提示:timer may not have been initialized 我实际试验下来也是如此,完整的程序看一下就知道了
      

  4.   

    定时器都不清楚 好好看看jdk timer