使用AWT制作倒数计时器

时间:2014-11-06 06:44:05

标签: java multithreading timer

我试图在AWT Frame中制作倒数计时器,但是在标签中更改了数字。我试图用线程来解决问题。这是我未完成的代码:

public static void main(String[] args){
    Frame app = new Frame("Time Ticker");
    //label is used to display digit here I will let the app count from 0-99;
    final Label label = new Label(); 
    //set it final bc this can be accessed in Anonymous Inner class

    Thread threadDigit = new Thread(){
        public void run(){
           for(int i=0; i<100; i++){
              label.setText(""+i);  
           //Problem: Reported As Error Why I cannot setText this way?
              try{
                //so the count down process will be more obvious
                sleep(500);
              }catch(Exception e){
                System.out.println("insomia");
              }       
        }

    };
    threadDigit.start();
    app.add(label);
    app.setSize(300,400);
    app.setVisible(true);


}

即使我这样做了,结果是没有线程睡眠,只显示99,我知道我只启动一次threadDigit,所以for循环做了0-99之间的所有事情并显示99,想知道我怎么能看到数字从0-99开始,500毫秒的间隔。谢谢你的帮助!

0 个答案:

没有答案