import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class Example extends Applet
implements ActionListener{ // #1
public static void main(String[] args){
Frame win=new Frame(?xample");
Example example=new Example();
win.add(?enter", example);
example.init(); // #2
win.setSize(600,360);
win.setVisible(true);
}
public void init(){ // #3
Button button;
Label label;
button=new Button(" OK "):
button.setBounds(280,200,100,20);
button.addActionListener(this);
label=new Label();
label.setBounds(260,100,200,20);
add(button);
add(label);
}
public void actionPerformed(ActionEvent e){
Object source=e.getSource();
if(source==button){
label.setText(" It is a example !");
}
}
}
----在程序中的#1处,必须让程序继承java.applet.Applet类这是做为Applet的必要条件,在#2处,需要生成本类的一个实例example,通过此实例来调用init()方法,#3是init()方法。
----如果把该程序看作Applet,它只是覆盖了init()方法和actionPerformed()方法来响应Button事件。如果作为Application,则由main()方法开始,先生成程序本身的实例将程序加入窗口,然后调用init()方法。编译此程序,即可以在提示下作为Application直接运行,又可以作为Applet送入任何浏览器。
///////////////////////////////
大连博涵咨询服务有限公司 吕东兵








