狄慧201771010104《面向物件程式設計(java)》第十三週學習總結
實驗十三 圖形介面事件處理技術
實驗時間 2018-11-22
一、知識點總結
1、AWT事件處理機制的概要:
1>監聽器物件是一個實現了特定監聽器介面的類的例項;
2>事件源是一個能夠註冊監聽器物件併發送事件物件的物件;
3>當事件發生時,事件源將事件物件傳遞給所有註冊的監聽器;
4>監聽器物件將利用事件物件中的資訊決定如何對事件做出響應。
2、改變觀感:
在預設情況下,Swing程式使用Metal觀感,可以採用兩種方式改變觀感:
1>在Java安裝的子目錄jre/lib下有一個檔案swing.properties。在這個檔案中,將屬性swing.defaultlaf設定為所希望的觀感類名;
2>動態的改變觀感。需要呼叫靜態的UIManager.setLookAndFeel方法,並提供所想要的觀感類名,然後再呼叫靜態方法SwingUtilities.updataComponentTreeUI來重新整理全部的元件集。
3、用同一個動作響應按鈕、選單項或按鍵的方式:
1>實現一個擴充套件於AbstractAction類的類。多個相關的動作可以使用同一個類;
2>構造一個動作類的物件;
3>使用動作物件建立按鈕或選單項,構造器將從動作物件中讀取標籤文字和圖示;
4>為了能夠通過按鍵觸發動作,必須額外地執行幾步操作。首先定位定位頂層視窗元件;
5>然後,得到頂層元件的WHEN_ANCESTOR_OF_FOCUS_COMPONENT輸入對映。為需要的按鍵建立一個KeyStrike物件。建立一個描述動作字串這樣的動作鍵物件。將(按鍵、動作鍵)對新增到輸入對映中;
6>最後,得到頂層元件的動作對映。將(動作鍵、動作物件)新增到對映中。
4、AWT事件分為底層事件和語義事件。
1>語義事件:表示使用者動作的事件。點選事件;
2>底層事件:形成那些事件的事件。在點選按鈕時,包含了按下滑鼠、連續移動滑鼠、擡起滑鼠事件。
二、實驗內容
1、實驗目的與要求
(1) 掌握事件處理的基本原理,理解其用途;
(2) 掌握AWT事件模型的工作機制;
(3) 掌握事件處理的基本程式設計模型;
(4) 瞭解GUI介面元件觀感設定方法;
(5) 掌握WindowAdapter類、AbstractAction類的用法;
(6) 掌握
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();//生成ButtonFrame類物件frame frame.setTitle("ButtonTest");//設定窗體標題 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//設定關閉按鈕 frame.setVisible(true);//視窗是否可見 }); } }
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;//JPanel 是一般輕量級容器 private static final int DEFAULT_WIDTH = 600;//靜態常量 private static final int DEFAULT_HEIGHT = 400; 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(); // add buttons to panel //新增三個按鈕元件 buttonPanel.add(yellowButton); buttonPanel.add(blueButton); buttonPanel.add(redButton); // add panel to frame add(buttonPanel); // create button actions //生成三個監聽器類物件 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); } /** * An action listener that sets the panel's background color. */ //監聽器類 private class ColorAction implements ActionListener { private Color backgroundColor; public ColorAction(Color c) { backgroundColor = c; } public void actionPerformed(ActionEvent event)//更改容器的背景色 { buttonPanel.setBackground(backgroundColor); } } } ButtonFrame
簡化後
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();//生成ButtonFrame類物件frame frame.setTitle("ButtonTest");//設定窗體標題 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//設定關閉按鈕 frame.setVisible(true);//視窗是否可見 }); }
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;// JPanel 是一般輕量級容器 private static final int DEFAULT_WIDTH = 600;// 靜態常量 private static final int DEFAULT_HEIGHT = 400; public ButtonFrame() { setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);// 設定元件大小 buttonPanel = new JPanel(); add(buttonPanel); makeButton("yellow", Color.YELLOW); makeButton("blue", Color.BLUE); makeButton("red", Color.RED); makeButton("green", Color.GREEN); } /** * An action listener that sets the panel's background color. */ // 監聽器類 /* * private class ColorAction implements ActionListener { private Color * backgroundColor; * * public ColorAction(Color c) { backgroundColor = c; } * * public void actionPerformed(ActionEvent event)// 更改容器的背景色 { * buttonPanel.setBackground(backgroundColor); } } */ public void makeButton(String name, Color backgroundColor) { JButton button = new JButton(name); buttonPanel.add(button); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent enent) { buttonPanel.setBackground(backgroundColor); } }); } }
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();//生成ButtonFrame類物件frame frame.setTitle("ButtonTest");//設定窗體標題 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//設定關閉按鈕 frame.setVisible(true);//視窗是否可見 }); } } ButtonTest
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;// JPanel 是一般輕量級容器 private static final int DEFAULT_WIDTH = 600;// 靜態常量 private static final int DEFAULT_HEIGHT = 400; public ButtonFrame() { setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);// 設定元件大小 buttonPanel = new JPanel(); add(buttonPanel); makeButton("yellow", Color.YELLOW); makeButton("blue", Color.BLUE); makeButton("red", Color.RED); makeButton("green", Color.GREEN); } /** * An action listener that sets the panel's background color. */ public void makeButton(String name, Color backgroundColor) { JButton button = new JButton(name); buttonPanel.add(button); button.addActionListener((e)-> { buttonPanel.setBackground(backgroundColor); }); } } ButtonFrame
測試程式2:
l 在elipse IDE中除錯執行教材449頁程式11-2,結合程式執行結果理解程式;
l 在元件觀感設定程式碼處添加註釋;
l 瞭解GUI程式中觀感的設定方法。
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);
});
}
} PlafTest
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();//調整視窗大小
} /**
* 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();
}
catch (Exception e)
{
e.printStackTrace();//列印堆疊資訊
}
});
}
} PlafFrame
測試程式3:
l 在elipse IDE中除錯執行教材457頁-458頁程式11-3,結合程式執行結果理解程式;
l 掌握AbstractAction類及其動作物件;
l 掌握GUI程式中按鈕、鍵盤動作對映到動作物件的方法。
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); }); } } ActionTest
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 提供輸入事件 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();//返回用於確定為特定 KeyStroke 繫結觸發何種 Action 的 ActionMap amap.put("panel.yellow", yellowAction); amap.put("panel.blue", blueAction); amap.put("panel.red", redAction); } public class ColorAction extends 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);//設定與指定鍵關聯的 Value 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);//設定元件的背景色 } } } ActionFrame
測試程式4:
l 在elipse IDE中除錯執行教材462頁程式11-4、11-5,結合程式執行結果理解程式;
l 掌握GUI程式中滑鼠事件處理技術。
import java.awt.*; import javax.swing.*; import mouse.MouseFrame; /** * @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); }); } } MouseTest
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();//調整視窗大小 } } MouseFrame
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;//Rectangle2D 類描述通過位置 (x,y) 和尺寸 (w x h) 定義的矩形。 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); } //Dimension 類封裝單個物件中元件的寬度和高度 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();//返回Point2D 的 X 座標 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));//setCursor為指定的游標設定游標影象 } 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(); } } } } MouseComponent
實驗2:結對程式設計練習
利用班級名單檔案、文字框和按鈕元件,設計一個有如下介面(圖1)的點名器,要求使用者點選開始按鈕後在文字輸入框隨機顯示2017級網路與資訊安全班同學姓名,如圖2所示,點選停止按鈕後,文字輸入框不再變換同學姓名,此同學則是被點到的同學姓名。
package 點名器啦; 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(100,100,180,30); show.setFont(new Font("宋體",Font.BOLD,25)); add(but); add(show); setLayout(null);//佈局管理器必須先初始化為空才能賦值 setVisible(true); setResizable(false); setBounds(200,200,400,400); //setBackground(Color.red);不起作用 this.getContentPane().setBackground(Color.pink); 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("E:\\java\\第十三週\\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); } } }
小夥伴:李瑞紅
三、實驗總結
本章我們學習了圖形介面事件處理技術的知識,首先掌握了事件處理的基本原理,並學會了事件處理的基本程式設計模型;在老師的演示程式碼過程中,我清楚的學習到了lambda表示式的簡便性,簡化程式碼的好處和程式碼的多變性;在實驗中,通過助教學長的演示和在書上原始碼基礎上進行的改編、新增,做出了點名器的程式碼程式,感覺又是一次充實的實驗。