Listener监听器一

来源:互联网 发布:java全文检索 编辑:程序博客网 时间:2024/05/01 21:08
1 事件三要素
  a)事件源:操作事件的对象,例如:窗体Frame
  b)事件监听器:事件监听器监听事件源,例如WindowListner,它是一个接口  
  c)事件,例如:单击事件,通过事件,可以取得事件源

2 适配器模式
  a)当一个接口有较多的方法时,而实现类只需对其中少数几个实现,此时可以使用适配器模式

  b)适配器模式常用于GUI编程

package cn.itcast.web.gui;

import java.awt.Frame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;


/**
 * 事件三要素:
 * a:事件源:操作事件 的对象,例如:窗体Fram
 * a)事件监听器:事件 监听器监听事件源,例如:windowsListner,它是一个接口
 * c)事件,例如:单击事件,通过事件,可以取得事件源
 *@author yi.xiaoqun
 *@创建日期2013-12-9
 * @版本 V1.0
 */
//GUI(图形用户界面)
public class Demo1 {
    public static void main(String[] args){
        //创建一个不可创建的窗体
        Frame frame=new Frame("我的窗体");
        frame.setSize(200,250);
        //设置窗体显示的任性
        frame.setLocation(300, 200);
        //将窗体显示出来
        frame.setVisible(true);
        //为窗体添加事件【事件监听器,监听事件源】
        //frame.addWindowListener(new MyWindowListener());
        frame.addWindowListener(new MyWindowListener2());
        
    }

}
/*//事件监听器
class MyWindowListener implements WindowListener{
    //重写父类相关的方法
    public void windowOpened(WindowEvent e) {
        // TODO Auto-generated method stub
        
    }
    //事件处理程序 ,由监听器自动调用
    public void windowClosing(WindowEvent e) {
        // TODO Auto-generated method stub
        System.out.println("windowClosing");
        //取得事件源
        Frame frame=(Frame)e.getSource();
        //隐藏事件源
        frame.setVisible(false);
        //将JVM强行关闭
        System.exit(1);
    }

    public void windowClosed(WindowEvent e) {
        // TODO Auto-generated method stub
        
    }

    public void windowIconified(WindowEvent e) {
        // TODO Auto-generated method stub
        
    }

    public void windowDeiconified(WindowEvent e) {
        // TODO Auto-generated method stub
        
    }

    public void windowActivated(WindowEvent e) {
        // TODO Auto-generated method stub
        
    }

    public void windowDeactivated(WindowEvent e) {
        // TODO Auto-generated method stub
        
    }
    
}*/
//事件监听器[扩展WindowAdapter适配器]
class MyWindowListener2 extends WindowAdapter{
    //重写父类相关的方法
    public void windowClosing(WindowEvent e) {
        System.out.println("hello");
        Frame frame = (Frame) e.getSource();
        frame.setVisible(false);
        System.exit(1);
    }
}
java web监听器

监听器的工作过程和生命周期
  开发过程:
  a)写一个普通类实现对应的接口,即事件监听器
  b)在web.xml文件中注册事件监听器
    <!-- 事件源注册事件监听器,由容器完成 -->
    <listener>
        <listener-class>cn.itcast.web.listener.MyServletContextListener</listener-class>        
      </listener>

   生命周期:
    空参构造(1次)->初始化(1次)->销毁化(1次),是一个单例的模式
     
   在部署web应用是产生,即用户第一次访问之前已经产生,在重新部署web应用时,后销毁原监听器,再产生新的监听器


package cn.itcast.web.listener;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
//事件 监听器[用于产监听ServletContext对象的产生和销毁],通过反射
public class MyServletContextListener implements ServletContextListener {
    public MyServletContextListener(){
        System.out.println("空参数!");
        System.out.println(this.hashCode());
    }
    //产生
    public void contextDestroyed(ServletContextEvent sce) {
        // TODO Auto-generated method stub
        System.out.println("ServletContext产生");
        System.out.println(this.hashCode());
    }
    //销毁
    public void contextInitialized(ServletContextEvent sce) {
        // TODO Auto-generated method stub
        System.out.println("ServletContext销毁");
        System.out.println(this.hashCode());
    }

}

web.xml的配置

<!-- 事件源注册事件监听器,由wen容器完成 -->
  <listener>
      <listener-class>cn.itcast.web.listener.MyServletContextListener</listener-class>
  </listener>