如下代码:
    init()是如何调用start()方法启动线程的?
import java.applet.*;
import java.awt.*;
public class MyThread extends Applet implements Runnable {
int x = 0,y = 5;
Thread Scrollwords = null;
public void init() {
setFont(new Font("TimeRomen",Font.BOLD,16));
}
public void start() {
if(Scrollwords==null) {
Scrollwords = new Thread(this);
Scrollwords.start();
}
}
public void run() {
while(Scrollwords!=null) {
x=x+1;
y=y+1;
if(x>200)
x=0;
if(y>100)
y=10;
repaint();
try {
Thread.sleep(20);
}
catch(InterruptedException e){e.printStackTrace();}
}
}
public void paint(Graphics g) {
g.drawString("Here is big club", x, y);
}
public void stop(){
Thread.yield();
Scrollwords = null;
}
}

解决方案 »

  1.   


    import java.applet.*; 
    import java.awt.*; public class MyThread extends Applet implements Runnable { 
        int x = 0,y = 5; 
        Thread Scrollwords = null;     public void init() { 
        setFont(new Font("TimeRomen",Font.BOLD,16)); 
        }     public void start() { 
            if(Scrollwords==null) { 
                Scrollwords = new Thread(this); 
                Scrollwords.start(); 
            } 
        }     public void run() { 
            while(Scrollwords!=null) { 
                x=x+1; 
                y=y+1; 
                if(x>200) 
                    x=0; 
                if(y>100) 
                    y=10; 
                repaint(); 
                try { 
                    Thread.sleep(20); 
                } 
                catch(InterruptedException e){e.printStackTrace();} 
            } 
        }     public void paint(Graphics g) { 
            g.drawString("Here is big club", x, y); 
        }     public void stop(){ 
            Thread.yield(); 
            Scrollwords = null; 
        } 
    }
      

  2.   

    init()
    start()
    stop()
    destroy()
    是Applet的生命周期,在init()方法后,系统自动调用start()方法,
    或者当用户从其它页面转到包含applet的页面时,该方法也被调用。