如何停止javax.swing.Timer
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这个方法,求助各位~~