import java.awt.*;
import java.awt.event.*;
public class TestWindowEvent {
public static void main (String args[]) {
new MyFrame("WindowEvent test");
}
}
class MyFrame extends Frame {
MyFrame(String s) {
super(s);
setLayout(null);
this.setBackground(new Color(200,222,255)); //设置窗口背景色(色值范围0-255)
setBounds(50,50,300,200);
setVisible(true);
this.addWindowListener(new Monitor()); //Monitor要实现WindowListener接口
/*====匿名类(不是一个类,但是当做WindowAdapter类来使用====//
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e) {
setVisible(false);
System.exit(0);
}
});
//===但是上面的 【this.addWindowListener(new Monitor());】去掉,则等效
*/
}
class Monitor extends WindowAdapter { //但是WindowListener接口有太多方法,避免重写,所以继承接口的子类
public void windowClosing(WindowEvent e) { //然后只需要实现对应的方法即可(关闭窗口)
setVisible(false); //设置窗口不可见
System.exit(0); //0是正常退出,-1是非正常退出
}
}
}
GUI的windowEvent事件(实现关闭窗口)
最新推荐文章于 2024-04-25 15:59:30 发布