自定义窗口的关闭事件:
给窗口实例添加一个监听,然后定义一个windowClosing(不是WindowClosed)函数,具体的窗口关闭事件响应就写在这个函数中,下面从其他网友页面上摘抄了一个简单的实例。
import java.awt.*;
import java.awt.event.*;
public class Testscoll {
public Testscoll() {
Frame f = new Frame("javascollpane");
ScrollPane scr = new ScrollPane();
TextArea text = new TextArea("", 50, 50, TextArea.SCROLLBARS_NONE);
scr.add(text);
f.add(scr);
f.setSize(60, 60);
f.setVisible(true);
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {// <--不是windowClosed,是windowClosing
((Window) e.getComponent()).dispose();
System.out.print("Closing");
System.exit(0);
}
});
}
public static void main(String[] args) {
new Testscoll();
}
}