import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class Example extends Applet implements ActionListener { // #1
    Button button;
    Label label;
    public void actionPerformed(ActionEvent e)
{ //#4
        Object source = e.getSource();
        if (source == button)
        {
            label.setText(" It is a example !");
        }
    }
    public void init()
{ // #3
        button = new Button(" OK ");
        button.setBounds(280, 200, 100, 20);
        button.addActionListener(this);
        //Label=new Label();
        label = new Label();
        label.setBounds(260, 100, 200, 20);
        add(button);
        add(label);
    }
    public static void main(String[] args)
{
        Frame win = new Frame("Example");
        Example example = new Example();
        win.add("enter", example);
        example.init(); // #2
        win.setSize(600, 360);
        win.setVisible(true);
    }
}

解决方案 »

  1.   

    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;
    public class Example extends Applet implements ActionListener{ // #1

     Button button;
         Label label;     public static void main(String[] args){
            Frame win=new Frame("Example");
            Example example=new Example();
            win.add("enter", example);
            example.init(); // #2
            win.setSize(600,360);
            win.setVisible(true);
         }     public void init(){ // #3       
           button=new Button(" OK ");
           button.setBounds(280,200,100,20);
           button.addActionListener(this);
           //Label=new Label();
           label=new Label();
           label.setBounds(260,100,200,20);
           add(button);
           add(label);
         }     public void actionPerformed(ActionEvent e){ //#4
            Object source=e.getSource();
            if(source == button){
              label.setText(" It is a example !");
            }
         }
    }
      

  2.   

    谢谢楼上各位!现在又有如下问题:
       javac Example.java通过了,但是用java Example时报了一个cannot add to layout :unknow constraint :enter.
      

  3.   

    1、你这个是Applet,应该用AppletView。
    2、main里这写
    Frame win = new Frame("Example");
    win.setLayout(new BorderLayout());
    Example example = new Example();
    win.add(example, "Center");
    example.init(); // #2
    win.setSize(600, 360);
    win.setVisible(true);
    如果你一定要运行main的话
      

  4.   

    对,谢谢!
    顺利通过了,现在就是点击OK按钮时,Label上没有显示It is a example!.
    并且点能最小化和最大化,就是不能点差退出!
    在次感谢各位高手!
      

  5.   

    那是刷新的问题,
         public void actionPerformed(ActionEvent e){ //#4
            Object source=e.getSource();
            if(source == button){
              label.setText(" It is a example !");          //这里
              this.validate();
            }
         }
      

  6.   

    Frame win = new Frame("Example");
    win.addWindowListener(new WindowAdapter()
    {
    public void windowClosing(WindowEvent e)
    {
    System.exit(0);
    }
    });
    win.setLayout(new BorderLayout());这个也要偶写出来 :(
      

  7.   

    谢谢up,现在这个程序很完美了,就是刷新this.validate();没有起到作用一样,label还是不能显示出
    It is a example !
    请在指教!
      

  8.   

    不会吧,我用怎么没问题呢?
    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;
    public class Example extends Applet implements ActionListener { // #1
    Button button;
    Label label;
    public void actionPerformed(ActionEvent e)
    { //#4
    Object source = e.getSource();
    if (source == button)
    {
    label.setText(" It is a example !");
    this.validate();
    }
    }
    public void init()
    { // #3
    button = new Button(" OK ");
    button.setBounds(280, 200, 100, 20);
    button.addActionListener(this);
    //Label=new Label();
    label = new Label();
    label.setBounds(260, 100, 200, 20);
    add(button);
    add(label);
    }
    public static void main(String[] args)
    {
    Frame win = new Frame("Example");
    win.addWindowListener(new WindowAdapter()
    {
    public void windowClosing(WindowEvent e)
    {
    System.exit(0);
    }
    });
    win.setLayout(new BorderLayout()); Example example = new Example();
    win.add(example, "Center");
    example.init(); // #2
    win.setSize(600, 360);
    win.setVisible(true);
    }
    }
      

  9.   

    To relive!
      我的程序和你up的一样,但就是Label中的It is a example !显示不出来,
    帮我想想,有什么原因,谢谢!