结果
上面的代码示例将在启用 java 的 Web 浏览器中产生以下结果。
下面是另一个使用 Applet 显示时钟的示例。
import java.applet.*;
import java.awt.*;
import java.util.*;
import java.text.*;
public class javaApplication6 extends Applet implements Runnable {
Thread t1 = null;
int hours = 0, minutes = 0, seconds = 0;
String time = "";
public void init() {
setBackground( Color.green);
}
public void start() {
t1 = new Thread( this );
t1.start();
}
public void run() {
try {
while (true) {
Calendar cal = Calendar.getInstance();
hours = cal.get( Calendar.HOUR_OF_DAY );
if ( hours > 12 ) hours -= 12;
minutes = cal.get( Calendar.MINUTE );
seconds = cal.get( Calendar.SECOND );
SimpleDateFormat formatter = new SimpleDateFormat("hh:mm:ss");
Date d = cal.getTime();
time = formatter.format( d );
repaint();
t1.sleep( 1000 );
}
}
catch (Exception e) { }
}
public void paint( Graphics g ) {
g.setColor( Color.blue );
g.drawString( time, 50, 50 );
}
}