Java基础学习之Swing事件监听

网友投稿 268 2023-01-12

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{

arKhBV @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);

}

});

ContainearKhBVr 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("打印");

btarKhBVn.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 How to Write an Action Listener

*

* @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);

}

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:众楷物流查询(上海众楷物流有限公司)
下一篇:德快递物流查询单号(德天物流快递单号查询)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~