TimerTask仅触发一次

克里斯汀·贝克(Christian Baker)

我设置了TimerTask UpdateTask,但在启动程序时仅触发一次。为什么它不继续触发?

这里的某些方法在其他类中,如果您需要它们,请不要犹豫让我知道。

import java.awt.Graphics;
import java.util.Timer;
import java.util.TimerTask;


public class graphpanel extends variables
{
Timer timer = new Timer();

int ypoint;
int barheight;

int height = getHeight();
int width = getWidth();
int bars = (int)getLife() - (int)getAge();
int xpoint = 0;
int barwidth = 20;

public graphpanel()
{
    timer.schedule(new UpdateTask(), 10);
}


public void paintComponent (Graphics g)
{
    super.paintComponent(g);

    for (int i = 0; i < bars; i++)
    {
        barheight = (int) getTime(i)/100;
        ypoint = height/2 - barheight;
        g.drawRect(xpoint, ypoint, barwidth, barheight);
        g.drawString("hey", 10*i, 40);
    }
}

class UpdateTask extends TimerTask
{
    public void run()
    {
        bars = (int)getLife() - (int)getAge();
        System.out.print("TimerTask detected");
        repaint();
    }
}

}

斯蒂芬·温克勒(Stefan Winkler)

Timer.schedule(TimerTask, long) 仅将任务安排为一次性执行。

使用

timer.scheduleAtFixedRate(new UpdateTask(), 10, 10);

用于重复调用您的TimerTask

更多信息:JavaDoc

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章