1. 程式人生 > >馮誌霞201771010107《面向對象程序設計(java)》第十三周學習

馮誌霞201771010107《面向對象程序設計(java)》第十三周學習

必須 align 管理 fin xtend div ossh 不同 sets

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

實驗時間 2018-11-22

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 javax.swing.*;

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

  

package button;

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

public class ButtonFrame extends JFrame {
	private JPanel buttonPanel;// 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");// 帶有string參數顯示text文本
		JButton blueButton = new JButton("Blue");
		JButton redButton = new JButton("Red");
		JButton GreenButton = new JButton("Greeen");*/
		// 必須要放在容器中
		buttonPanel = new JPanel();

		/*
		 * // 將按鈕組件添加到面板,,,只有容器組件有ADD方法 buttonPanel.add(yellowButton);
		 * buttonPanel.add(blueButton); buttonPanel.add(redButton);
		 */
		// add panel to frame
		add(buttonPanel);

		/*
		 * create button actions ColorAction yellowAction = new
		 * ColorAction(Color.gray);//類名調用Color.gray ColorAction blueAction = new
		 * ColorAction(Color.BLUE); ColorAction redAction = new
		 * ColorAction(Color.RED);//3監聽器
		 * 
		 * // associate actions with buttons
		 * yellowButton.addActionListener(yellowAction);//zhucejizhi
		 * blueButton.addActionListener(blueAction);//註冊監聽器類對象的為事件源
		 * redButton.addActionListener(redAction);
		 */

		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);
		button.addActionListener((e)->          //匿名內部類不需要定義監聽器類
				{

					
						buttonPanel.setBackground(backgroundColor);
				

	});
	}

	/**
	 * An action listener that sets the panel‘s background color.
	 */
	/*private abstract class ColorAction implements ActionListener// 自定義類實現監聽器接口才可以創建見監聽器對象,此為監聽器類coloraction
	{
		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程序中觀感的設置方法。

package plaf;

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

   
   private void makeButton(String name, String className)
   {
      //將按鈕添加到面板中

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

      // 通過監聽器對象給按鈕添加動作事件

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

  

package plaf;

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");
         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         frame.setVisible(true);
      });
   }
}

  技術分享圖片技術分享圖片

技術分享圖片 技術分享圖片技術分享圖片

測試程序3:

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

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

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

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
{
   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);//InputMap 提供輸入事件(目前只使用 KeyStroke)和 Object 之間的綁定。InputMap 通常與 ActionMap 一起使用,以確定按下鍵時執行一個 Action
      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();//ActionMap 提供從 Object(稱為鍵 或 Action 名)到 Action 的映射。當按下某一個鍵時,ActionMap 通常與 InputMap 一起使用來定位特定操作
      amap.put("panel.yellow", yellowAction);
      amap.put("panel.blue", blueAction);
      amap.put("panel.red", redAction);
   }
   
   public class ColorAction extends AbstractAction
   {
      
      public ColorAction(String name, Icon icon, Color c)
      {
    	  //putValue設置與指定鍵關聯的 Value。指定者:接口 Action 中的 putValue 

         putValue(Action.NAME, name);
         putValue(Action.SMALL_ICON, icon);
         putValue(Action.SHORT_DESCRIPTION, "Set panel color to " + name.toLowerCase());//將此 String 中的所有字符都轉換為小寫
         putValue("color", c);
      }

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

  

package action;

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

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

  技術分享圖片

測試程序4:

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

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

package mouse;

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;
   private static final int DEFAULT_HEIGHT = 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());//添加指定的鼠標偵聽器,以接收發自此組件的鼠標事件。如果偵聽器 l 為 null,則不會拋出異常並且不執行動作。 
      addMouseMotionListener(new MouseMotionHandler());
   }

   public Dimension getPreferredSize() { return new Dimension(DEFAULT_WIDTH, DEFAULT_HEIGHT); } //如果 preferredSize 已設置為一個非 null 值,則返回該值  
   
   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;
   }

   /**
    * 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();//返回事件相對於源組件的水平 x 坐標
            int y = event.getY();//返回事件相對於源組件的垂直 y 坐標。

            // drag the current rectangle to center it at (x, y)
            current.setFrame(x - SIDELENGTH / 2, y - SIDELENGTH / 2, SIDELENGTH, SIDELENGTH);//將此 Rectangle2D 外部邊界的位置和大小設置為指定矩形值。
            repaint();
         }
      }
   }   
}

  

package mouse;

import javax.swing.*;

/**
 * A frame containing a panel for testing mouse operations
 */
public class MouseFrame extends JFrame
{
   public MouseFrame()
   {
      add(new MouseComponent());
      pack();
   }
}

  

package mouse;

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.util.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.Frame;
import java.io.File;
import java.io.FileNotFoundException;


