import java.awt.*;
import java.util.Date;
import java.applet.*;
import java.text.DateFormat;
public class Clock extends Applet implements Runnable
{
Label time;
Thread timer;
DateFormat timeFormat;
volatile boolean running;
public void init()
{
Label time=new Label();
time.setFont=(new Font("hegg",Font.BOLD,12));
time.setAlignMent(Label.CENTER);
setLayout(new BorderLayout());
add(time,BorderLayout.CENTER);
timeFormat=DateFormat.getTimeInstance(DateFormat.MEDIUM);
}
public void start()
{
running=true;
if(timer==null){
timer=new Thread(this);
timer.start();}
}
public void run()
{
while(running){
time.setText(timeFormat.Format(new Date()));
try
{
Thread.sleep(1000);
}
catch(InterruptedException e){}
}
timer=null;
}
public void stop()
{
running=false;
}
public String getAppletInfo()
{
return "Clock applet Copyright (c) 2000 by David Flanagan";
}
}
import java.util.Date;
import java.applet.*;
import java.text.DateFormat;
public class Clock extends Applet implements Runnable
{
Label time;
Thread timer;
DateFormat timeFormat;
volatile boolean running;
public void init()
{
Label time=new Label();
time.setFont=(new Font("hegg",Font.BOLD,12));
time.setAlignMent(Label.CENTER);
setLayout(new BorderLayout());
add(time,BorderLayout.CENTER);
timeFormat=DateFormat.getTimeInstance(DateFormat.MEDIUM);
}
public void start()
{
running=true;
if(timer==null){
timer=new Thread(this);
timer.start();}
}
public void run()
{
while(running){
time.setText(timeFormat.Format(new Date()));
try
{
Thread.sleep(1000);
}
catch(InterruptedException e){}
}
timer=null;
}
public void stop()
{
running=false;
}
public String getAppletInfo()
{
return "Clock applet Copyright (c) 2000 by David Flanagan";
}
}
改:time.setFont(new Font("hegg",Font.BOLD,12));原:time.setAlignMent(Label.CENTER);
改:time.setAlignment(Label.CENTER);原:time.setText(timeFormat.Format(new Date()));
改:time.setText(timeFormat.format(new Date()));Java是对大小写敏感的,写程序一定要注意。
该为:time=new Label();注意变量的作用域!!!