1.事件对象: 
一般继承自java.util.EventObject对象,由开发者自行定义.

2.事件源: 
就是触发事件的源头,不同的事件源会触发不同的事件类型.

3.事件监听器: 
事件监听器负责监听事件源发出的事件.一个事件监听器通常实现java.util.EventListener这个标识接口.

其整个处理过程是这样的,事件源可以注册事件监听器对象,并可以向事件监听器对象发送事件对象.事件发生后,事件源将事件对象发给已经注册的所有事件监听器. 
监听器对象随后会根据事件对象内的相应方法响应这个事件.

以下为四种实现方式

Java代码:

package TT;

import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.RootPaneContainer;
/**
*自身类作为事件监听器 :
*/
class EventListener1 extends JFrame implements ActionListener {
private JButton btBlue, btDialog; public EventListener1() {
setTitle("Java GUI 事件监听处理");
setBounds(100, 100, 500, 350);
setLayout(new FlowLayout());
btBlue = new JButton("蓝色");
btDialog = new JButton("弹窗"); // 将按钮添加事件监听器
btBlue.addActionListener(this);
btDialog.addActionListener(this); add(btBlue);
add(btDialog); setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
} // ***************************事件处理***************************
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btBlue) {
Container c = getContentPane();
c.setBackground(Color.BLUE);
} else if (e.getSource() == btDialog) {
JDialog dialog = new JDialog();
dialog.setBounds(300, 200, 400, 300);
dialog.setVisible(true);
}
} } /**
* Java事件监听处理——内部类处理
*
*/ class EventListener2 extends JFrame {
private JButton btBlue, btDialog; // 构造方法
public EventListener2() {
setTitle("Java GUI 事件监听处理");
setBounds(100, 100, 500, 350);
setLayout(new FlowLayout());
btBlue = new JButton("蓝色");
btDialog = new JButton("弹窗");
// 添加事件监听器对象(面向对象思想)
btBlue.addActionListener(new ColorEventListener());
btDialog.addActionListener(new DialogEventListener()); add(btBlue);
add(btDialog);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
} // 内部类ColorEventListener,实现ActionListener接口
class ColorEventListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
Container c = getContentPane();
c.setBackground(Color.BLUE);
}
} // 内部类DialogEventListener,实现ActionListener接口
class DialogEventListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
JDialog dialog = new JDialog();
dialog.setBounds(300, 200, 400, 300);
dialog.setVisible(true);
}
} } /**
* Java事件监听处理——匿名内部类处理
*
*/
class EventListener3 extends JFrame {
private JButton btBlue, btDialog; public EventListener3() {
setTitle("Java GUI 事件监听处理");
setBounds(100, 100, 500, 350);
setLayout(new FlowLayout()); btBlue = new JButton("蓝色");
btDialog = new JButton("弹窗"); // 添加事件监听器(此处即为匿名类)
btBlue.addActionListener(new ActionListener() {
// 事件处理
@Override
public void actionPerformed(ActionEvent e) {
Container c = getContentPane();
c.setBackground(Color.BLUE);
}
}); // 并添加事件监听器
btDialog.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JDialog dialog = new JDialog();
dialog.setBounds(300, 200, 400, 300);
dialog.setVisible(true);
}
}); add(btBlue);
add(btDialog);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
} } /**
* Java事件监听处理——外部类处理
*
*/
class EventListener4 extends JFrame {
private JButton btBlue, btDialog; public EventListener4() {
setTitle("Java GUI 事件监听处理");
setBounds(100, 100, 500, 350);
setLayout(new FlowLayout());
btBlue = new JButton("蓝色");
btDialog = new JButton("弹窗");
// 将按钮添加事件监听器
btBlue.addActionListener(new ColorEventListener(this));
btDialog.addActionListener(new DialogEventListener()); add(btBlue);
add(btDialog);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
} } // 外部类ColorEventListener,实现ActionListener接口
class ColorEventListener implements ActionListener {
private EventListener4 el; ColorEventListener(EventListener4 el) {
this.el = el;
} @Override
public void actionPerformed(ActionEvent e) {
Container c = el.getContentPane();
c.setBackground(Color.BLUE);
}
} // 外部类DialogEventListener,实现ActionListener接口
class DialogEventListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
JDialog dialog = new JDialog();
dialog.setBounds(300, 200, 400, 300);
dialog.setVisible(true);
}
} public class ActionListenerTest {
public static void main(String args[]) {
new EventListener4();
}
}

