1. 程式人生 > >201771010125王瑜《面向物件程式設計(Java)》第十三週學習總結

201771010125王瑜《面向物件程式設計(Java)》第十三週學習總結

                  201771010125王瑜《面向物件程式設計(Java)》第十三週學習總結

一.理論部分

1.上週學習了GUI為使用者提供的互動式的圖形化操作介面;AWT包元件,Component元件,Container容器以及Swing元件等;而這周則主要學習到了事件處理,主要知識如下所示:(1)事件處理基礎(2)動作(3)滑鼠事件(4)AWT事件繼承層次

2.事件處理基礎:

(1)事件源:能夠產生事件的物件;一個事件源是一個 能夠註冊監聽器並向監聽器傳送事件物件的物件

(2)事件監聽器:接收事件源傳送的通告並對傳送的事件做出響應;一個監聽器物件就是一個實現了專門監聽器接 口的類例項,該類必須實現介面中的方法,這些方法當 事件發生時,被自動執行

(3)事件物件:Java將事件的相關資訊封裝在一個事件物件中,所有的事件物件都最終派生於java.util.EventObject類;不同的事件源可以產生不 同類別的事件

3.監聽器物件:是一個實現了特定監聽器介面( listener interface)的類例項;事件源:是一個能夠註冊監聽器物件併發送事件對 象的物件;當事件發生時,事件源將事件物件自動傳遞給所 有註冊的監聽器;監聽器物件利用事件物件中的資訊決定如何對事 件做出響應

4.GUI設計中,程式設計師需要對元件的某種事件進行響應和處理時,必須完成兩個步驟:

(1)定義實現某事件監聽器介面的事件監聽器類,並具體化介面中宣告的事件處理抽象方法

(2)為元件註冊實現了規定介面的事件監聽器物件監聽器介面的實現:

5.監聽器類必須實現與事件源相對應的介面,即必 須提供介面中方法的實現

建立按鈕物件 JButton類常用的一組構造方法:

(1) JButton(String text):建立一個帶文字的按扭

(2) JButton(Icon icon) :建立一個帶圖示的按鈕

(3)JButton(String text, Icon icon) :建立一個帶文字和圖示 的按鈕

按鈕物件的常用方法 ① getLabel( ):返回按鈕的標籤字串; ② setLabel(String s):設定按鈕的標籤為字串s

6.動作事件:當特定元件動作(點選按鈕)發生時,該元件生成此動作事件;

(A)該 事 件 被 傳 遞 給 組 件 注 冊 的 每 一 個 ActionListener 物件, 並 調 用 監 聽 器 對 象 的 actionPerformed方法以接收這類事件物件

(B)能夠觸發動作事件的動作,主要包括:

(1)點選按鈕;(2)雙擊一個列表中的選項;(3)選擇選單項;(4)在文字框中輸入回車

7.滑鼠事件:滑鼠監聽器介面;滑鼠監聽器介面卡

使用者點選滑鼠按鈕時,會呼叫三個監聽器方法:

 (1) 滑鼠第一次被按下時呼叫mousePressed方法;

 (2) 滑鼠被釋放時呼叫mouseReleased方法;

 (3)兩個動作完成之後,呼叫mouseClicked方法

8.AWT事件繼承層次:AWT中的事件分類:語義事件以及低層事件

9.(1)所有的事件都是由java.util包中的EventObject 類擴充套件而來

(2)AWTEevent 是所有AWT 事件類的父類, 也是 EventObject的直接子類

(3) 有些Swing元件生成其他型別的事件物件,一般直 接 擴 展 於 EventObject, 而不是AWTEvent,位於 javax.swing.event.*

二.實驗部分:

1、實驗目的與要求

(1) 掌握事件處理的基本原理,理解其用途;

(2) 掌握AWT事件模型的工作機制;

(3) 掌握事件處理的基本程式設計模型;

(4) 瞭解GUI介面元件觀感設定方法;

(5) 掌握WindowAdapter類、AbstractAction類的用法;

(6) 掌握GUI程式中滑鼠事件處理技術。

2、實驗內容和步驟

實驗1: 匯入第11章示例程式,測試程式並進行程式碼註釋。

測試程式1:

l 在elipse IDE中除錯執行教材443頁-444頁程式11-1,結合程式執行結果理解程式;

l 在事件處理相關程式碼處添加註釋;

l 用lambda表示式簡化程式;

l 掌握JButton元件的基本API;

l 掌握Java中事件處理的基本程式設計模型。

