一、事件监听机制
-- 事件源:awt 或swing包中的那些图形界面组件,即发生事件的组件
-- 事件:Event 用户对组件的一个操作
-- 监听器:Listener 负责处理事件的方法
二、java.awt.event 包下的类
WindowEvent //窗口事件,比如用户点了半闭窗口,窗口得到或失去焦点,最大化最小化等
MouseEvent //鼠标事件,鼠标按下,鼠标释放,点击(按下后再松开)等
ActionEvent //动作事件,它不是代表一个具体动作,而是一种语义,比如按纽,或菜单被点击,在文本框中按下回车等,可以这样理解:用户的某一动作导致了某个组件本身的基本作用发生了,这就是ActionEvent事件
不同的事件类型,对应着不同的事件监听器接口,接口的名称和事件的名称是相对应的。
WindowEvent - >WindowListener
MouseEvent ->MouseListener
ActionEvent ->ActionListener
代码示例:
import java.awt.Frame;import java.awt.event.WindowEvent;import java.awt.event.WindowListener;public class Test20 {public static void main(String[] args) { Frame f = new Frame(); f.setSize(400, 400); f.setVisible(true); f.addWindowListener(new WindowListener() { @Overridepublic void windowOpened(WindowEvent e) { // 窗口被打开// TODO Auto-generated method stub} @Overridepublic void windowClosing(WindowEvent e) { // 设置关闭事件// TODO Auto-generated method stubSystem.exit(0); } @Overridepublic void windowClosed(WindowEvent e) { // 用户已经关闭窗口// TODO Auto-generated method stub} @Overridepublic void windowIconified(WindowEvent e) { // 被最小化的时候// TODO Auto-generated method stub} @Overridepublic void windowDeiconified(WindowEvent e) { // 最小化被还原的时候// TODO Auto-generated method stub} @Overridepublic void windowActivated(WindowEvent e) { // 窗体被激活// TODO Auto-generated method stub} @Overridepublic void windowDeactivated(WindowEvent e) { // 失去焦点的时候// TODO Auto-generated method stub} }); } }
有没有发现,用WindowListener接口的时候,会引入一大堆不常用的代码(这里我们只想设置关闭),借口里的方法只能被覆盖,又不能删掉,这样就是的整个项目显得很罗嗦,为了解决这个事情,就有了事件适配器。
三、事件适配器
JDK 针对大多数事件监听器接口类定义了相应的实现类(里面有很多空实现的方法,方便我们创建侦听器对象),我们称为事件适配器类。这里我用到了WindowAdapter。
import java.awt.Frame;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;public class Test21 {public static void main(String[] args) { Frame f = new Frame("事件适配器的栗子"); f.setSize(400, 400); f.setVisible(true); f.addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent e) { System.exit(0); } }); } }
可以观察一下WindowAdapter类的源码,找找感觉。
public abstract class WindowAdapterimplements WindowListener, WindowStateListener, WindowFocusListener {/** * Invoked when a window has been opened. */public void windowOpened(WindowEvent e) {}/** * Invoked when a window is in the process of being closed. * The close operation can be overridden at this point. */public void windowClosing(WindowEvent e) {}/** * Invoked when a window has been closed. */public void windowClosed(WindowEvent e) {}/** * Invoked when a window is iconified. */public void windowIconified(WindowEvent e) {}/** * Invoked when a window is de-iconified. */public void windowDeiconified(WindowEvent e) {}/** * Invoked when a window is activated. */public void windowActivated(WindowEvent e) {}/** * Invoked when a window is de-activated. */public void windowDeactivated(WindowEvent e) {}/** * Invoked when a window state is changed. * @since 1.4 */public void windowStateChanged(WindowEvent e) {}/** * Invoked when the Window is set to be the focused Window, which means * that the Window, or one of its subcomponents, will receive keyboard * events. * * @since 1.4 */public void windowGainedFocus(WindowEvent e) {}/** * Invoked when the Window is no longer the focused Window, which means * that keyboard events will no longer be delivered to the Window or any of * its subcomponents. * * @since 1.4 */public void windowLostFocus(WindowEvent e) {} }
练习几个个事件处理程序的栗子吧。
例一:
import java.awt.Button;import java.awt.FlowLayout;import java.awt.Frame;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;//例一:在窗体中放置一个按纽,点击后让程序退出class TestFrame implements ActionListener { // ActionListener接口里面只有一个方法,下面会重写private Frame f;public TestFrame() { f = new Frame("窗口"); init(); }private void init() { f.setSize(300, 300); f.setLayout(new FlowLayout());// 布局模式Button b = new Button("退出程序"); b.addActionListener(this); f.add(b); f.setVisible(true); } @Overridepublic void actionPerformed(ActionEvent e) {// TODO Auto-generated method stubf.setVisible(false); f.dispose();// 在关闭的时候,可以用它来销毁窗体资源System.exit(0);// 退出 } }public class Test22 {public static void main(String[] args) {new TestFrame(); } }
上面的,点击退出程序按钮才可以退出,点击右上角的X,是不可以退出的哦。因为没有设置WindowListener哦。
这个例子用到ActionListener接口,可以看一下它的源代码,如下:
public interface ActionListener extends EventListener {/** * Invoked when an action occurs. */public void actionPerformed(ActionEvent e); }
例二:
TextField txtNo;= Frame("请输入密码"f.setBounds(50, 50, 400, 400);f.setLayout( FlowLayout());= TextField(10); code = (!(code >= KeyEvent.VK_0 && code <= KeyEvent.VK_9)) {System.out.println(KeyEvent.getKeyText(code) + "输入有误" 0
测试类:
public class Test23 {public static void main(String[] args) {new TestFrame(); } }
例三:
列出指定目录的内容:
import java.awt.Button;import java.awt.FlowLayout;import java.awt.Frame;import java.awt.TextArea;import java.awt.TextField;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import java.io.File;public class Test24 {public static void main(String[] args) {new MyWindow(); } }class MyWindow { MyWindow() { init(); }private Frame f;private Button b;private TextField txtDir;// 用来输入目录名称private TextArea txtFileList;// 用来显示文件列表private void init() { f = new Frame("窗口"); f.setBounds(44, 44, 500, 500); f.setLayout(new FlowLayout()); txtDir = new TextField(8); b = new Button("显示"); txtFileList = new TextArea(20, 30);// 用来显示文件列表的区域f.add(txtDir); f.add(b); f.add(txtFileList); initEvent(); f.setVisible(true); }private void initEvent() {// TODO Auto-generated method stubf.addWindowListener(new WindowAdapter() {/** * Invoked when a window is in the process of being closed. The * close operation can be overridden at this point. */public void windowClosing(WindowEvent e) { System.exit(0); } }); b.addActionListener(new ActionListener() {/** * Invoked when an action occurs. */public void actionPerformed(ActionEvent e) { txtFileList.setText(""); String dirStr = txtDir.getText();// 取出用户输入的路径File file = new File(dirStr);if (file.isDirectory() && file.exists()) { String[] fileNameList = file.list();for (String s : fileNameList) { txtFileList.append(s + "\r\n");// 别忘了换行符 } } else { txtFileList.append("输入有误,请重新输入"); } } }); } }
结果:(我让它显示我D盘的目录)
以上是Java中GUI编程讲解(下)的详细内容。更多信息请关注PHP中文网其他相关文章!

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于结构化数据处理开源库SPL的相关问题,下面就一起来看一下java下理想的结构化数据处理类库,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于PriorityQueue优先级队列的相关知识,Java集合框架中提供了PriorityQueue和PriorityBlockingQueue两种类型的优先级队列,PriorityQueue是线程不安全的,PriorityBlockingQueue是线程安全的,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于java锁的相关问题,包括了独占锁、悲观锁、乐观锁、共享锁等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于多线程的相关问题,包括了线程安装、线程加锁与线程不安全的原因、线程安全的标准类等等内容,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于枚举的相关问题,包括了枚举的基本操作、集合类对枚举的支持等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Java的相关知识,其中主要介绍了关于关键字中this和super的相关问题,以及他们的一些区别,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于平衡二叉树(AVL树)的相关知识,AVL树本质上是带了平衡功能的二叉查找树,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Java的相关知识,其中主要整理了Stream流的概念和使用的相关问题,包括了Stream流的概念、Stream流的获取、Stream流的常用方法等等内容,下面一起来看一下,希望对大家有帮助。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

SublimeText3汉化版
中文版,非常好用

MinGW - 适用于 Windows 的极简 GNU
这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

Atom编辑器mac版下载
最流行的的开源编辑器

记事本++7.3.1
好用且免费的代码编辑器

mPDF
mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),