Java事件监听的四种实现方式的更多相关文章

  1. java 事件监听 - 鼠标

    java 事件监听 - 鼠标 //事件监听 //鼠标事件监听 //鼠标事件监听有两个实现接口 //1.MouseListener 普通的鼠标操作 //2.MouseMotionListener 鼠标的 ...

  2. java 事件监听 - 键盘

    java 事件监听 - 键盘 //事件监听 //键盘事件监听,写了一个小案例,按上下左右,改变圆形的位置,圆形可以移动 import java.awt.*; import javax.swing.*; ...

  3. java 事件监听 - 控件

    java 事件监听 //事件监听 //事件监听,写了一个小案例,点击按钮改变面板的颜色. import java.awt.*; import javax.swing.*; import java.aw ...

  4. java事件监听机制

    1.简单的事件监听 package demo2; /* * java事件监听机制 */ import java.awt.*; import javax.swing.*; import java.awt ...

  5. java事件监听机制2

    今天早上的两点收获: 1.addActionListener(其中的setActionCommand函数就是要对对象进行唯一性的标记,便于消息传来后进行处理.理论上actionlistener可以全部 ...

  6. JAVA事件监听机制的实现

    今天学习了java的事件编程机制,略有体会,先在此记下心得. 第一,首先明确几个概念. 事件源:一个产生或者触发事件的对象.事件:承载事件源状态改变时的信息对象.事件监听器接口:实际上就是一个类,该类 ...

  7. Java事件监听机制与观察者设计模式

    一. Java事件监听机制 1. 事件监听三要素: 事件源,事件对象,事件监听器 2. 三要素之间的关系:事件源注册事件监听器后,当事件源上发生某个动作时,事件源就会调用事件监听的一个方法,并将事件对 ...

  8. extjs组件添加事件监听的三种方式

    extjs对组件添加监听的三种方式  在定义组件的配置时设置 如代码中所示:  Java代码  xtype : 'textarea',  name : 'dataSetField',  labelSe ...

  9. 关于实现自定义Dialog和实现Dialog里view的事件监听的两种方法

    一.自定义dialog. 二.实现dialog里view的事件监听 1.自定义dialog比较简单.在实例化new的时候,加入样式,布局就行了.或者重写dialog. 2.实现dialog里view的 ...

随机推荐

  1. 使用 Gogs 搭建自己的 Git 服务器

    安装过程分为这些步骤: 新建用户: 下载源码编译 / 下载预编译二进制打包: 运行安装: 配置调整: 配置 nginx 反向代理: 保持服务运行: 注意,这里默认你已经安装好了 MySQL 服务器和 ...

  2. SOA架构商城二 框架搭建

    1.创建父工程 创建Maven工程pingyougou-parent,选择packaging类型为pom ,在pom.xml文件中添加锁定版本信息dependencyManagement与plugin ...

  3. yii---进行增删改查

    我们使用yii进行数据的增删改查: 一.新增数据 使用model::save()操作进行新增数据 $user= new User; $user->username =$username; $us ...

  4. html 自动弹出框

    1.点击div外部隐藏, //*代表tip_box所包含的子元素 $('body').click(function(e) { var target = $(e.target); if(!target. ...

  5. HDU-1881 毕业bg (01背包变形)

    毕业bg Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submissi ...

  6. 同样的神经网络引擎,苹果A11芯片比华为麒麟970牛在哪?

    本文编辑:Cynthia iPhone X发布之后,999美元的售价并没有让人止步,相反,新iPhone的Face ID等功能让越来越多的人期待,库克在<早安美国>节目里也说了,iPhon ...

  7. HDU 5512 - Pagodas - [gcd解决博弈]

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5512 Time Limit: 2000/1000 MS (Java/Others) Mem ...

  8. php 实现 二维码 扫描登录

    本人简单实现的示例,使用任意二维码工具打开二维码对应链接 http://www.54php.cn/demo1/scan 原理介绍: 第一步:访问登录页面,生成唯一key,例如MkhjDFL=,并且将此 ...

  9. PL/SQL常用表达式及举例(二)

    使用LOOP循环 declare v_i number:=1; begin loop dbms_output.put_line('v_i='||v_i); exit when v_i>=3; v ...

  10. 数据库级别DDL操作监控审计、数据库触发器/服务器触发器

    关键词:数据库触发器/服务器触发器  ,数据库级别DDL操作监控审计,禁止修改登录名密码 [1]数据库级别DDL操作监控审计 转自2012示例库,只能数据库级别,不能实例级别 use database ...