用中文注释一下代码://Clock.java import java.awt.*; import javax.swing.*; import java.util.*; //import Date and Locale import java.text.SimpleDateFormat; public class Clock extends JApplet implements Runnable { Thread clockThread; public void init() { //在JApplet的方法init 中,创建一线程命名为Clock clockThread = new Thread(this, "Clock-thread"); clockThread.start(); } //实现Runnable接口中的方法run,每隔一秒刷新时钟画面 public void run() { boolean flag=true; while (flag) { repaint(); try { clockThread.sleep(1000); } catch (InterruptedException e) { flag=false;} } //end of while } //end of run public void paint(Graphics g){ //JApplet的方法paint,显示当前时钟对象的值 super.paint(g); SimpleDateFormat formatter = new SimpleDateFormat (" hh:mm:ss ", Locale.getDefault()); Date currentDate = new Date(); String lastdate = formatter.format(currentDate); g.drawString(lastdate ,5, 10); } public void destroy() { //JApplet的方法 clockThread.interrupt(); } }
时间: 2024-02-15 17:26:52 浏览: 165
com.pinktwins.rtf:对 javax.swing.text.rtf.* 的自定义修复
//Clock.java
//导入需要使用的类库
import java.awt.*;
import javax.swing.*;
import java.util.*;
import java.text.SimpleDateFormat;
//定义一个类Clock,继承自JApplet类,并实现Runnable接口
public class Clock extends JApplet implements Runnable {
//声明一个线程对象clockThread
Thread clockThread;
//在JApplet的方法init中,初始化clockThread线程并启动
public void init() {
clockThread = new Thread(this, "Clock-thread");
clockThread.start();
}
//实现Runnable接口中的方法run,每隔一秒刷新时钟画面
public void run() {
boolean flag=true;
while (flag) {
repaint(); //调用repaint()方法,刷新时钟画面
try {
clockThread.sleep(1000); //线程休眠一秒钟
} catch (InterruptedException e) {
flag=false;
}
} //end of while
} //end of run
//实现JApplet的方法paint,显示当前时钟对象的值
public void paint(Graphics g){
super.paint(g); //调用父类的paint()方法
SimpleDateFormat formatter = new SimpleDateFormat (" hh:mm:ss ", Locale.getDefault()); //创建日期格式化对象
Date currentDate = new Date(); //获取当前时间
String lastdate = formatter.format(currentDate); //将当前时间格式化
g.drawString(lastdate ,5, 10); //在指定位置绘制时间字符串
}
//实现JApplet的方法destroy,中断线程
public void destroy() {
clockThread.interrupt(); //中断线程
}
}
阅读全文