在此applet示例中,您将看到如何在浏览器状态栏中显示消息。为了使示例更加有趣,我们将当前时间显示为消息。该时间将在小应用程序的生命周期中每秒更新一次。package org.nhooo.example.applet;
import java.applet.Applet;
import java.awt.Graphics;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class TimeApplet extends Applet implements Runnable {
private DateFormat formatter = null;
private Thread t = null;
public void init() {
formatter = new SimpleDateFormat("hh:mm:ss");
t = new Thread(this);
}
public void start() {
t.start();
}
public void stop() {
t = null;
}
public void paint(Graphics g) {
Date now = Calendar.getInstance().getTime();
// 在浏览器状态栏上显示当前时间
this.showStatus(formatter.format(now));
}
public void run() {
int delay = 1000;
try {
while (t == Thread.currentThread()) {
// 每秒重新绘制小程序
repaint();
Thread.sleep(delay);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}