Java基礎(chǔ)學(xué)習(xí)之Swing事件監(jiān)聽
一、初始代碼架構(gòu)
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("事件監(jiān)聽測(cè)試");
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);
}
}
運(yùn)行的結(jié)果

二、需求分析
想要點(diǎn)擊按鈕的時(shí)候在終端打印一行信息(比如"按鈕被點(diǎn)擊")
產(chǎn)品爸爸,提了新需求,不能實(shí)現(xiàn)也要?jiǎng)?chuàng)造辦法實(shí)現(xiàn)的啦
2.1 寫監(jiān)聽器
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Btn{
public static void main(String []args){
JFrame f = new JFrame("事件監(jiān)聽測(cè)試");
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("打印");
/**
* 事件監(jiān)聽的使用步驟:
* 1. 創(chuàng)建監(jiān)聽聽
* 2. 將監(jiān)聽器注冊(cè)到按鈕上
*
*/
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("按鈕被點(diǎn)擊了----");
}
}
點(diǎn)擊按鈕的結(jié)果
2.2 發(fā)現(xiàn)問題
中規(guī)中矩地實(shí)現(xiàn)監(jiān)聽器地話,發(fā)現(xiàn)要另外寫一個(gè)類實(shí)現(xiàn)
ActionListener接口地方法,使得代碼架構(gòu)顯得比較臃腫。
2.3 使用匿名內(nèi)部類優(yōu)化代碼
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Btn{
public static void main(String []args){
JFrame f = new JFrame("事件監(jiān)聽測(cè)試");
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("打印");
/**
* 事件監(jiān)聽的使用步驟:
* 1. 創(chuàng)建監(jiān)聽聽
* 2. 將監(jiān)聽器注冊(cè)到按鈕上
*
*/
ActionListener bl = new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
System.out.println("匿名內(nèi)部類優(yōu)化----");
}
};
btn.addActionListener(bl);
page.add(btn);
f.setVisible(true);
}
}
class btnListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e){
System.out.println("按鈕被點(diǎn)擊了----");
}
}

2.4 優(yōu)化完之后發(fā)現(xiàn)還是不是很優(yōu)雅
因?yàn)槊看伪O(jiān)聽都有重復(fù)代碼
@Override
public void actionPerformed(ActionEvent e){
System.out.println("匿名內(nèi)部類優(yōu)化----");
}
2.5 使用Lambda表達(dá)式再優(yōu)化
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Btn{
public static void main(String []args){
JFrame f = new JFrame("事件監(jiān)聽測(cè)試");
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("打印");
/**
* 事件監(jiān)聽的使用步驟:
* 1. 創(chuàng)建監(jiān)聽聽
* 2. 將監(jiān)聽器注冊(cè)到按鈕上
*
*/
// ActionListener bl = new ActionListener(){
// @Override
// public void actionPerformed(ActionEvent e){
// System.out.println("匿名內(nèi)部類優(yōu)化----");
// }
// };
// btn.addActionListener(bl);
btn.addActionListener((e)->{
System.out.println("使用Lambda表達(dá)式優(yōu)化");
});
page.add(btn);
f.setVisible(true);
}
}
class btnListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e){
System.out.println("按鈕被點(diǎn)擊了----");
}
}
結(jié)果

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("事件監(jiān)聽測(cè)試");
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表達(dá)式優(yōu)化");
});
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 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);
}
到此這篇關(guān)于Java基礎(chǔ)學(xué)習(xí)之Swing事件監(jiān)聽的文章就介紹到這了,更多相關(guān)Swing事件監(jiān)聽內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Maven清理java項(xiàng)目中未使用到 jar 依賴包的方法
本文主要介紹了Maven清理java項(xiàng)目中未使用到 jar 依賴包的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2025-02-02
Java實(shí)體類中Set按照對(duì)象的某個(gè)字段對(duì)set排序
這篇文章主要介紹了Java實(shí)體類中Set按照對(duì)象的某個(gè)字段對(duì)set排序,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-06-06
Java中的synchronized?優(yōu)化方法之鎖膨脹機(jī)制
這篇文章主要介紹了Java中的synchronized?優(yōu)化方法之鎖膨脹機(jī)制,鎖膨脹機(jī)制是提升?synchronized?性能最有利的方法之一,下面我們就來看看什么事鎖膨脹及鎖膨脹的各種細(xì)節(jié)2022-05-05
java實(shí)現(xiàn)手寫一個(gè)簡(jiǎn)單版的線程池
有些人可能對(duì)線程池比較陌生,并且更不熟悉線程池的工作原理。本文就來手寫一個(gè)簡(jiǎn)單版的線程池,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08
Spring手動(dòng)生成web.xml配置文件過程詳解
這篇文章主要介紹了Spring手動(dòng)生成web.xml配置文件過程詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07
java?stream使用指南之sorted使用及進(jìn)階方式
這篇文章主要介紹了java?stream使用指南之sorted使用及進(jìn)階方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-05-05

