为什么我写的applet老是不能得到想要的显示
我照书写了一个applet,运行后不能得到显示,这是一个线程示例.代码如下:
html代码如下:
<html>
<head>
<title> </title>
</head>
<body>
<applet code= "DigitalClock.class " width=400 height=400>
</applet>
</body>
</html>
java applet代码如下:
import java.awt.Graphics;
import java.awt.Font;
import java.util.Date;
public class DigitalClock extends java.applet.Applet
implements Runnable{
Font theFont =new Font( "TimesRoman ",Font.BOLD,24);
Date theDate;
Thread runner;
public void start(){
if(runner==null)runner=new Thread(this);
runner.run();
}
public void stop(){
if(runner!=null){
runner.stop();
runner=null;
}
}
public void run(){
while(true){
theDate=new Date();
repaint();
try{Thread.sleep(1000);}
catch(InterruptedException e){}
}
}
public void paint(Graphics g){
g.setFont(theFont);
g.drawString(theDate.toString(),100,100);
}
}
请高手指点 如果可以的话,还有一个问题就是Thread中的stop()已过时,该用什么方法代替呢??
------解决方案--------------------改成我这样就好了:
import java.awt.Graphics;
import java.awt.Font;
import java.util.Date;
public class DigitalClock extends java.applet.Applet
implements Runnable{
Font theFont =new Font( "TimesRoman ",Font.BOLD,24);
Date theDate;
Thread runner;
public void run()
{
while(true)
{
theDate = new Date();
try
{
Thread.sleep(1000);
}
catch(Exception e)
{
e.printStackTrace();
}
repaint();
}
}
public void init()
{
runner = new Thread(this);
runner.start();
}
public void paint(Graphics g){
g.setFont(theFont);
g.drawString(theDate.toString(),100,100);
}
}