1. 程式人生 > >劉誌梅2017710101152.《面向對象程序設計(java)》第十三周學習總結

劉誌梅2017710101152.《面向對象程序設計(java)》第十三周學習總結

繪制 下界 ner utili spa ktr err nmon gif

實驗十三 圖形界面事件處理技術

實驗時間 2018-11-22

1、理論知識

(1)任何支持GUI的操作環境都要不斷地監視按鍵或點擊鼠標這樣的事件。

JAVA這樣的面向對象語言,都將事件的相關的信息封裝在一個事件對象中。

不同的事件源可以產生不同類別的事件。

(2)AWT事件處理機制的概要:監聽器對象是一個實現了特定監聽器接口的類的實例;事件源是一個能夠註冊監聽器對象的並發送事件對象的對象;當事件發生時,事件源將事件對象傳遞給所有註冊的監聽器;監聽器對象將利用事件對象中的信息決定如何對事件作出響應。

為了實現ActionListeer接口,監聽器類必須有一個被稱為actionPerformed的方法,該方法接收一個ActionEvent對象參數。

每個監聽器執行一個單獨的動作。

在默認情況下,Swing程序使用Metal觀感,可以采用兩種方式改變觀感(1.在Java安裝的子目錄jre/lib下有一個文件swing.properties,在這個文件中,將屬性swing.defaultlaf設置為所希望的觀感類名;2.動態的改變觀感)。

窗口監聽器必須是實現WindowListener接口的類的一個對象。

(3)將一個監聽器對象加到下面幾個事件源上:標記為Blue的工具欄按鈕;標記為Blue的菜單項;按鍵CTRL+B。

一個動作是一個封裝下列內容的對象:命令的說明(一個文本字符串和一個可選圖標);執行命令所需要的參數。

如果動作對象添加到菜單或工具欄上,它的名稱和圖標就會被自動的提取出來,並顯示在菜單項或工具欄項中(

Action是一個接口,而不是一個類)。

用戶界面中可以包含許多按鈕、菜單、滾動欄以及其他組件。

習慣上,使用字符串none表示空動作。

用同一個動作響應按鈕、菜單項或按鍵的方式:實現一個擴展於AbstractAction類的類(多個相關的動作可以使用同一個類);構造一個動作類的對象;使用動作對象創建按鈕或菜單項(構造器將從動作對象中讀取標簽文本和圖標);為了能夠通過按鍵觸發動作,必須額外的執行幾步操作(首先定位頂層窗口組件);然後,得到頂層組件的WHEN_ANCESTOR_OF_FOCUS_COMPONENT輸入映射;最後得到頂層組件的動作映射。

4)鼠標操作將由用戶界面中的各種組件內部處理。

當用戶點擊鼠標按鈕時,將會調用三個監聽器方法:鼠標第一次被按下時調用mousePressed;鼠標被釋放時調用mouseReleased;最後調用mouseClicked。

可以采用位掩碼來測試已經設置了哪個修飾符。

當鼠標在窗口移動時,窗口會收到一連串的鼠標移動事件;在程序中僅僅用拖動的矩形更新當前光標位置。

兩個鼠標事件方法:mouseEntered和mouseExited。

(5) 事件對象封裝了事件源與監聽器彼此通信的事件信息。

AWT將事件分為底層事件和語義事件;語義事件是表示用戶動作的事件;底層事件是形成那些事件的事件。

常用的語義事件類:ActionEvent(對應按鈕點擊、菜單選擇、選擇列表項或在文本框中ENTER);AdjustmentEvent(用戶調節滾動條);ItemEvent(用戶從復選框或列表框中任選一項)。

常用的5個底層事件類:KeyEvent(一個鍵被按下或釋放);MouseEvent(鼠標鍵被按下、釋放、移動或拖動);MouseWheelEvent(鼠標滾輪被轉動);FocusEvent(某個組件獲得焦點或失去焦點);WindowEvent(窗口狀態被改變)。

2、實驗內容和步驟

實驗1: 導入第11示例程序,測試程序並進行代碼註釋。

測試程序1:

l elipse IDE中調試運行教材443-444頁程序11-1,結合程序運行結果理解程序;

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

l lambda表達式簡化程序;