package button;   import java.awt.*; import java.awt.event.*; import javax.swing.*;   /** * A frame with a button panel */ public class ButtonFrame extends JFrame {    private JPanel buttonPanel;    private static final int DEFAULT_WIDTH = 300 ;    private static final int DEFAULT_HEIGHT = 200 ;      public ButtonFrame()    {             //調整元件的大小,寬度和高度       setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);         // 建立了三個按鈕物件       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);    }      /**     * An action listener that sets the panel's background color.     */    private class ColorAction implements ActionListener    //ColorAction類後面實現了一個監聽器介面類ActionListener    {       private Color backgroundColor; //Color 類用於封裝預設 sRGB 顏色空間中的顏色,或者用於封裝由 ColorSpace 標識的任意顏色空間中的顏色         public ColorAction(Color c)       {          backgroundColor = c;       }         public void actionPerformed(ActionEvent event)       {          buttonPanel.setBackground(backgroundColor);       }    } }

  

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 package button;   import java.awt.*; import javax.swing.*;   /** * @version 1.34 2015-06-12 * @author Cay Horstmann */ public class ButtonTest {    public static void main(String[] args)    {        //lambda表示式       EventQueue.invokeLater(() -> {          JFrame frame = new ButtonFrame();          frame.setTitle( "ButtonTest" );          //setTitle表示將此窗體的標題設定為制定的字串          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);          //根據引數的值顯示或隱藏視窗          frame.setVisible( true );       });    } }

 

測試程式2:

l 在elipse IDE中除錯執行教材449頁程式11-2,結合程式執行結果理解程式;

l 在元件觀感設定程式碼處添加註釋;

l 瞭解GUI程式中觀感的設定方法。

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 package button;   import java.awt.*; import java.awt.event.*; import javax.swing.*;   /** * A frame with a button panel */ public class ButtonFrame extends JFrame {     private JPanel buttonPanel;     private static final int DEFAULT_WIDTH = 300 * 2 ;     private static final int DEFAULT_HEIGHT = 200 * 2 ;       public ButtonFrame() {         setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);         //用new運算子呼叫構造器         buttonPanel = new JPanel();     //建立按鈕生成了三個按鈕物件         makeButton( "黃色" , Color.yellow);         makeButton( "藍色" , Color.blue);         makeButton( "紅色" , Color.red);         makeButton( "綠色" ,Color.green);         add(buttonPanel);       }       protected void makeButton(String name,Color backgound) {         // 建立按鈕         JButton button = new JButton(name);         // 向面板新增按鈕         buttonPanel.add(button);         // 建立按鈕操作         //方法一:通過內部類方式實現         /*ColorAction action = new ColorAction(backgound);         // associate actions with buttons         button.addActionListener(action);*/         //方法二:匿名內部類方式實現         button.addActionListener( new ActionListener() {                         @Override             public void actionPerformed(ActionEvent e) {                 // TODO 自動生成的方法存根                 buttonPanel.setBackground(backgound);             }         });         //方法三通過lambad表示式實現         button.addActionListener((e)->{             buttonPanel.setBackground(backgound);         });             }       /**      * An action listener that sets the panel's background color.      */     //這是實現了 ActionListener介面的內部類     /*private class ColorAction implements ActionListener {      //實現了標準介面:監聽器介面,類名是ColorAction         private Color backgroundColor;           public ColorAction(Color c) {             backgroundColor = c;         }           public void actionPerformed(ActionEvent event) {             buttonPanel.setBackground(backgroundColor);         }     }*/ }

 執行結果:

測試程式3:

l 在elipse IDE中除錯執行教材457頁-458頁程式11-3,結合程式執行結果理解程式;

l 掌握AbstractAction類及其動作物件;

l 掌握GUI程式中按鈕、鍵盤動作對映到動作物件的方法。

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 package action;   import java.awt.*; import java.awt.event.*; import javax.swing.*;   /** * A frame with a panel that demonstrates color change actions. */ public class ActionFrame extends JFrame { //JPanel是一般輕量級容器    private JPanel buttonPanel;    private static final int DEFAULT_WIDTH = 300 ;    private static final int DEFAULT_HEIGHT = 200 ;      public ActionFrame()    {       setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);         buttonPanel = new JPanel();         //定義動作       Action yellowAction = new ColorAction( "Yellow" , new ImageIcon( "yellow-ball.gif" ),             Color.YELLOW);       Action blueAction = new ColorAction( "Blue" , new ImageIcon( "blue-ball.gif" ), Color.BLUE);       Action redAction = new ColorAction( "Red" , new ImageIcon( "red-ball.gif" ), Color.RED);         // 新增這些操作的按鈕       buttonPanel.add( new JButton(yellowAction));       buttonPanel.add( new JButton(blueAction));       buttonPanel.add( new JButton(redAction));         // 將面板新增到框       add(buttonPanel);         // 將y b和r鍵與名稱相關聯       InputMap imap = buttonPanel.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);       imap.put(KeyStroke.getKeyStroke( "ctrl Y" ), "panel.yellow" );       imap.put(KeyStroke.getKeyStroke( "ctrl B" ), "panel.blue" );       imap.put(KeyStroke.getKeyStroke( "ctrl R" ), "panel.red" );         // 將名稱與操作相關聯       ActionMap amap = buttonPanel.getActionMap();       //ActionMap 提供從 Object(稱為鍵 或 Action 名)到 Action 的對映       amap.put( "panel.yellow" , yellowAction);       //put:新增一個 key 到 action 的繫結       amap.put( "panel.blue" , blueAction);       amap.put( "panel.red" , redAction);    }       public class ColorAction extends AbstractAction    //ColorAction類介面實現了AbstractAction介面的繼承    {       /**        * Constructs a color action.        * @param name the name to show on the button        * @param icon the icon to display on the button        * @param c the background color        */       public ColorAction(String name, Icon icon, Color c)       {          putValue(Action.NAME, name);          //NAME用來儲存動作的 String 名稱的鍵,用於選單或按鈕。          putValue(Action.SMALL_ICON, icon);          //用來儲存小型 Icon(比如 ImageIcon)的鍵          putValue(Action.SHORT_DESCRIPTION, "Set panel color to " + name.toLowerCase());          putValue( "color" , c);       }     //指