Java界面编程—事件监听机制
2024-09-25 02:22:10
组件首先要先注册事件处理器,当用户单击组件、移动鼠标或者敲击键盘时都会产生事件(Event),一旦有时间发生,应用程序就会做出对该事件的响应,这些组件就是事件源(Event source)。
接受、解析和处理事件,实现和用户交互的方法称为事件处理器(Event handler)。
事件源(即组件)可以产生多种不同类型的事件,也可以注册多种不同类型的事件监听器,当事件源(组件)上发生某种事件,生成相应的事件对象,该对象中封装了有关该事件的各种信息。该对象被传递到相应的注册的事件处理器,此时事件的处理方法才执行。
事件源:就是 awt 包或 swing 包中的那些图形界面的组件(如按钮、文本框、单选框等)
事件:每个事件源都有自己特定的对应事件和共性事件
监听器:可以发出某一个事件的动作都已经封装到监听器中
事件处理:触发事件后的处理方式
事件监听处理的四种方法
-- 自身类实现 ActionListener 接口,作为事件监听器。但如果容器中有多个监听处理部分时,需要一个一个去判断事件源,因此会影响程序性能。不建议使用。
-- 通过匿名类处理。如果容器的监听事件比较少是,该方式很合适;但当监听事件较多时,会造成代码可读性差。
-- 通过内部类处理。该方式符合面向对象编程(可以设置内部类只允许自身类适用,而且方便自身类的资源),尤其适合需要处理多个监听事件的情形,可读性也很好。
-- 通过外部类处理。当多个监听事件相同时,可以选用此种方式。
自身类实现 ActionListener 接口,作为事件监听器
package listener; 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; public class EventListener1 extends JFrame implements ActionListener { // 自身类实现 ActionListener 接口,作为事件监听器
private static final long serialVersionUID = 1L;
private JButton btBlue;
private JButton btDialog; public EventListener1() { // 构造器
setTitle("Java GUI 时间监听处理"); // 设置标题栏内容
setBounds(100, 100, 500, 350); // 设置初始化窗口位置
setLayout(new FlowLayout()); // 设置布局管理器
btBlue = new JButton("蓝色"); // 创建一个按钮
btBlue.addActionListener(this); // 将按钮添加事件监听器
btDialog = new JButton("弹窗"); // 创建一个按钮
btDialog.addActionListener(this); // 将按钮添加事件监听器
add(btBlue); // 将按钮添加到 JFrame 容器上
add(btDialog); // 将按钮添加到 JFrame 容器上
setVisible(true); // 设置窗口可视化
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 设置窗口关闭
} @Override
public void actionPerformed(ActionEvent ae) { // 事件处理
if (ae.getSource() == btBlue) { // 判断最初发生 Event 事件的对象
Container container = getContentPane(); // 获取容器
container.setBackground(Color.blue); // 设置容器背景颜色
} else if (ae.getSource() == btDialog) { // 判断最初发生 Event 事件的对象
JDialog dialog = new JDialog(); // 创建 JDialog 窗口对象
dialog.setBounds(300, 200, 400, 300);
dialog.setVisible(true);
}
} public static void main(String[] args) {
new EventListener1(); // 创建 EventListener1 对象
}
}
通过匿名类处理事件监听
package listener; 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; public class EventListener2 extends JFrame { private static final long serialVersionUID = 1L;
private JButton btBlue;
private JButton btDialog; public EventListener2() { // 构造器
setTitle("Java GUI 事件监听处理"); // 设置标题栏内容
setBounds(100,100,500,350); // 设置初始化窗口位置
setLayout(new FlowLayout()); // 设置窗口布局
btBlue = new JButton("蓝色"); // 创建一个按钮
btBlue.addActionListener(new ActionListener() { // 添加事件监听器,此处是匿名内部类
@Override
public void actionPerformed(ActionEvent e) { // 事件处理
Container container = getContentPane(); // 获得容器
container.setBackground(Color.blue); // 设置容器背景色
}
});
btDialog = new JButton("弹框"); // 创建一个按钮
btDialog.addActionListener(new ActionListener() { // 添加事件监听器,此处是匿名内部类
@Override
public void actionPerformed(ActionEvent e) { // 事件处理
JDialog dialog = new JDialog(); // 创建一个 JDialog 窗口对象
dialog.setBounds(300,200,400,300);
dialog.setVisible(true);
}
});
add(btBlue); // 将按钮添加到 JFrame 容器中
add(btDialog); // 将按钮添加到 JFrame 容器中
setVisible(true); // 设置窗口可视化
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 设置窗口关闭
}
public static void main(String[] args) {
new EventListener2(); // 创建 EventListener2 对象
}
}
通过内部类处理事件监听
package listener; 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; public class EventListener3 extends JFrame { private static final long serialVersionUID = 1L;
private JButton btBlue;
private JButton btDialog; public EventListener3() { // 构造方法
setTitle("Java GUI 事件监听处理"); // 设置标题栏内容
setBounds(100,100,500,350); // 设置初始化窗口位置
setLayout(new FlowLayout()); // 设置容器布局
btBlue = new JButton("蓝色"); // 创建一个按钮
btBlue.addActionListener(new ColorEventListener()); // 按钮添加事件监听器
btDialog = new JButton("弹窗"); // 创建一个按钮
btDialog.addActionListener(new DialogEventListener()); // 按钮添加事件监听器
add(btBlue); // 将按钮添加到容器中
add(btDialog); // 将按钮添加到容器中
setVisible(true); // 设置容器可视化
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 设置窗口关闭
}
class ColorEventListener implements ActionListener{ // 使用内部类
@Override
public void actionPerformed(ActionEvent e) { // 事件执行
Container container = getContentPane();
container.setBackground(Color.blue);
}
}
class DialogEventListener implements ActionListener { // 使用内部类
@Override
public void actionPerformed(ActionEvent e) { // 事件执行
JDialog dialog = new JDialog();
dialog.setBounds(300, 200, 400, 300);
dialog.setVisible(true);
}
}
public static void main(String[] args) {
new EventListener3(); // 创建 EventListener3 实例对象
}
}
通过外部类处理事件监听
package listener; import java.awt.FlowLayout; import javax.swing.JButton;
import javax.swing.JFrame; public class EventListener4 extends JFrame { private static final long serialVersionUID = 1L;
private JButton btBlue;
private JButton btDialog; public EventListener4() {
setTitle("Java GUI 事件监听处理");
setBounds(100, 100, 500, 350);
setLayout(new FlowLayout());
btBlue = new JButton("蓝色");
btBlue.addActionListener(new ColorEventListener(this));
btDialog = new JButton("弹窗");
btDialog.addActionListener(new DialogEventListener());
add(btBlue);
add(btDialog);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
} public static void main(String[] args) {
new EventListener4();
}
} // 外部类
package listener; import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; public class ColorEventListener implements ActionListener {
private EventListener4 el; public ColorEventListener(EventListener4 el) {
this.el = el;
}
@Override
public void actionPerformed(ActionEvent arg0) {
Container container = el.getContentPane();
container.setBackground(Color.blue);
}
}
// 外部类
package listener; import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JDialog; public class DialogEventListener implements ActionListener { @Override
public void actionPerformed(ActionEvent arg0) {
JDialog dialog = new JDialog();
dialog.setBounds(300, 200, 400, 300);
dialog.setVisible(true);
}
}
效果
最新文章
- java实现二叉树
- 用jdbc访问大段文本数据
- MSSQL 批量Update
- 汉诺塔(c++)
- [转]Hibernate不能自动建表解决办法及Hibernate不同数据库的连接及SQL方言
- php多种实例理解无限极分类
- 传统IT大佬们,路在何方?
- Random的nextInt用法
- 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(20)-权限管理系统-根据权限获取菜单
- 【MFC学习笔记-作业8-蝴蝶飞~】【什么鬼作业】
- 对“demo!demo.Index+HookProc::Invoke”垃圾收集的类型已委托回调。这可能会导致应用程序崩溃、损坏和数据丢失。当传递委托给非托管代码,托管应用程序必须让这些委托保持活着
- HTML/CSS/Javascript调试入门(转)
- php发送短信验证码
- CKFinker 2.5.0.1 去demo标示
- 人脸识别准备 -- 基于raspberry pi 3b + movidius
- Spring Cloud系列之Feign的常见问题总结
- 2019-03-27-day020-单继承与多继承
- gulp 报错'wacth' errord
- javascript+dom编程艺术 读后感
- [转]C++中关于new和delete的使用