Java基礎(chǔ)學習之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)聽測試"); 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); } }
運行的結(jié)果
二、需求分析
想要點擊按鈕的時候在終端打印一行信息(比如"按鈕被點擊")
產(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("按鈕被點擊了----"); } }
點擊按鈕的結(jié)果
2.2 發(fā)現(xiàn)問題
中規(guī)中矩地實現(xiàn)監(jiān)聽器地話,發(fā)現(xiàn)要另外寫一個類實現(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)聽測試"); 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("按鈕被點擊了----"); } }
結(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)聽測試"); 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); }
到此這篇關(guān)于Java基礎(chǔ)學習之Swing事件監(jiān)聽的文章就介紹到這了,更多相關(guān)Swing事件監(jiān)聽內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java數(shù)據(jù)結(jié)構(gòu)與算法之插入算法實現(xiàn)數(shù)值排序示例
這篇文章主要介紹了java數(shù)據(jù)結(jié)構(gòu)與算法之插入算法實現(xiàn)數(shù)值排序的方法,結(jié)合簡單實例形式分析了插入算法的節(jié)點操作與排序相關(guān)實現(xiàn)技巧,需要的朋友可以參考下2016-08-08