信息发布→ 登录 注册 退出

Java基础学习之Swing事件监听

发布时间:2026-01-11

点击量:
目录
  • 一、初始代码架构
  • 二、需求分析
    • 2.1 写监听器
    • 2.2 发现问题
    • 2.3 使用匿名内部类优化代码
    • 2.4 优化完之后发现还是不是很优雅
    • 2.5 使用Lambda表达式再优化
    • 2.6 最终的代码
  • 三、ActionListener接口源码

    一、初始代码架构

    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);
    
    }
    
    
    在线客服
    服务热线

    服务热线

    4008888355

    微信咨询
    二维码
    返回顶部
    ×二维码

    截屏,微信识别二维码

    打开微信

    微信号已复制,请打开微信添加咨询详情!