摘自:http://www.ntu.edu.sg/home/ehchua/programming/java/J4a_GUI.html

Refer to the WindowEventDemo, a WindowEvent listener is required to implement the WindowListener interface,which declares 7 abstract methods. Although we are only interested in windowClosing(), we need to provide an empty body to the other 6 methods in order to compile the program. This is tedious. 当实现例如WindowListener接口的时候,虽然只需要用到其中的一个函数,但是却需要将其他的函数都写出,这样非常多余。
An adapter class called WindowAdapter is therefore provided, which implements the WindowListener interface and provides default implementations to all the 7 abstract methods. You can then derive a subclass from WindowAdapter and override only methods of interest and leave the rest to their default implementation.有了adapter类之后,就只需要写出需要实现的方法就行,其他不需要使用的函数就不用写出。import java.awt.*;
import java.awt.event.*;// An AWT GUI program inherits the top-level container java.awt.Frame
public class WindowEventDemoAdapter extends Frame {private TextField tfCount;private int count = 0;/** Constructor to setup the GUI */public WindowEventDemoAdapter () {setLayout(new FlowLayout());add(new Label("Counter"));tfCount = new TextField("0", 10);tfCount.setEditable(false);add(tfCount);Button btnCount = new Button("Count");add(btnCount);btnCount.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent evt) {++count;tfCount.setText(count + "");}});// Allocate an anonymous instance of an anonymous inner class// that extends WindowAdapter.// "this" Frame adds the instance as WindowEvent listener.addWindowListener(new WindowAdapter() {@Overridepublic void windowClosing(WindowEvent e) {System.exit(0);  // Terminate the program
         }});setTitle("WindowEvent Demo");setSize(250, 100);setVisible(true);}/** The entry main method */public static void main(String[] args) {new WindowEventDemoAdapter();   // Let the constructor do the job
   }
}Similarly, adapter classes such as MouseAdapter, MouseMotionAdapter, KeyAdapter, FocusAdapter are available for MouseListener, MouseMotionListener, KeyListener, and FocusListener, respectively.There is no ActionAdapter for ActionListener, because there is only one abstract method (i.e. actionPerformed()) declared in the ActionListener interface. This method has to be overridden and there is no need for an adapter.

Event Listener's Adapter Classes相关推荐

  1. 关于事件监听机制的总结(Listener和Adapter)

    记得以前看过事件监听机制背后也是有一种设计模式的.(设计模式的名字记不清了,只记得背后实现的数据结构是数组.) 附上事件监听机制的分析图: 一个事件源可以承载多个事件(只要这个事件源支持这个事件就可以 ...

  2. [Violation] Added non-passive event listener to a scroll-blocking ‘mousewheel‘ event.

    在基于 Element-ui 写项目的时候,Chrome 提醒: [Violation] Added non-passive event listener to a scroll-blocking ' ...

  3. 解决移动端报错:Unable to preventDefault inside passive event listener due to target being treated as……

    解决移动端报错:Unable to preventDefault inside passive event listener due to target being treated as-- 报错如下 ...

  4. MAP地图报错Unable to preventDefault inside passive event listener invocation.

    地图每次鼠标点击.移入移出都会报 Unable to preventDefault inside passive event listener invocation. 不管是对接什么地图都会报错 原因 ...

  5. 解决警告:Unable to preventDefault inside passive event listener due to target being treated as passive.

    注: 1.滑动或多次点击时警告[Intervention] Unable to preventDefault inside passive event listener due to target b ...

  6. Unable to preventDefault inside passive event listener due to target being treated as passive.

    最近做项目经常在 chrome 的控制台看到如下提示: Unable to preventDefault inside passive event listener due to target bei ...

  7. 滑动报错 Unable to preventDefault inside passive event listener due to target being treated as passive.

    app开发,滑动时报错 Unable to preventDefault inside passive event listener due to target being treated as pa ...

  8. React中使用antd的select报错Unable to preventDefault inside passive event listener invocation.

    问题描述 报错Unable to preventDefault inside passive event listener invocation. React中使用antd的select报错 Unab ...

  9. 【关于echarts使用所遇问题01】---- Unable to preventDefault inside passive event listener invocation

    项目场景: Echarts区域缩放(datazoom)鼠标滚动报错:Unable to preventDefault inside passive event listener invocation ...

  10. vue-admin-elem对接地图报错Unable to preventDefault inside passive event listener invocation.

    问题: 我的项目对接的地图每次鼠标点击.移入移出都会报 Unable to preventDefault inside passive event listener invocation. 不管是对接 ...

最新文章

  1. 【云计算的1024种玩法】回忆经典,用虚拟主机重建复古DZ和无心宠物
  2. MCSE2003学习之三
  3. $.ajax居然触发popstate事件?
  4. 大小端字节序介绍以及判断当前环境字节序的程序【C语言】
  5. Solaris 9安装VNC
  6. [CSS] Scale on Hover with Transition
  7. 用session实现html登录页面跳转页面跳转页面跳转,js判断登录与否并确定跳转页面的方法...
  8. AlphaFold2立功!清华团队用深度学习增强新冠抗体,创AI里程碑
  9. WinCE的开发流程
  10. sql 2008 每次打开一个表都要登录_如何实现一个简易的orm
  11. Oracle Dataguard 管理命令
  12. html中rem和em,CSS 中的 rem 和 em 的区别(1)
  13. 石头记特定卡密生成获取get!{石头记卡密使用方法}
  14. 使用cmd结束进程的3种方法
  15. Python车牌识别、车牌抓取源代码
  16. 基于MATLAB小波变换的医学图像分割的研究
  17. Docker——数据卷的概述和使用
  18. Flink1.10.1编译hadoop2.7.2 编译flink-shaded-hadoop-2-uber
  19. office图标无法正常显示
  20. 独立性与互不相容的区别

热门文章

  1. Latex:图片排版的位置参数【htbp】
  2. mysql sleep进程 java_请教java更新mysql,更新进程sleep
  3. 航天有关的计算机知识,这些关于神舟十二上的电脑硬核小知识,你知道吗?
  4. python不可变变量_python的可变变量和不可变变量
  5. 差异基因p为0_【i春秋杯网络安全联赛WriteUp】为逆行者加油
  6. WebSocket 协议 RFC 文档(全中文翻译) 1
  7. Kubernetes 持久化存储是个难题,解决方案有哪些?\n
  8. 客户端(vue框架)与服务器(koa框架)通信及服务器跨域配置详解
  9. protobuf java学习
  10. 【SpringBoot_ANNOTATIONS】自动装配 04 Aware 注入Spring底层组件 原理