问答题

下面是一个Applet程序,其功能是实现网页上的电子时钟,要求显示的格式为hh:mm:ss如02:04:50。提示:通过获取当前系统时间来实现。请改正程序中的错误(有下划线的语句),使程序能输出正确的结果。 注意:不改动程序的结构,不得增行或删行。 程序运行结果如下:
import java.applet.*; import java.awt.*; import java.util.Date; /* <applet code=”ex9_3.Class”,width;800 height=400> </applet> */ public class ex9_3 extends Applet implements Runnable{ private Thread thTimer:null; private String strTime; private int tHour,tMin,tSec; public void init(){ setFont(new Font("Times New Roman",Font.BOLD,20)); } public void paint(Graphics Graph){ Date dNow = new Date(); tHour = dNow.getHours(); tMin = dNow.getMinutes(); tSec = dNow.getSeconds(); if(tHour<=9) strTime = "0" + tHour + ":" ; else strTime = tHour + ":" ; if (tMin<=9) strTime = "0" + strTime + tMin + ":"; else strTime = strTime + tMin + ":" ; if(tSec<=9) strTime = "0" + strTime + tSec; else strTime = strTime + tSec; Graph.drawString(strTime,80,80); } public void start() { if(thTimer == null) { thTimer = new Thread(); thTimer.start(); } } public void run(){ while(thTimer != null){ repaint(); try{ Thread.sleep(1000); } catch (InterruptedException ie){ } } } public void stop () { thTimer = null; } } ex9_3.html <HTML> <HEAD> <TITLE>ex9_3</TITLE> </HEAD> <BODY> <applet code="ex9_3.class"width = 800 height=400> </applet> </BODY> </HTML>

【参考答案】

StrTime = strTime + "0" + tMin + ":"strTime = StrTime + "0" ......

(↓↓↓ 点击下方‘点击查看答案’看完整答案 ↓↓↓)
热门 试题