分类：电脑/网络-程序设计-JAVA相关
问题：朋友们帮我看看这个程序，该怎样改正才能让点击另一个按钮时不再进行? 
package c;import java.awt.*;import java.awt.event.*;import java.applet.*;public class Counter1 extends Applet {  private int count = 0;  private Button     onOff = new Button("Toggle"),    start = new Button("Start");  private TextField t = new TextField(10package c;import java.awt.*;import java.awt.event.*;import java.applet.*;public class Counter1 extends Applet {  private int count = 0;  private Button     onOff = new Button("Toggle"),    start = new Button("Start");  private TextField t = new TextField(10);  private boolean runFlag = true;  public void init() {    add(t);    start.addActionListener(new StartL());    add(start);    onOff.addActionListener(new OnOffL());    add(onOff);  }  public void go() {    while (true) {      try {        Thread.currentThread().sleep(100);      } catch (InterruptedException e){}      if(runFlag)         t.setText(Integer.toString(count++));    }  }  class StartL implements ActionListener {    public void actionPerformed(ActionEvent e) {      go();    }  }  class OnOffL implements ActionListener {    public void actionPerformed(ActionEvent e) {      runFlag = !runFlag;    }  }  public static void main(String[] args) {    Counter1 applet = new Counter1();    Frame aFrame = new Frame("Counter1");    aFrame.addWindowListener(      new WindowAdapter() {        public void windowClosing(WindowEvent e) {          System.exit(0);        }      });    aFrame.add(applet, BorderLayout.CENTER);    aFrame.setSize(300,200);    applet.init();    applet.start();    aFrame.setVisible(true);  }} ///:~
回答：一个线程应该是没法实现吧，而且你也好像没有用到线程，这个需要2个线程来实现，如下，添加了3个类，PrimeThread中重载run方法，ExtendThread用来停止计数，而Seq则是个用来线程之间传递值的容器。通过点击Start按钮启动线程，Toggle按钮则停止线程，再按Toggle则重新将runFlag设置为true，Start按钮再启动2个线程继续计数。package c14;import java.awt.*;import java.awt.event.*;import java.applet.*;public class Counter1 extends Applet {  private Button onOff = new Button("Toggle"), start = new Button("Start");  private TextField t = new TextField(10);  private PrimeThread pt;  private ExtendThread et;  public void init() {    add(t);    start.addActionListener(new StartL());    add(start);    onOff.addActionListener(new OnOffL());    add(onOff);  }  public void go() {    pt = new PrimeThread(t);    et = new ExtendThread();    pt.start();    et.start();  }  class StartL implements ActionListener {    public void actionPerformed(ActionEvent e) {      go();    }  }  class OnOffL implements ActionListener {    public void actionPerformed(ActionEvent e) {      stop();    }  }  public void stop() {    et.stoped();  }public static void main(String[] args) {Counter1 applet = new Counter1();Frame aFrame = new Frame("Counter1");aFrame.addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent e) {System.exit(0);}});aFrame.add(applet, BorderLayout.CENTER);aFrame.setSize(300,200);applet.init();applet.start();aFrame.setVisible(true);}}class PrimeThread extends Thread {    private TextField t;  public PrimeThread(TextField t) {    this.t = t;  }    public synchronized void run() {    while(Seq.getInstance().getRunFlag()) {      try {        sleep(100);      } catch (InterruptedException e) {        e.printStackTrace();      }      t.setText("" + Seq.getInstance().getNumber());    }        if (!Seq.getInstance().getRunFlag()) {      suspend();    }  }}class ExtendThread extends Thread {  public void stoped() {    Seq.getInstance().setRunFlag(!Seq.getInstance().getRunFlag());  }}class Seq{  private static int number = 0;  private static boolean runFlag = true;   private static Seq seq = new Seq();   private Seq() {}   public static Seq getInstance(){     return seq;   }  public int getNumber(){     number++;    return number;  }  public boolean getRunFlag() {    return runFlag;  }  public void setRunFlag(boolean flag) {    runFlag = flag;  }}