摘自:
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. Javascript和OCX的结合历程

    这种相对古老的调用方式主要用在Windows系列平台上的多种语言软件之间的合作. 此次项目合作方式是,客户提供语音功能的OCX模块和VC++客户端软件,我需要在WEB软件中调用其中的接口开发相关功能, ...

  2. scikit-learn 入门练习

    1. 一个简单的SVM实例: from sklearn import svm X = [[2, 0], [1, 1], [2,3]] y = [0, 0, 1] clf = svm.SVC(kerne ...

  3. 为anaconda的jupyter notebook设置初始化目录

    在使用jupyter进行编程时,初始化目录可能不是自己想要的目录,那么下面讲解修改成自己想要的目录. 1) 在命令行中输入jupyter notebook --generate-config,会产生一 ...

  4. 关于spring xml文件中的xmlns,xsi:schemaLocation(转)

    使用spring也有一段时间了,配置文件也见了不少了,但是发现配置文件的beans里面有很多链接,一开始也很迷惑,所以抽了一点时间整里了一下. 首先我们看到的一个spring的配置文件大概如下面这个样 ...

  5. Oracle学习笔记之四,SQL语言入门

    1. SQL语言概述 1.1 SQL语言特点 集合性,SQL可以的高层的数据结构上进行工作,工作时不是单条地处理记录,而对数据进行成组的处理. 统一性,操作任务主要包括:查询数据:插入.修改和删除数据 ...

  6. removeChildByTag、schedule、schedule_selector

    Test4::Test4() { CCSprite *sp1 = CCSprite::create(s_pPathSister1); CCSprite *sp2 = CCSprite::create( ...

  7. You have JVM property "https.proxyHost" set to “localhost”

    Mac下Pycharm和AndroidStudio里面proxy配置页都提示这个,后来在~/.gradle/gradle.properties里面找到了proxy设置代码,删掉就好了.

  8. 移动端自动化测试 -- appium 之Desired Capabilities与 定位控件

    一.Desired Capabilities Desired Capabilities 在启动 session 的时候是必须提供的. Desired Capabilities 本质上是以 key va ...

  9. 每日英语:Mercedes Sheds Its 'Old' Design

    Daimler AG's flagship Mercedes-Benz often gets the rap for being an 'old man's car.' More unusual is ...

  10. Oracle PLSQL Demo - 24.分隔字符串function

    -- refer: -- http://www.cnblogs.com/gnielee/archive/2009/09/09/1563154.html -- http://www.cnblogs.co ...