l 掌握JButton組件的基本API;

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

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;//寬度值300像素
   private static final int DEFAULT_HEIGHT = 200;//高度值200像素

   public ButtonFrame()
   {      
      setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

      // create buttons
      JButton yellowButton = new JButton("Yellow");
      JButton blueButton = new JButton("Blue");
      JButton redButton = new JButton("Red");

      buttonPanel = new JPanel();//創建JPanel

      // add buttons to panel
      buttonPanel.add(yellowButton);
      buttonPanel.add(blueButton);
      buttonPanel.add(redButton);

      // add panel to frame
      add(buttonPanel);
      //為每種顏色new一個對象
      // create button actions
      ColorAction yellowAction = new ColorAction(Color.YELLOW);
      ColorAction blueAction = new ColorAction(Color.BLUE);
      ColorAction redAction = new ColorAction(Color.RED);//生成三個相同的類對象(colouraction)
      //(註冊機制)
      // associate actions with buttons
      yellowButton.addActionListener(yellowAction);
      blueButton.addActionListener(blueAction);
      redButton.addActionListener(redAction);
   }

   /**
    * An action listener that sets the panel‘s background color.
    */
   private class ColorAction implements ActionListener
   {    //監聽器接口ActionListener(監聽器類且類名為ColorAction)
      private Color backgroundColor;

      public ColorAction(Color c)
      {
         backgroundColor = c;
      }

      public void actionPerformed(ActionEvent event)//ActionListener只有一個方法actionPerformed
      {
         buttonPanel.setBackground(backgroundColor);
      }
   }
}
//只有容器組件有add方法

修改後的:

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;//寬為300px
   private static final int DEFAULT_HEIGHT = 200;//長為200px
//註釋掉這個構造器之後只能顯示一個窗格,面板上沒有內容
/*   public ButtonFrame()//構造器
   {      
      setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);//決定框架大小

      // create buttons  創建按鈕
      JButton yellowButton = new JButton("Yellow");//Yellow字符串是顯示在按鈕上的文本
      JButton blueButton = new JButton("Blue");
      JButton redButton = new JButton("Red");

      buttonPanel = new JPanel();//使 buttonPanel指向一個具體的容器對象

      // add buttons to panel
      buttonPanel.add(yellowButton);//將按鈕添加到面板中
      buttonPanel.add(blueButton);
      buttonPanel.add(redButton);

      // add panel to frame
      add(buttonPanel);

      // create button actions 
      //生成三個監聽器類對象   當事件發生時,事件源將事件對象傳遞給所有註冊的監聽器
      //顏色值在Color類中,通過類名調用
      ColorAction yellowAction = new ColorAction(Color.YELLOW);
      ColorAction blueAction = new ColorAction(Color.BLUE);
      ColorAction redAction = new ColorAction(Color.RED);

      // associate actions with buttons  將操作與按鈕相關聯
      yellowButton.addActionListener(yellowAction);
      //註釋掉之後程序可以運行,點擊按鈕,背景色不變
      blueButton.addActionListener(blueAction);
      redButton.addActionListener(redAction);
   }*/
   public ButtonFrame()//構造器
   {   
      buttonPanel = new JPanel();//使 buttonPanel指向一個具體的容器對象
      setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);//決定框架大小
      add(buttonPanel);
      makeButton("Yellow",Color.YELLOW);
      makeButton("blue",Color.BLUE);
      makeButton("red",Color.RED);
      makeButton("green",Color.GREEN);
   }
    public void makeButton(String name,Color BackgroundColor)
    {
        JButton button = new JButton(name);
        buttonPanel.add(button);
        ColorAction action = new ColorAction(BackgroundColor);//初始化一個顏色對象
        button.addActionListener(action);//添加一個事件監聽器
    }
   /**
    * An action listener that sets the panel‘s background color.
    */
   //監聽器類,類名是ColorAction
   private class ColorAction implements ActionListener//實現了一個監聽器接口
   {
      private Color backgroundColor;

      public ColorAction(Color c)
      {
         backgroundColor = c;
      }

      public void actionPerformed(ActionEvent event)
      {
         buttonPanel.setBackground(backgroundColor);
      }
   }
}

技術分享圖片

技術分享圖片

測試程序2

l elipse IDE中調試運行教材449頁程序11-2,結合程序運行結果理解程序;

l 在組件觀感設置代碼處添加註釋;

l 了解GUI程序中觀感的設置方法。

import java.awt.*;
import javax.swing.*;

/**
 * @version 1.32 2015-06-12
 * @author Cay Horstmann
 */
public class PlafTest
{
   public static void main(String[] args)
   {
      EventQueue.invokeLater(() -> {
         JFrame frame = new PlafFrame();
         frame.setTitle("PlafTest");//框架標題(“PlafTest”)
         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         frame.setVisible(true);
      });
   }
}

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

