- 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)”