0

无法每秒更新时间

import java.awt.*;
import java.applet.*;
import java.util.*;
public class AppletClock extends Applet{

    Calendar calendar; 
    Thread changeTime = null;
    Thread changeBgColor = null;
    String currentTime;
    Color randomColor;
    Font font = new Font("Arial",Font.BOLD+Font.ITALIC,80);

    public void init(){     
        setBackground(Color.black);
        setForeground(Color.WHITE);     

        changeTime = new Thread(new Runnable(){

            public void run(){              
                for(;;){                    
                    calendar = Calendar.getInstance();/*When I Instantiate calendar object outside of for loop this code doesnot work and the time didnt gets updated every second Its because of this factory method or something else*/
                    currentTime = calendar.get(Calendar.HOUR) +":"+calendar.get(Calendar.MINUTE)+":"+calendar.get(Calendar.SECOND);

                    try{

                        Thread.sleep(1000);

                    }
                    catch(Exception e){                     
                        System.out.println(e);                      
                    }
                    repaint();                  
                }               
            }
        });
        changeTime.start();

        changeBgColor = new Thread(new Runnable(){

            public void run(){              
                for(;;){                    
                    Random random = new Random();
                    int red = random.nextInt(255);
                    int green = random.nextInt(255);
                    int blue = random.nextInt(255);
                    randomColor = new Color(red,green,blue);

                    try{

                        Thread.sleep(1000);

                    }
                    catch(Exception e){                     
                        System.out.println(e);                      
                    }
                    repaint();                  
                }               
            }
        });
        changeBgColor.start();      
    }

    public void paint(Graphics g){      
        setBackground(randomColor);

        g.setFont(font);
        g.drawString(currentTime,80,120);       
    }
}
/*      
        <APPLET CODE = "AppletClock.class" WIDTH = 500 HEIGHT = 200></APPLET>       
        */

当我在循环calendar之外实例化对象时,for此代码不起作用,并且时间不会每秒更新一次。这是因为这个工厂方法或其他原因

4

5 回答 5

6

创建日历实例时,它会使用当前时间设置时间,因此如果您在 2013 年创建实例并访问同一实例而不在 2014 年更新它,它仍将保持创建时间

如果您想每次都更新实例,您可以设置当前毫秒

calendar.setTime(System.currentTimeInMillis());
于 2013-07-12T06:32:21.490 回答
3

日历代表一些固定的时间。因此,如果您在循环之外实例化一次,它将始终表示相同的时间,除非您在循环中更新它,例如使用

calendar.setTime(new Date());

或者

calendar.setTimeInMillis(System.currentTimeMillis());

也就是说,鉴于您只需要此循环中的日历,我不明白您为什么不保留此解决方案,并将其声明为局部变量而不是将其声明为字段。

于 2013-07-12T06:34:30.493 回答
2

当您调用时设置时间 calendar = Calendar.getInstance();当您在循环之外执行此操作时,它只设置一次并且不再更改。

此外,您应该考虑使用 ScheduledExecutorService 而不是两个线程。它更容易处理,你只需要一个线程。

于 2013-07-12T06:34:27.170 回答
1

它与实例化它的位置无关。就像日期一样,它保存您实例化它的那一刻的静态时间。

于 2013-07-12T06:37:01.633 回答
0

我不知道你为什么要打扰...

你可以,而不是只提供一个自定义DateFormat

public static final DateFormat TIME_FORMAT = new SimpleDateFormat("h:M:s");
private Date currentTime;

然后在你的线程中......

public void run(){
    for(;;){
        currentTime = new Date();
        try{
            Thread.sleep(1000);
        }
        catch(Exception e){
            System.out.println(e);
        }
        repaint();
    }
}

然后在你的绘画方法中......

public void paint(Graphics g){
    setBackground(randomColor);
    g.setFont(font);
    g.drawString(TIME_FORMAT.format(currentTime),80,120);
}

但这只是我...

于 2013-07-12T06:43:58.620 回答