大家一起为奥运出力,看我的奥运倒计时问题出在哪里? (applet,线程的关闭)
程序功能实现一个可以精确到秒的倒计时,所以想用到thread sleep()一下,实现每秒获取一次系统时间。不过这样一来在网页中调用它就关不掉了,必须还得去进程里结束。
欢迎大虾指正!
import java.applet.Applet;
import java.awt.*;
import java.util.Calendar;
import java.util.GregorianCalendar;
import javax.swing.* ;
public class Data extends Applet {
boolean b = true ;
Calendar calendar = new GregorianCalendar();
Calendar examdate = new GregorianCalendar(2008,7,8,20,0,0);
Thread t = new Thread();
public void run() {
}
public void paint(Graphics g) {
int sign=0;
int signy=0;
t.start();
try
{
while( calendar.before( examdate ) && b )
{
g.clearRect(5,5,250,70);
g.drawRect(5,5,250,70);
g.setColor(Color.red);
g.drawString( " 现在时刻 : " +(calendar.get(Calendar.MONTH)+1)+ " 月 "+
calendar.get(Calendar.DAY_OF_MONTH) + " 日 "+ calendar.get(Calendar.HOUR_OF_DAY)+
" 时 "+calendar.get(Calendar.MINUTE)+ " 分 "+ calendar.get(Calendar.SECOND)+ " 秒 ",15,30);
if(calendar.get(Calendar.YEAR)!=2008)
signy=1;
else
signy=0;
if(calendar.get(Calendar.HOUR_OF_DAY)> =8)
{
sign=1;
}
else
sign=0;
g.drawString( " 离奥运开幕还有 : "+(365* signy +19-calendar.get(Calendar.DAY_OF_YEAR)-sign)+ " 天 "+
(24*sign+7-calendar.get(Calendar.HOUR_OF_DAY)) + " 时 "+ (59-calendar.get(Calendar.MINUTE))+
" 分 " + (60-calendar.get(Calendar.SECOND)) + " 秒 " ,15,60);
t.sleep(1000);
calendar = new GregorianCalendar();
}
g.drawString( " 感 谢 使 用 08 奥 运 倒 计 时 ",15,30);
g.drawString( " 祝 中 国 成 功 ! ",15,60);
}
catch(InterruptedException e)
{
}
}
public void stop() {
b=false;
System.exit(0);
}
public void destroy() {
b=false;
System.exit(0);
}
}
------解决方案--------------------写的就不对
你在paint里面 不停的循环
主线程都被你阻塞了