Java--WindowListener 窗体事件监听
程序员文章站
2022-04-27 19:45:11
...
WindowListener 窗体事件监听
package com.lddx.day0307;
import java.awt.Frame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.JFrame;
//WindowListener 窗体事件监听
public class TestWindowListener {
public static void main(String[] args) {
TestWindowListener wl=new TestWindowListener();
wl.init();
}
Frame f=new Frame(); //创建窗体Frame
public void init(){
//JFrame f=new JFrame();//创建窗体JFrame
//绑定监听器
f.addWindowListener(w);
//绑定适配器
f.addWindowListener(wa);
f.setTitle("How are you!");
f.setSize(600, 500);
f.setLocation(500, 300);
f.setVisible(true);
//f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 该方法可以用JFrame调用;
}
//定义匿名类 -- 实现WindowListener监听器 可以实现多个接口
WindowListener w=new WindowListener(){
//窗口被打开时的监听
public void windowOpened(java.awt.event.WindowEvent e) {
System.out.println("窗口被打开啦!");
};
//窗口被关闭时的监听
public void windowClosed(java.awt.event.WindowEvent e) {
System.out.println("窗口被关闭啦!");
};
//点击窗口关闭按钮监听
public void windowClosing(java.awt.event.WindowEvent e) {
System.out.println("点击窗口关闭按钮啦!");
//System.exit(0);//结束程序
f.dispose();//该方法关闭窗体,同时也会触发WindowClosed;
};
//窗口被**时的监听
public void windowActivated(java.awt.event.WindowEvent e) {
System.out.println("窗口被**啦!");
};
//窗口失去活性的监听
public void windowDeactivated(java.awt.event.WindowEvent e) {
System.out.println("窗口失去活性啦!");
};
//窗口从最小化还原时监听
public void windowDeiconified(java.awt.event.WindowEvent e) {
System.out.println("窗口从最小化还原啦!");
};
//窗口被最小化时监听
public void windowIconified(java.awt.event.WindowEvent e) {
System.out.println("窗口被最小化啦!");
};
};
//定义匿名类 -- 继承WindowAdatper适配器 不能多继承
WindowAdapter wa=new WindowAdapter(){
//窗口被打开时的监听
public void windowOpened(java.awt.event.WindowEvent e) {
System.out.println("窗口又被打开啦!");
};
};
}
上一篇: 搞笑冷笑话