import java.applet.*;   //有一处错误不会改
import java.awt.*;
import java.awt.event.*;
public class Example19_7 extends Applet implements ActionListener
{
Toolkit toolkit;
Button button;
public void init()
{
toolkit=getToolkit();
button=new Button("确定");
add(button);
button.addActionListener(this);
}
public void actoinPerformed(ActionEvent e)
{
if(e.getSource()==button)
{
for(int i=0;i<=9;i++)
{
toolkit.beep();
try{
Thread.sleep(500);
}
catch(InterruptedException e1){}
}
}
}
}
第二,有三处错误
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
class shouhuoyuan
{
int number1=2,number2=0,number3=0;String s=null;
public synchronized void shoupiaoguize(int money)
{
if(money==5)
{
number1=number1+1;
s="给您入场券您的钱正好";
Example19_8.text.append("\n"+s);//必须用Example19_8?
}
else if(money==20)
{
while(number1<3)
{
try{
wait();
}
catch(InterruptedException e){}
}
number1=number1-3;         //这个时候number1是0还是-1呢,执行顺序是怎样的呢
number2=number2+1;
s="给您入场券"+" 您给我20,找您15";
Example19_8.text.append("\n"+s);
}
notifyAll();
}
}
public class Example19_9 extends Applet implements Runnable
{
shouhuoyuan wang;
Thread zhang,li;
static TextArea text;//为什么要static呢
public void init()
{
zhang=new Thread(this);
li=new Thread(this);
text=new TextArea(10,30);add(text);
wang=new shouhuoyuan();
}
public void start()
{
zhang.start();li.start();
}
public void run()
{
if(Thread.currentThread()==zhang)
{
wang.shoupiaoguize(20);
}
else if(Thread.currentThread()==li)
{
wang.shoupiaoguize(5);
}
}
}
第三  
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class Example19_11 extends Applet implements Runnable,ActionListener
{
Button b=new Button("go");TextField text=null;
Thread fa,yun1,yun2;
int x=10;
Graphics mypen=null;
public void init()
{
b.addActionListener(this);text=new TextField(20);
fa=new Thread(this);yun1=new Thread(this);yun2=new Thread(this);
add(b);add(text);
mypen=getGraphics();
}
public void start()
{
fa.start();
}
public void actionPerformed(ActionEvent e)  //怎么执行?什么时候执行?
{
fa.interrupt();
}
public void run()
{
if(Thread.currentThread()==fa)
{
while(true)
{
text.setText("准备跑.......");   
try{
fa.sleep(30);
}
catch(InterruptedException e)     //怎么执行?什么时候执行?
{
text.setText("跑");
yun1.start();break;
}
}
}
if(Thread.currentThread()==yun1)
{
while(true)
{
x=x+1;
mypen.setColor(Color.blue);
mypen.clearRect(10,80,99,100);
mypen.fillRect(x,85,5,5);
try{
yun1.sleep(10);
}
catch(InterruptedException e)
{
yun2.start();return;        //返回到哪
}
if(x>=100)
{
yun1.interrupt();    //什么意思
}
}
}
if(Thread.currentThread()==yun2)
{
while(true)
{
x=x+1;
mypen.setColor(Color.red);
mypen.clearRect(105,80,150,100);
mypen.fillRect(x+5,85,7,7);
try{
yun2.sleep(10);
}
catch(InterruptedException e)
{
text.setText("到达终点");return;
}
if(x>=200)
{
yun2.interrupt();
}
}
}
}
}

解决方案 »

  1.   

    第一处错误在于
    public void actoinPerformed(ActionEvent e)action 拼错了
      

  2.   

    第二 
    text.append("\n"+s);//必须用Example19_8?
    static TextArea text;//为什么要static呢
    这两个要一起回答,首先应该是Example19_9, 正因为text定义为static所以才能用class的名字直接调用,而不用建立它的实例,而且可以被所有的实例公用。number1=number1-3;         //这个时候number1是0还是-1呢,执行顺序是怎样的呢
    这个部分,程序一直在等,等number1>=3时才执行,所以 number1 = 3-3 = 0