public class 點名器1 extends JFrame implements ActionListener{
	private JButton but ;

	private JButton show;
	private static boolean flag = true;
	public static void main(String arguments []) {
		 new 點名器1();

		}

public  點名器1(){

but = new JButton("開始");
but.setBounds(100,150,100,40);

show = new JButton("隨機點名");
show.setBounds(80,80,180,30);
show.setFont(new Font("楷體",Font.BOLD,30));

add(but);

add(show);

setLayout(null);//布局管理器必須先初始化為空才能賦值
setVisible(true);
setResizable(false);
setBounds(100,100,300,300);
//setBackground(Color.red);不起作用
this.getContentPane().setBackground(Color.cyan);
setTitle("點名");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

but.addActionListener(this);
}

	public void actionPerformed(ActionEvent e){
    	int i=0;
    	String names[]=new String[50];
    	try {
			Scanner in=new Scanner(new File("D:\\studentnamelist.txt"));
			while(in.hasNextLine())
			{
				names[i]=in.nextLine();
				i++;
			}
		} catch (FileNotFoundException e1) {
			
			e1.printStackTrace();
		}

	
    	if(but.getText()=="開始"){  	

	show.setBackground(Color.BLUE);
	flag=true;
	new Thread(){   
        public void run(){
            while(點名器1.flag){
            Random r = new Random(); 
            int i= r.nextInt(47);
            show.setText(names[i]);
            }
        }
    }.start();
    but.setText("停止");//更改文本內容
    but.setBackground(Color.YELLOW);
}    
else if(but.getText()=="停止"){
    flag = false;
    but.setText("開始");
    but.setBackground(Color.WHITE);
    show.setBackground(Color.red);
}
	}



}

  技術分享圖片

理論知識總結:①創建容器

  首先要創建一個GUI應用程序,需要創建一個用於容納所有其它GUI組件元素的載體,Java中稱為容器。典型的包括窗口(Window)、框架(Frame/JFrame)、對話框(Dialog/JDialog)、面板(Panel/JPanel)等。只有先創建了這些容器,其它界面元素如按鈕(Button/JButton)、標簽(Label/JLabel)、文本框(TextField/JTextField)等才有地方放。

  ②添加組件

  為了實現GUI應用程序的功能,為了與用戶交換,需要在容器上添加各種組件/控件。這需要根據具體的功能要求來決定用什麽組件。例如,如果需要提示信息,可用標簽(Label/JLabel);如果需要輸入少量文本,可用文本框(TextField/JTextField);如果需要輸入較多文本,可用文本區域(TextArea/JTextArea);如果需要輸入密碼,可用密碼域(JPasswordField)等等。

  ③安排組件
  與傳統的Windows環境下的GUI軟件開發工具不同,為了更好地實現跨平臺,Java程序中各組件的位置、大小一般不是以絕對量來衡量,而是以相對量來衡量。例如有時候,程序的組件的位置是按"東/East"、"西/West"、"南/South"、"北/North"、"中 /Center"這種方位來標識的。因此,在組織界面時,除了要考慮所需的組件種類外,還需要考慮如何安排這些組件的位置與大小。這一般是通過設置布局管理器(Layout Manager)及其相關屬性來實現的。事實上上述按方位來安排組件就是采用了Java中多種布局管理器裏的BorderLayout布局管理器。

  ④處理事件

  為了完成一個GUI應用程序所應具備的功能,除了適當地安排各種組件產生美觀的界面外,還需要處理各種界面元素事件,以便真正實現與用戶的交換,完成程序的功能。在Java程序中這一般是通過實現適當的事件監聽者接口來完成的。比如如果需要響應按鈕事件,就需要實現 ActionListener監聽者接口;如果需要響應窗口事件,就需要實現WindowListener監聽者接口。

java采用的是事件源——事件監聽器模型
1:事件(event):是指一個狀態的改變,或者一個活動的發生。例如,簡單常見的是單擊一個按鈕,或者輸入一個按鍵都是一個事件。
2:事件源(event source):能夠產生事件的對象稱為事件源,如文本框,按鈕,下拉列表等。
3:事件監聽器(event listener):需要一個對象對事件源進行監聽,以便對發生的事件做出處理,該對象就是事件監聽器。事件源通過調用相應的方法將某個對象作為自己的監聽器。無論應用程序還是小程序都采用這一機制。
例,對於按鈕,註冊監聽器的方法是 addActionListener(監視器);:
4: 事件源獲得監聽器之後,相應的操作就會導致事件的發生,並通知監聽器,監聽器就會做出相應的處理。
5:處理事件的接口:監聽器負責處理事件源發生的事件。為了處理事件源發生的事件,監聽器會自動調用一個方法來處理事件,該方法定義在相應事件的接口中。

馮誌霞201771010107《面向對象程序設計(java)》第十三周學習