《Java 核心技术 卷1》 笔记 第八章 事件处理
8.1 事件處理基礎(chǔ)
事件:用戶進行某種輸入操作時,觸發(fā)的效果。比如點擊鼠標,按下鍵盤按鍵。
事件過程(VB):事件與過程之間有顯著的關(guān)系。把觸發(fā)效果(固定,語言寫死)對應的過程放在事件編寫的代碼中。
事件隊列(C):代碼不斷檢查事件隊列,查詢到有事件發(fā)生時,進行處理。
Java的事件觸發(fā)過程:
Java中所有事件對象派生于EventObject
ActionEvent:按鈕事件
WindowEvent:窗口事件
8.1.1 按鈕點擊事件
public class Main {public static void main(String[] args) {Main solution = new Main();EventQueue.invokeLater(new Runnable() {@Overridepublic void run() {ButtonFrame b = new ButtonFrame();b.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);b.setVisible(true);}});}}class ButtonFrame extends JFrame{public static final int W = 300;public static final int H = 200;private JPanel buttonPanel;public ButtonFrame(){setTitle("ButtonTest");setSize(W,H);Toolkit t = Toolkit.getDefaultToolkit();Dimension d = t.getScreenSize();setLocation(((int)d.getWidth()-W)/2,((int)d.getHeight()-H)/2);JButton yellowButton = new JButton("Yellow");JButton blueButton = new JButton("Blue");JButton redButton = new JButton("Red");buttonPanel = new JPanel();buttonPanel.add(yellowButton);buttonPanel.add(blueButton);buttonPanel.add(redButton);add(buttonPanel);ColorAction yellowAction = new ColorAction(Color.YELLOW);ColorAction blueAction = new ColorAction(Color.BLUE);ColorAction redAction = new ColorAction(Color.RED);yellowButton.addActionListener(yellowAction);blueButton.addActionListener(blueAction);redButton.addActionListener(redAction);}private class ColorAction implements ActionListener{Color bgColor;public ColorAction(Color c){bgColor = c;}public void actionPerformed(ActionEvent event){buttonPanel.setBackground(bgColor);}}}8.1.2 建議使用內(nèi)部類
這里其實作者混淆了代碼簡單和執(zhí)行速度快的概念,我覺得不足為憑。但確實寫事件的時候,大多數(shù)還是習慣用內(nèi)部類,通常來說用靜態(tài)內(nèi)部類更好一些。但只做小工具的場景下,其實內(nèi)部類影響有限,可以按作者的來。
import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.HashMap; import java.util.Map;public class Main {public static void main(String[] args) {Main solution = new Main();EventQueue.invokeLater(new Runnable() {@Overridepublic void run() {ButtonFrame b = new ButtonFrame();b.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);b.setVisible(true);}});}}class ButtonFrame extends JFrame implements ActionListener{public static final int W = 300;public static final int H = 200;private JPanel buttonPanel;Map<JButton,Color> m = new HashMap<>();public ButtonFrame(){setTitle("ButtonTest");setSize(W,H);Toolkit t = Toolkit.getDefaultToolkit();Dimension d = t.getScreenSize();setLocation(((int)d.getWidth()-W)/2,((int)d.getHeight()-H)/2);JButton yellowButton = new JButton("Yellow");JButton blueButton = new JButton("Blue");JButton redButton = new JButton("Red");buttonPanel = new JPanel();buttonPanel.add(yellowButton);buttonPanel.add(blueButton);buttonPanel.add(redButton);add(buttonPanel);yellowButton.addActionListener(this);blueButton.addActionListener(this);redButton.addActionListener(this);m.put(yellowButton,Color.YELLOW);m.put(blueButton,Color.BLUE);m.put(redButton,Color.RED);}public void actionPerformed(ActionEvent event){buttonPanel.setBackground(m.getOrDefault(event.getSource(),Color.YELLOW));} }??系列內(nèi)容:
《Java 核心技術(shù) 卷1》 筆記:第一章 Java程序設(shè)計概述
《Java 核心技術(shù) 卷1》 筆記:第二章 Java程序設(shè)計環(huán)境
《Java 核心技術(shù) 卷1》 筆記:第三章 Java基本的程序設(shè)計結(jié)構(gòu)(1)
《Java 核心技術(shù) 卷1》 筆記:第三章 Java基本的程序設(shè)計結(jié)構(gòu)(2)
《Java 核心技術(shù) 卷1》 筆記:第三章 Java基本的程序設(shè)計結(jié)構(gòu)(3)
《Java 核心技術(shù) 卷1》 筆記:第三章 Java基本的程序設(shè)計結(jié)構(gòu)(4)
《Java 核心技術(shù) 卷1》 筆記:第三章 Java基本的程序設(shè)計結(jié)構(gòu)(5)
《Java 核心技術(shù) 卷1》 筆記:第三章 Java基本的程序設(shè)計結(jié)構(gòu)(6)
《Java 核心技術(shù) 卷1》 筆記:第三章 Java基本的程序設(shè)計結(jié)構(gòu)(7)大數(shù)處理、數(shù)組、多維數(shù)組、控制臺傳參
《Java 核心技術(shù) 卷1》 筆記 第四章:類與對象
《Java 核心技術(shù) 卷1》 筆記 第四章:類與對象(2) GregorianCalendar 與 類的基本組成
《Java 核心技術(shù) 卷1》 筆記 第四章:類與對象(3) 構(gòu)造器全局私有方法
《Java 核心技術(shù) 卷1》 筆記 第四章:類與對象(4) 靜態(tài)字段+靜態(tài)方法+工廠方法
《Java 核心技術(shù) 卷1》 筆記 第四章:類與對象(5) 形參與實參 構(gòu)造器的默認值與默認構(gòu)造
《Java 核心技術(shù) 卷1》 筆記 第四章:類與對象(6) 構(gòu)造器調(diào)用與初始化塊
《Java 核心技術(shù) 卷1》 筆記 第四章:類與對象(7) 注釋、JavaDoc與類設(shè)計
《Java 核心技術(shù) 卷1》 筆記 第五章 繼承
《Java 核心技術(shù) 卷1》 筆記 第五章 繼承(2)
《Java 核心技術(shù) 卷1》 筆記 第五章 繼承(3)
《Java 核心技術(shù) 卷1》 筆記 第五章 繼承(4)equals方法
《Java 核心技術(shù) 卷1》 筆記 第五章 繼承(5)hashCode 與 toString
《Java 核心技術(shù) 卷1》 筆記 第五章 繼承(6) 泛型
《Java 核心技術(shù) 卷1》 筆記 第五章 繼承(7) 包裝類和可變數(shù)組
《Java 核心技術(shù) 卷1》 筆記 第五章 繼承(8) 枚舉類與類反射
《Java 核心技術(shù) 卷1》 筆記 第五章 繼承(9) 異常捕獲與反射運用
《Java 核心技術(shù) 卷1》 筆記 第五章 繼承(10)反射
《Java 核心技術(shù) 卷1》 筆記 第五章 繼承(11)反射泛型數(shù)組+方法指針+類設(shè)計技巧
《Java 核心技術(shù) 卷1》 筆記 第六章 接口和內(nèi)部類
《Java 核心技術(shù) 卷1》 筆記 第六章 接口和內(nèi)部類(2)
《Java 核心技術(shù) 卷1》 筆記 第六章 接口和內(nèi)部類(3) 接口回調(diào)與內(nèi)部類
《Java 核心技術(shù) 卷1》 筆記 第六章 接口和內(nèi)部類(4) 局部內(nèi)部類和局部內(nèi)部類引用方法變量分析
《Java 核心技術(shù) 卷1》 筆記 第六章 接口和內(nèi)部類(5) 匿名內(nèi)部類和靜態(tài)內(nèi)部類
《Java 核心技術(shù) 卷1》 筆記 第六章 接口和內(nèi)部類(6) 靜態(tài)代理
《Java 核心技術(shù) 卷1》 筆記 第六章 接口和內(nèi)部類(7) 動態(tài)代理
《Java 核心技術(shù) 卷1》 筆記 第七章 圖形程序設(shè)計
《Java 核心技術(shù) 卷1》 筆記 第七章 圖形程序設(shè)計(2)
《Java 核心技術(shù) 卷1》 筆記 第七章 圖形程序設(shè)計(3)
《Java 核心技術(shù) 卷1》 筆記 第七章 圖形程序設(shè)計(4)
《Java 核心技術(shù) 卷1》 筆記 第七章 圖形程序設(shè)計(5)
《Java 核心技術(shù) 卷1》 筆記 第七章 圖形程序設(shè)計(6) 圖像平鋪
喜歡的話,點個贊吧~!平時做題,以及筆記內(nèi)容將更新到公眾號。
關(guān)注公眾號,互相學習:鈺娘娘知識匯總
總結(jié)
以上是生活随笔為你收集整理的《Java 核心技术 卷1》 笔记 第八章 事件处理的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Spring工具类的使用
- 下一篇: 深入分析JavaWeb Item7 --