13、过滤器、监听器常见应用
监听器:GUI编程中经常使用
代码show
package com.gongyi.listener;
import javax.swing.plaf.ColorUIResource;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
/**
* @Classname TestPanel
* @Description TODO
* @Date 2021/10/23 13:14
* @Created by Administrator
*/
public class TestPanel {
public static void main(String[] args) {
Frame frame = new Frame("1024节日快乐");//新建一个窗体
Panel panel = new Panel(null);//面板
frame.setLayout(null);//设置窗体的布局
frame.setBounds(300,300,500,500);
frame.setBackground(new Color(0,0,255));//设置背景颜色
panel.setBounds(50,50,300,300);
panel.setBackground(new ColorUIResource(0,255,0));//设置背景颜色
frame.add(panel);
frame.setVisible(true);
//监听事件,监听关闭事件
frame.addWindowListener(new WindowListener() {
public void windowOpened(WindowEvent e) {
System.out.println("打开");
}
public void windowClosing(WindowEvent e) {
System.out.println("关闭ing");
System.exit(0);
}
public void windowClosed(WindowEvent e) {
System.out.println("关闭ed");
}
public void windowIconified(WindowEvent e) {
}
public void windowDeiconified(WindowEvent e) {
}
public void windowActivated(WindowEvent e) {
System.out.println("激活");
}
public void windowDeactivated(WindowEvent e) {
System.out.println("未激活");
}
});
/*frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.out.println("适配器模式关闭窗口");
super.windowClosing(e);
}
});*/
}
}
效果图:
彩蛋
1.java.awt中的监听器中的适配器设计模式
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
super.windowClosing(e);
}
});