Java基础学习之Swing事件监听
一、初始代码架构
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Btn extends JFrame{ public static void main(String []args){ JFrame f = new JFrame("事件监听测试"); f.setBounds(0,0,300,400); f.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){ f.setVisible(false); f.dispose(); System.exit(0); } }); Container page = f.getContentPane(); page.setLayout(new FlowLayout()); JButton btn = new JButton("打印"); page.add(btn); f.setVisible(true); } }
运行的结果
二、需求分析
想要点击按钮的时候在终端打印一行信息(比如"按钮被点击")
产品爸爸,提了新需求,不能实现也要创造办法实现的啦
2.1 写监听器
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Btn{ public static void main(String []args){ JFrame f = new JFrame("事件监听测试"); f.setBounds(0,0,300,400); f.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){ f.setVisible(false); f.dispose(); System.exit(0); } }); Container page = f.getContentPane(); page.setLayout(new FlowLayout()); JButton btn = new JButton("打印"); /** * 事件监听的使用步骤: * 1. 创建监听听 * 2. 将监听器注册到按钮上 * */ btnListener bl = new btnListener(); btn.addActionListener(bl); page.add(btn); f.setVisible(true); } } class btnListener implements ActionListener{ @Override public void actionPerformed(ActionEvent e){ System.out.println("按钮被点击了----"); } }
点击按钮的结果
2.2 发现问题
中规中矩地实现监听器地话,发现要另外写一个类实现
ActionListener
接口地方法,使得代码架构显得比较臃肿。
2.3 使用匿名内部类优化代码
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Btn{ public static void main(String []args){ JFrame f = new JFrame("事件监听测试"); f.setBounds(0,0,300,400); f.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){ f.setVisible(false); f.dispose(); System.exit(0); } }); Container page = f.getContentPane(); page.setLayout(new FlowLayout()); JButton btn = new JButton("打印"); /** * 事件监听的使用步骤: * 1. 创建监听听 * 2. 将监听器注册到按钮上 * */ ActionListener bl = new ActionListener(){ @Override public void actionPerformed(ActionEvent e){ System.out.println("匿名内部类优化----"); } }; btn.addActionListener(bl); page.add(btn); f.setVisible(true); } } class btnListener implements ActionListener{ @Override public void actionPerformed(ActionEvent e){ System.out.println("按钮被点击了----"); } }
2.4 优化完之后发现还是不是很优雅
因为每次监听都有重复代码
@Override public void actionPerformed(ActionEvent e){ System.out.println("匿名内部类优化----"); }
2.5 使用Lambda表达式再优化
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Btn{ public static void main(String []args){ JFrame f = new JFrame("事件监听测试"); f.setBounds(0,0,300,400); f.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){ f.setVisible(false); f.dispose(); System.exit(0); } }); Container page = f.getContentPane(); page.setLayout(new FlowLayout()); JButton btn = new JButton("打印"); /** * 事件监听的使用步骤: * 1. 创建监听听 * 2. 将监听器注册到按钮上 * */ // ActionListener bl = new ActionListener(){ // @Override // public void actionPerformed(ActionEvent e){ // System.out.println("匿名内部类优化----"); // } // }; // btn.addActionListener(bl); btn.addActionListener((e)->{ System.out.println("使用Lambda表达式优化"); }); page.add(btn); f.setVisible(true); } } class btnListener implements ActionListener{ @Override public void actionPerformed(ActionEvent e){ System.out.println("按钮被点击了----"); } }
结果
2.6 最终的代码
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Btn{ public static void main(String []args){ JFrame f = new JFrame("事件监听测试"); f.setBounds(0,0,300,400); f.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){ f.setVisible(false); f.dispose(); System.exit(0); } }); Container page = f.getContentPane(); page.setLayout(new FlowLayout()); JButton btn = new JButton("打印"); btn.addActionListener((e)->{ System.out.println("使用Lambda表达式优化"); }); page.add(btn); f.setVisible(true); } }
三、ActionListener接口源码
/* * Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved. * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */ package java.awt.event; import java.util.EventListener; /** * The listener interface for receiving action events. * The class that is interested in processing an action event * implements this interface, and the object created with that * class is registered with a component, using the component's * {@code addActionListener} method. When the action event * occurs, that object's {@code actionPerformed} method is * invoked. * * @see ActionEvent * @see <a href="https://docs.oracle.com/javase/tutorial/uiswing/events/actionlistener.html" rel="external nofollow" >How to Write an Action Listener</a> * * @author Carl Quinn * @since 1.1 */ public interface ActionListener extends EventListener { /** * Invoked when an action occurs. * @param e the event to be processed */ public void actionPerformed(ActionEvent e); }
到此这篇关于Java基础学习之Swing事件监听的文章就介绍到这了,更多相关Swing事件监听内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
如何在springMVC的controller中获取request
这篇文章主要介绍了如何在springMVC的controller中获取request,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下2019-12-12如何通过eclipse web项目导入itellij idea并启动
这篇文章主要介绍了如何通过eclipse web项目导入itellij idea并启动,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下2020-12-12Set接口深入剖析之HashSet、LinkedHashSet和TreeSet
这篇文章主要介绍了Set接口深入剖析之HashSet、LinkedHashSet和TreeSet,LinkedHashSet是HashSet的子类,实现了Set接口,LinkedHashSet底层是一个LinkedHashMap,底层维护了一个数组+双向链表,需要的朋友可以参考下2023-09-09System.currentTimeMillis()计算方式与时间的单位转换详解
这篇文章主要介绍了System.currentTimeMillis()计算方式与时间的单位转换详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2020-05-05SpringBoot登录、退出、获取用户信息的session处理方案
这篇文章主要介绍了SpringBoot登录、退出、获取用户信息的session处理,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下2023-08-08深入浅析springboot中static和templates区别
这篇文章主要介绍了springboot中static和templates区别,本文通过图文实例代码相结合给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下2020-02-02java 中序列化与readResolve()方法的实例详解
这篇文章主要介绍了java 中序列化与readResolve()方法的实例详解的相关资料,这里提供实例帮助大家理解这部分知识,需要的朋友可以参考下2017-08-08
最新评论