当前位置: 代码迷 >> Eclipse >> 这个线程如何终止,或者是帮忙完善一下程序,求
  详细解决方案

这个线程如何终止,或者是帮忙完善一下程序,求

热度:83   发布时间:2016-04-23 13:57:09.0
这个线程怎么终止,或者是帮忙完善一下程序,求高手指点,
Java code
package q;import java.util.*;import java.awt.*;import java.awt.event.*;import java.applet.*;public class q 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) {        q applet = new q();        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);    }} // /:~


------解决方案--------------------
我觉得你应该先弄个窗口出来,然后,在上面添加你需要的组件,
再添加一句“setDefaultCloseOeration(JFrame.EXIT_ON_CLOSE)”
  相关解决方案