摘自:
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() {
@Override
public 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() {
@Override
public 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. Android事件监听器Event Listener

    在 Android 中,我们可以通过事件处理使UI与用户互动(UI Events). UI的用户事件处理,即View处理用户的操作,在应用程序中几乎不可避免.View是重要的类,它是与用户互动的前线: ...

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

    Unable to preventDefault inside passive event listener due to target being treated as passive 今天在做项目 ...

  4. Unable to preventDefault inside passive event listener

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

  5. 滑动时候警告:Unable to preventDefault inside passive event listener

    1 前言 在制作2048时,需要在手机端添加滑动检测事件,然后发现控制台有警告,如下: main2048.js:218 [Intervention] Unable to preventDefault ...

  6. 移动端页面滑动时候警告:Unable to preventDefault inside passive event listener due to target being treated as passive.

    移动端项目中,在滚动的时候,会报出以下提示: [Intervention] Unable to preventDefault inside passive event listener due to ...

  7. [Intervention] Unable to preventDefault inside passive event listener due to target being treated as passive. See https://www.chromestatus.com/features/5093566007214080

    相信如果用谷歌浏览器做移动端页面的时候 用touch事件的时候应该遇到过这个东东吧 documet.addEventListener("touchstart",function() ...

  8. [Intervention] 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 being tr ...

  9. Presto Event Listener开发

    简介 同Hive Hook一样,Presto也支持自定义实现Event Listener,用于侦听Presto引擎执行查询时发生的事件,并作出相应的处理.我们可以利用该功能实现诸如自定义日志记录.调试 ...

随机推荐

  1. oracle排序使用,很多中函数,不同的效果

    参考原文:http://blog.csdn.net/wanglipo/article/details/6954915 row_number() OVER (PARTITION BY COL1 ORDE ...

  2. unity 查看prefab层次

    点那个箭头,可以展开:

  3. 如何用adb抓取log?

    在Androidclient的測试过程中,有时候我们会遇到闪退等异常情况. 这时我们能够通过adb抓取log.从而给开发提供很多其它信息. 一.下载ADB.exe     在网上搜索"adb ...

  4. C# xml可序列化多值枚举脚本

    代码: using System; using System.Collections.Generic; using System.Xml; using System.Xml.Schema; using ...

  5. HTML解析HtmlAgility学习

    HtmlAgility是一个开源的Html解析库,据说是C#版的JQuery,功能非常强大. 该篇学习它的解析功能,还可以模拟用户请求,创建html,设置代理等等,暂先不研究. ----------- ...

  6. MySQL抓包工具:MySQL Sniffer 和性能优化

    简介 MySQL Sniffer 是一个基于 MySQL 协议的抓包工具,实时抓取 MySQLServer 端的请求,并格式化输出.输出内容包访问括时间.访问用户.来源 IP.访问 Database. ...

  7. CLR Debug extention

    https://github.com/Microsoft/clrmd https://www.nuget.org/packages/Microsoft.Diagnostics.Runtime http ...

  8. ubantu 文件系统的目录结构

      对于每一个Linux学习者来说,了解Linux文件系统的目录结构,是学好Linux的至关重要的一步.,深入了解linux文件目录结构的标准和每个目录的详细功能,对于我们用好linux系统只管重要, ...

  9. GNU风格 ARM汇编语法1

    汇编源程序一般用于系统最基本的初始化:初始化堆栈指针.设置页表.操作 ARM的协处理器等. 这些初始化工作完成后就可以跳转到C代码main函数中执行. 1.GNU汇编语言语句格式 任何Linux汇编行 ...

  10. 解决linux分区提示doesn't contain a valid partition table

    目前 partition table 大概有叁种: 最传统的 mbr.大容量的 gpt.小设备的 无; 遇上最后那种就会出现 "doesn't contain a valid partiti ...