摘自:
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. 控件不响应OnMouseMove

    原文链接: http://bbs.csdn.net/topics/370017205 问:我继承CStatic写了个CStaticEx,设为Rectangle类型,用来画图.把这个控件添加到了一个Vi ...

  2. Snail—OC学习之类别Category

    类别就是向类加入一些实用的功能或者方法 利于开发 类能够是系统类.能够是自己定义类 类别跟子类是不一样的.类别仅仅能加入一些方法 属性变量什么的不能够加入 不创建新类,就可以对已有类进行扩展 做项目的 ...

  3. Ext.net控件调整后台事件、方法论

    一.以ext.net的button为例调用后台事件: 前台代码: <ext:Button ID="Button1" runat="server" Text ...

  4. 微服务架构的进程间通信(IPC)

    先抛出几个问题: 微服务架构的交互模式有哪些? 微服务常用的进程间通信技术有哪些? 如何处理部分请求失败? API的定义需要注意的事项有哪些 微服务的通信机制与SOA的通信机制之间的关系与区别 微服务 ...

  5. scratch资源

    火柴人工作室:https://scratch.mit.edu/studios/170355/ 像素工作室:https://scratch.mit.edu/studios/3667398/ snap官网 ...

  6. [k8s]如何处理dockerfile无expose情况下在k8s里暴漏访问

    做镜像时候忘记expose端口了, 或者要做一个通用的镜像, expose端口不固定, 又要在k8s环境里跑并暴漏服务访问,怎么破? 实际上: yaml的 ports: - containerPort ...

  7. Cocos2d-x和时间有关的代码

    用cocos2d-x获取系统时间,格式为年月日时分秒: void GetTime(float dt) { struct tm *tm; #if (CC_TARGET_PLATFORM == CC_PL ...

  8. How to Enable EPEL Repository for RHEL/CentOS 7.x/6.x/5.x

    What is EPEL EPEL (Extra Packages for Enterprise Linux) is open source and free community based repo ...

  9. Oracle PLSQL Demo - 20.弱类型REF游标[没有指定查询类型,也不指定返回类型]

    declare Type ref_cur_variable IS REF cursor; cur_variable ref_cur_variable; v_ename ); v_deptno ); v ...

  10. 安卓测试之---Monkey

    Tip:在阅读此篇之前,请先了解ADB命令: http://www.cnblogs.com/pearl07/p/8572390.html 一.什么是Monkey Monkey是Android SDK提 ...