我照书写了一个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()已过时,该用什么方法代替呢??

解决方案 »

  1.   

    改成我这样就好了:
    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);
      }}
      

  2.   

    运行的时候稍微等待几秒才会看到时间,可能反应比较慢吧。我没用stop,和你一样没找到替代的方法。