/**
 * A frame with a button panel for changing look-and-feel
 */
public class PlafFrame extends JFrame
{
   private JPanel buttonPanel;

   public PlafFrame()//按鈕面板
   {
      buttonPanel = new JPanel();

      UIManager.LookAndFeelInfo[] infos = UIManager.getInstalledLookAndFeels();
      for (UIManager.LookAndFeelInfo info : infos)
         makeButton(info.getName(), info.getClassName());

      add(buttonPanel);
      pack();
   }

   /**
    * Makes a button to change the pluggable look-and-feel.
    * @param name the button name
    * @param className the name of the look-and-feel class
    */
   private void makeButton(String name, String className)
   {
      // add button to panel

      JButton button = new JButton(name);
      buttonPanel.add(button);

      // set button action

      button.addActionListener(event -> {
         // button action: switch to the new look-and-feel
         try
         {
            UIManager.setLookAndFeel(className);
            SwingUtilities.updateComponentTreeUI(this);
            pack();//調用靜態UIManager.setLookAndFeel方法
         }
         catch (Exception e)
         {
            e.printStackTrace();
         }
      });
   }
}

技術分享圖片

測試程序3

l elipse IDE中調試運行教材457-458頁程序11-3,結合程序運行結果理解程序;

l 掌握AbstractAction類及其動作對象;

l 掌握GUI程序中按鈕、鍵盤動作映射到動作對象的方法。

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
{
   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();

      // define actions
      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);

      // add buttons for these actions
      buttonPanel.add(new JButton(yellowAction));
      buttonPanel.add(new JButton(blueAction));
      buttonPanel.add(new JButton(redAction));
      //為這些動作添加按鈕
      // add panel to frame
      add(buttonPanel);

      // associate the Y, B, and R keys with names
      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");

      // associate the names with actions
      ActionMap amap = buttonPanel.getActionMap();
      amap.put("panel.yellow", yellowAction);
      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)//公共類ColorAction(字符串名稱,圖標,顏色)
      {
         putValue(Action.NAME, name);
         putValue(Action.SMALL_ICON, icon);
         putValue(Action.SHORT_DESCRIPTION, "Set panel color to " + name.toLowerCase());
         putValue("color", c);
      }

      public void actionPerformed(ActionEvent event)
      {
         Color c = (Color) getValue("color");
         buttonPanel.setBackground(c);
      }
   }
}

技術分享圖片

測試程序4

l elipse IDE中調試運行教材462頁程序11-411-5,結合程序運行結果理解程序;

l 掌握GUI程序中鼠標事件處理技術。

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.util.*;
import javax.swing.*;

/**
 * A component with mouse operations for adding and removing squares.
 */
public class MouseComponent extends JComponent
{
   private static final int DEFAULT_WIDTH = 300;//寬度定義為300像素
   private static final int DEFAULT_HEIGHT = 200;//高度為200像素

   private static final int SIDELENGTH = 10;
   private ArrayList<Rectangle2D> squares;
   private Rectangle2D current; // the square containing the mouse cursor

   public MouseComponent()
   {
      squares = new ArrayList<>();
      current = null;//當前為零

      addMouseListener(new MouseHandler());
      addMouseMotionListener(new MouseMotionHandler());
   }

   public Dimension getPreferredSize() { return new Dimension(DEFAULT_WIDTH, DEFAULT_HEIGHT); }   
   
   public void paintComponent(Graphics g)
   {
      Graphics2D g2 = (Graphics2D) g;

      // draw all squares
      for (Rectangle2D r : squares)
         g2.draw(r);
   }

   /**
    * Finds the first square containing a point.
    * @param p a point
    * @return the first square that contains p
    */
   public Rectangle2D find(Point2D p)
   {
      for (Rectangle2D r : squares)
      {
         if (r.contains(p)) return r;
      }
      return null;//返回null
   }

   /**
    * Adds a square to the collection.
    * @param p the center of the square
    */
   public void add(Point2D p)
   {
      double x = p.getX();
      double y = p.getY();

      current = new Rectangle2D.Double(x - SIDELENGTH / 2, y - SIDELENGTH / 2, SIDELENGTH,
            SIDELENGTH);
      squares.add(current);
      repaint(); //重畫
   }

   /**
    * Removes a square from the collection.
    * @param s the square to remove
    */
   public void remove(Rectangle2D s)
   {
      if (s == null) return;
      if (s == current) current = null;
      squares.remove(s);
      repaint();
   }

