Java事件监听的四种实现方式
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事件监听的四种实现方式的更多相关文章
- java 事件监听 - 鼠标
java 事件监听 - 鼠标 //事件监听 //鼠标事件监听 //鼠标事件监听有两个实现接口 //1.MouseListener 普通的鼠标操作 //2.MouseMotionListener 鼠标的 ...
- java 事件监听 - 键盘
java 事件监听 - 键盘 //事件监听 //键盘事件监听,写了一个小案例,按上下左右,改变圆形的位置,圆形可以移动 import java.awt.*; import javax.swing.*; ...
- java 事件监听 - 控件
java 事件监听 //事件监听 //事件监听,写了一个小案例,点击按钮改变面板的颜色. import java.awt.*; import javax.swing.*; import java.aw ...
- java事件监听机制
1.简单的事件监听 package demo2; /* * java事件监听机制 */ import java.awt.*; import javax.swing.*; import java.awt ...
- java事件监听机制2
今天早上的两点收获: 1.addActionListener(其中的setActionCommand函数就是要对对象进行唯一性的标记,便于消息传来后进行处理.理论上actionlistener可以全部 ...
- JAVA事件监听机制的实现
今天学习了java的事件编程机制,略有体会,先在此记下心得. 第一,首先明确几个概念. 事件源:一个产生或者触发事件的对象.事件:承载事件源状态改变时的信息对象.事件监听器接口:实际上就是一个类,该类 ...
- Java事件监听机制与观察者设计模式
一. Java事件监听机制 1. 事件监听三要素: 事件源,事件对象,事件监听器 2. 三要素之间的关系:事件源注册事件监听器后,当事件源上发生某个动作时,事件源就会调用事件监听的一个方法,并将事件对 ...
- extjs组件添加事件监听的三种方式
extjs对组件添加监听的三种方式 在定义组件的配置时设置 如代码中所示: Java代码 xtype : 'textarea', name : 'dataSetField', labelSe ...
- 关于实现自定义Dialog和实现Dialog里view的事件监听的两种方法
一.自定义dialog. 二.实现dialog里view的事件监听 1.自定义dialog比较简单.在实例化new的时候,加入样式,布局就行了.或者重写dialog. 2.实现dialog里view的 ...
随机推荐
- JVM常用工具使用之jmap
一.参考链接 https://www.cnblogs.com/yjd_hycf_space/p/7753847.html 二.个人的总结 一般我习惯使用 jmap -dump:live,format= ...
- wps插件开发中com组件权限
需要对wps写一个小的插件,也就是几行代码的事情,但却碰到了一个坑 wps中的com组件的调用和MSoffice非常的相似,几乎只需要把包的头修改一下就可以用了. 比如开发wps文档的插件,需要引用 ...
- 关于Jmeter3.0,你必须要知道的5点变化
2016.5.18日,Apache 发布了jmeter 3.0版本,本人第一时间上去查看并下载使用了,然后群里或同事都会问有什么样变化呢?正好在网上看到一遍关于3.0的文章,但是是英文的.这里翻译一下 ...
- 从Spring到SpringBoot构建WEB MVC核心配置详解
目录 理解Spring WEB MVC架构的演变 认识Spring WEB MVC 传统时代的Spring WEB MVC 新时代Spring WEB MVC SpringBoot简化WEB MVC开 ...
- 使用IntelliJ IDEA进行Python远程调试的需求(未完)
使用IntelliJ IDEA进行Python远程调试的需求(未完) 在研究深度学习Machlearning时,有时候需要借助ubuntu搭建的tensorflow环境,另外也有越来越多的运算程序只能 ...
- 表优化 altering table OPTIMIZE TABLE `sta_addr_copy`
表优化 altering table OPTIMIZE TABLE `sta_addr_copy` [总结] 1.实际测试的结果是,在state sqlaltering table OPTIMIZE ...
- SET NAMES
High Performance MySQL, Third Editionby Baron Schwartz, Peter Zaitsev, and Vadim Tkachenko Settings ...
- 构建更好的客户端 JavaScript 应用
你可能注意到了,最近的一段时间越来越多的Web应用有变复杂的趋势,重心从服务端慢慢向着客户端转移. 这是个正常的趋势么?我不知道.支持和反对者的讨论就像是在讨论复活者和圣诞节哪一个更好一样; 很难说哪 ...
- 【Swift初见】SourceKitService Terminated
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/weasleyqi/article/details/36162085 心血来潮想试试最新的Xcode6 ...
- 对innodb_flush_log_at_commit参数的写日志和刷盘行为进行图解
对innodb_flush_log_at_commit参数的写日志和刷盘行为进行图解