《Java 核心技术 卷1》 笔记 第八章 事件处理
8.1 事件處理基礎
事件:用戶進行某種輸入操作時,觸發的效果。比如點擊鼠標,按下鍵盤按鍵。
事件過程(VB):事件與過程之間有顯著的關系。把觸發效果(固定,語言寫死)對應的過程放在事件編寫的代碼中。
事件隊列(C):代碼不斷檢查事件隊列,查詢到有事件發生時,進行處理。
Java的事件觸發過程:
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 建議使用內部類
這里其實作者混淆了代碼簡單和執行速度快的概念,我覺得不足為憑。但確實寫事件的時候,大多數還是習慣用內部類,通常來說用靜態內部類更好一些。但只做小工具的場景下,其實內部類影響有限,可以按作者的來。
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));} }??系列內容:
《Java 核心技術 卷1》 筆記:第一章 Java程序設計概述
《Java 核心技術 卷1》 筆記:第二章 Java程序設計環境
《Java 核心技術 卷1》 筆記:第三章 Java基本的程序設計結構(1)
《Java 核心技術 卷1》 筆記:第三章 Java基本的程序設計結構(2)
《Java 核心技術 卷1》 筆記:第三章 Java基本的程序設計結構(3)
《Java 核心技術 卷1》 筆記:第三章 Java基本的程序設計結構(4)
《Java 核心技術 卷1》 筆記:第三章 Java基本的程序設計結構(5)
《Java 核心技術 卷1》 筆記:第三章 Java基本的程序設計結構(6)
《Java 核心技術 卷1》 筆記:第三章 Java基本的程序設計結構(7)大數處理、數組、多維數組、控制臺傳參
《Java 核心技術 卷1》 筆記 第四章:類與對象
《Java 核心技術 卷1》 筆記 第四章:類與對象(2) GregorianCalendar 與 類的基本組成
《Java 核心技術 卷1》 筆記 第四章:類與對象(3) 構造器全局私有方法
《Java 核心技術 卷1》 筆記 第四章:類與對象(4) 靜態字段+靜態方法+工廠方法
《Java 核心技術 卷1》 筆記 第四章:類與對象(5) 形參與實參 構造器的默認值與默認構造
《Java 核心技術 卷1》 筆記 第四章:類與對象(6) 構造器調用與初始化塊
《Java 核心技術 卷1》 筆記 第四章:類與對象(7) 注釋、JavaDoc與類設計
《Java 核心技術 卷1》 筆記 第五章 繼承
《Java 核心技術 卷1》 筆記 第五章 繼承(2)
《Java 核心技術 卷1》 筆記 第五章 繼承(3)
《Java 核心技術 卷1》 筆記 第五章 繼承(4)equals方法
《Java 核心技術 卷1》 筆記 第五章 繼承(5)hashCode 與 toString
《Java 核心技術 卷1》 筆記 第五章 繼承(6) 泛型
《Java 核心技術 卷1》 筆記 第五章 繼承(7) 包裝類和可變數組
《Java 核心技術 卷1》 筆記 第五章 繼承(8) 枚舉類與類反射
《Java 核心技術 卷1》 筆記 第五章 繼承(9) 異常捕獲與反射運用
《Java 核心技術 卷1》 筆記 第五章 繼承(10)反射
《Java 核心技術 卷1》 筆記 第五章 繼承(11)反射泛型數組+方法指針+類設計技巧
《Java 核心技術 卷1》 筆記 第六章 接口和內部類
《Java 核心技術 卷1》 筆記 第六章 接口和內部類(2)
《Java 核心技術 卷1》 筆記 第六章 接口和內部類(3) 接口回調與內部類
《Java 核心技術 卷1》 筆記 第六章 接口和內部類(4) 局部內部類和局部內部類引用方法變量分析
《Java 核心技術 卷1》 筆記 第六章 接口和內部類(5) 匿名內部類和靜態內部類
《Java 核心技術 卷1》 筆記 第六章 接口和內部類(6) 靜態代理
《Java 核心技術 卷1》 筆記 第六章 接口和內部類(7) 動態代理
《Java 核心技術 卷1》 筆記 第七章 圖形程序設計
《Java 核心技術 卷1》 筆記 第七章 圖形程序設計(2)
《Java 核心技術 卷1》 筆記 第七章 圖形程序設計(3)
《Java 核心技術 卷1》 筆記 第七章 圖形程序設計(4)
《Java 核心技術 卷1》 筆記 第七章 圖形程序設計(5)
《Java 核心技術 卷1》 筆記 第七章 圖形程序設計(6) 圖像平鋪
喜歡的話,點個贊吧~!平時做題,以及筆記內容將更新到公眾號。
關注公眾號,互相學習:鈺娘娘知識匯總
總結
以上是生活随笔為你收集整理的《Java 核心技术 卷1》 笔记 第八章 事件处理的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Spring工具类的使用
- 下一篇: 深入分析JavaWeb Item7 --