天天看点

awt--事件适配器--比如WindowAdapter

事件适配器

1 抽象类 implements 事件监听器,但是里面具体的方法全部空实现,可以实现多个监听器

2 用户继承 事件适配器类 ,按照需要实现自己的方法。

/*  事件适配器原理
 * 
 * 事件适配器--》实现事件监听器接口
 * 
 * 但是是空实现,所有实现的方法体里面没有语句,或者就是一个分号;
 * 
 * 用户再继承事件适配器,需要哪个方法就编写哪个方法
 * 
 * 中间连接的思维   比如  WindowAdapter  实现 WindowListener  但是是空实现,不信请看源代码
 * 
 * WindowListener 实现了3个接口,为三个监听器做了适配   
 * 
 * public abstract class WindowAdapter  implements WindowListener, WindowStateListener, WindowFocusListener
 * 
 * 
 * 定义成了抽象方法,表示要想使用它,必须要继承
 * */

package awt3yue2;


import java.awt.*;
import java.awt.event.*;
public class WindowAdapterTest {
	Frame f = new Frame("事件适配器测试");
	TextArea ta = new TextArea(5,10);
	
	public void init()
	{
		f.addWindowListener(new MyListener());
		f.add(ta); //默认放在中间
		f.pack();
		f.setVisible(true);
	}
	
	//继承事件适配器
	public class MyListener extends WindowAdapter
	{
		public void windowClosing(WindowEvent e)
		{
			 ta.append("窗口关闭");
			 //System.exit(0);
			 f.setVisible(false);
		}
	}
	
	
	public static void main(String [] args)
	{
		new WindowAdapterTest().init();;
	}

}