   private class MouseHandler extends MouseAdapter
   {
      public void mousePressed(MouseEvent event)
      {
         // add a new square if the cursor isn‘t inside a square
         current = find(event.getPoint());
         if (current == null) add(event.getPoint());
      }

      public void mouseClicked(MouseEvent event)
      {
         // remove the current square if double clicked
         current = find(event.getPoint());
         if (current != null && event.getClickCount() >= 2) remove(current);
      }
   }

   private class MouseMotionHandler implements MouseMotionListener
   {
      public void mouseMoved(MouseEvent event)
      {
         // set the mouse cursor to cross hairs if it is inside
         // a rectangle

         if (find(event.getPoint()) == null) setCursor(Cursor.getDefaultCursor());
         else setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
      }

      public void mouseDragged(MouseEvent event)
      {
         if (current != null)
         {
            int x = event.getX();
            int y = event.getY();

            // drag the current rectangle to center it at (x, y)
            current.setFrame(x - SIDELENGTH / 2, y - SIDELENGTH / 2, SIDELENGTH, SIDELENGTH);
            repaint();//重新繪制畫布,以顯示新的鼠標位置
         }
      }
   }   
}

import javax.swing.*;

/**
 * A frame containing a panel for testing mouse operations
 */
public class MouseFrame extends JFrame
{
   public MouseFrame()//公共類MouseFrame
   {
      add(new MouseComponent());
      pack();
   }
}
import java.awt.*;
import javax.swing.*;

/**
 * @version 1.34 2015-06-12
 * @author Cay Horstmann
 */
public class MouseTest
{
   public static void main(String[] args)
   {
      EventQueue.invokeLater(() -> {
         JFrame frame = new MouseFrame();
         frame.setTitle("MouseTest");
         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         frame.setVisible(true);
      });
   }
}

  

技術分享圖片

技術分享圖片

實驗2:結對編程練習

利用班級名單文件、文本框和按鈕組件,設計一個有如下界面(圖1)的點名器,要求用戶點擊開始按鈕後在文本輸入框隨機顯示2017級網絡與信息安全班同學姓名,如圖2所示,點擊停止按鈕後,文本輸入框不再變換同學姓名,此同學則是被點到的同學姓名。

import java.awt.*;
import java.awt.event.*;
import java.util.*;


public class dian {//公關類dian
public static void main(String []args){

MyFrame f = new MyFrame();
f.launchFrame();

}
}


class MyFrame extends Frame{
Label text2;
void launchFrame(){
setTitle("學生點名系統");
setVisible(true);
Button btn1 = new Button("開始");
Button btn2 = new Button("結束");
Label text1 = new Label("學號:");
text2 = new Label("按下開始按鈕開始點名");
Panel p=new Panel();

setBounds(300,300,300,400);

p.setBackground(Color.white);
p.setBounds(0,0,300,400);
p.setLayout(new FlowLayout(FlowLayout.CENTER,60,100));
//btn1.setBounds(20,20,50,20);
//btn2.setBounds(90,20,50,20);


add(p);
p.add(text1);//添加text1
p.add(text2);//添加text2
p.add(btn1);//添加btn1
p.add(btn2);//添加btn2
this.addWindowListener(new MyWindowMoniter());
btn1.addActionListener(new btnMoniter(this));
btn2.addActionListener(new btnMoniter(this));
}
}


class btnMoniter implements ActionListener {
MyFrame f;
btnMoniter(MyFrame f){
this.f=f;
}
public void actionPerformed(ActionEvent e){
String s=e.getActionCommand();
Random r=new Random();
int i=r.nextInt(50);
if(s=="開始"){ 
f.repaint();
f.text2.setText("正在點名!");
}
if(s=="結束"){//如果s意為結束
f.repaint();//重新一次
f.text2.setText(""+i);
}

}
}

//window時間監聽器,用於關閉window窗口
class MyWindowMoniter extends WindowAdapter{
public void windowClosing(WindowEvent e){
System.exit(0);//退出
}
}

技術分享圖片

實驗總結:通過本周實驗學習了一些常見事件如鼠標移動。單擊鼠標按鈕、在文本框中進行輸入等等,以及事件處理、動作、鼠標事件和AWT事件繼承層次;事件處理基礎包含:事件源、事件監聽器與事件對象;此外還有監聽器接口的實現;最後本周作業的編程有問題,代碼不熟悉,在網上搜索然後解讀代碼,希望學長可以詳細講解一下。

劉誌梅2017710101152.《面向對象程序設計(java)》第十三周學習總結