[java]代码库
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class AppletApp extends Applet {
public void main(String args[]) {
Frame frame = new Frame("Application"); // 构造一个Frame
AppletApp app = new AppletApp();
frame.add("Center", app);
frame.setSize(200, 200); // 改变Frame的尺寸
frame.validate();
frame.setVisible(true); // 使Frame可见
frame.addWindowListener(new WindowControl(app));
// 给Frame加入监听器
app.init(); // 初始化Applet
app.start(); // 运行该Applet
}
public void paint(Graphics g) { // 重画方法
g.drawString("hello world", 25, 25);
}
public void destroy() {
System.exit(0);
}
}
class WindowControl extends WindowAdapter { // 监听器类
Applet c;
public WindowControl(Applet c) { // 构造函数
this.c = c;
}
public void windowClosing(WindowEvent e) {
// 关闭窗口时调用的方法
c.destroy();
}
}