Java基礎學習之Swing事件監(jiān)聽
更新時間:2021年05月26日 08:55:18 作者:一腔詩意醉了酒
今天學習java的Swing庫,創(chuàng)建桌面應用的時候,突然發(fā)現(xiàn)有些按鈕需要特定的功能響應,故來研究一番Swing的事件監(jiān)聽,文中有非常詳細的介紹及代碼示例,需要的朋友可以參考下
一、初始代碼架構
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)聽測試");
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);
}
}
運行的結果

二、需求分析
想要點擊按鈕的時候在終端打印一行信息(比如"按鈕被點擊")
產(chǎn)品爸爸,提了新需求,不能實現(xiàn)也要創(chuàng)造辦法實現(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)聽測試");
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)聽器注冊到按鈕上
*
*/
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 發(fā)現(xiàn)問題
中規(guī)中矩地實現(xiàn)監(jiān)聽器地話,發(fā)現(xiàn)要另外寫一個類實現(xiàn)
ActionListener接口地方法,使得代碼架構顯得比較臃腫。
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)聽測試");
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)聽器注冊到按鈕上
*
*/
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("按鈕被點擊了----");
}
}

2.4 優(yōu)化完之后發(fā)現(xiàn)還是不是很優(yōu)雅
因為每次監(jiān)聽都有重復代碼
@Override
public void actionPerformed(ActionEvent e){
System.out.println("匿名內(nèi)部類優(yōu)化----");
}
2.5 使用Lambda表達式再優(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)聽測試");
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)聽器注冊到按鈕上
*
*/
// 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表達式優(yōu)化");
});
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("事件監(jiān)聽測試");
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表達式優(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);
}
到此這篇關于Java基礎學習之Swing事件監(jiān)聽的文章就介紹到這了,更多相關Swing事件監(jiān)聽內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Java中的synchronized?優(yōu)化方法之鎖膨脹機制
這篇文章主要介紹了Java中的synchronized?優(yōu)化方法之鎖膨脹機制,鎖膨脹機制是提升?synchronized?性能最有利的方法之一,下面我們就來看看什么事鎖膨脹及鎖膨脹的各種細節(jié)2022-05-05

