程序怎么改?
import java.awt.*;import java.applet.*;
import java.util.Date;
public class Clock extends Applet implements Runnable
{
Thread clockThread;
Font font;
public void init()
{
font =new Font("TimesRoman",Font.BOLD,48);
}
public void start()
{
if(clockThread==null)
{
clockThread=new Thread(this,"Showtime");
clockThread.start();
}
}
public void run()
{
while(clockThread!=null)
{
repaint();
try
{
clockThread.sleep(1000);
}catch(InterruptedException e) { }
}
}
public void paint(Graphics g)
{
Calendar now =new Calendar()
g.setFont(font);
g.setColor(Color.red);
g.drawString(now.getHours()+":"+now.getMinutes()+":"+now.getSeconds(),5,50);
}
public void stop()
{
}
}
编译结果:
D:\xu>javac Clock.java
Note: Clock.java uses or overrides a deprecated API.
Note: Recompile with -deprecation for details.
初步了解Date类的getHours(),getMinutes() getSeconds(),不能用了,
那该如何获取时间?
----------------解决方案--------------------------------------------------------
用Calendar的方法
get(Calendar.HOUR)
get(Calendar.MINUTE).....
----------------解决方案--------------------------------------------------------
以下是引用千里冰封在2007-6-21 12:29:45的发言:
用Calendar的方法
get(Calendar.HOUR)
get(Calendar.MINUTE).....
用Calendar的方法
get(Calendar.HOUR)
get(Calendar.MINUTE).....
同意....
还有就是Calendar now =new Calendar()是错的。。。应该是Calendar now = Calendar.getInstance();
然后再用 now.get(Calendar.HOUR_OF_DAY) 来写时间。如此类推...
再者包的引进:import java.util.*;
不能单单引进Date
----------------解决方案--------------------------------------------------------
改过来了,谢谢两位。
----------------解决方案--------------------------------------------------------