小小问题―关于java多线程

02-09-14 wwlhp

下面是我自学多线程后自己写的第一个程式,可是在内类那里编译时出错,不知为何?

// 在此输入java代码
import java.awt.*;
import java.awt.event.*;
import java.applet.*;

 
public class Applet1 extends Applet  
{
	boolean clicked = false;
	int count;
	
	public void init()
	{
		add( new ClickCanvas( this ) );
		addMouseListener( new MouseAdapter(){
				public void mousePressed( MouseEvent me )
				{
					++count;
					synchronized( MouseAdapter.this ){ 
							clicked = true;
							MouseAdapter.this.notify();	
					}
				}	});
	}
}
 

class ClickCanvas extends Canvas implements Runnable 
{
	Applet1 applet;
	
	public ClickCanvas( Applet1 applet )
	{
		this.applet = applet;
		setBackground( Color.blue );
		setSize( 100, 100 );
		new Thread( this ).start();
	}
	
	public void run()
	{
		while( true ){
			synchronized( applet ){
				while( !applet.clicked ) 
					try{	applet.wait();
					}catch( InterruptedException ie ){}
				repaint( 250 );
				applet.clicked = false;
			}
		}
	}
				 
	public void paint( Graphics g )
	{
		g.drawString( ""+ applet.count, 10, 20 ); 
	}
}
<p>

希望您能指点错误之处,非常感谢!!!