因为他不是application,而是一个applet你需要把它编译后放在一个html文件中

解决方案 »

  1.   

    但是为什么以下这个相似的程序(也是applet)就可以运行呢?package counter3;
    import java.awt.*;
    import java.awt.event.*;
    import java.applet.*;
    public class Counter3
    extends Applet implements Runnable {
    private int count = 0;
    private boolean runFlag = true;
    private Thread selfThread = null;
    private Button
    onOff = new Button("Toggle"),
    start = new Button("Start");
    private TextField t = new TextField(10);
    public void init() {
    add(t);
    start.addActionListener(new StartL());
    add(start);
    onOff.addActionListener(new OnOffL());
    add(onOff);
    }
    public void run() {
    while (true) {
    try {
    selfThread.sleep(100);
    } catch (InterruptedException e){}
    if(runFlag)
    t.setText(Integer.toString(count++));
    }
    }
    class StartL implements ActionListener {
    public void actionPerformed(ActionEvent e) {
    if(selfThread == null) {
    selfThread = new Thread(Counter3.this);
    selfThread.start();
    }
    }
    }
    class OnOffL implements ActionListener {
    public void actionPerformed(ActionEvent e) {
    runFlag = !runFlag;
    }
    }
    public static void main(String[] args) {
    Counter3 applet = new Counter3();
    Frame aFrame = new Frame("Counter3");
    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.   

    Swing规定你要将所有的组件加到content pane,调用add()之前先调用getContentPane()
    public void init() 
     Container cp=getContentPane();
     cp.setLayout(new FlowLayout());
     cp.add(t);
     start.addActionListener(new StartL());
     cp.add(start);
     onOff.addActionListener(new OnOffL());
     cp.add(onOff);
      

  3.   

    btw:这个好像是thinking in java里面多线程的例子:)