蘇浪浪 201771010120 面向物件程式設計(Java)第13周
/實驗十三 圖形介面事件處理技術
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; privatestatic final int DEFAULT_WIDTH = 300;//寬300 private static final int DEFAULT_HEIGHT = 200;//高200 public ButtonFrame() { setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT); // 建立按鈕 JButton orangeButton = new JButton("Orange");//建立一個帶文字的按鈕。 JButton blueButton = new JButton("blue"); JButton greyButton= new JButton("Grey"); buttonPanel = new JPanel(); // 向面板新增按鈕 buttonPanel.add(orangeButton); buttonPanel.add(blueButton); buttonPanel.add(greyButton); // 向框架新增面板 add(buttonPanel); // 建立按鈕操作 ColorAction orangeAction= new ColorAction(Color.ORANGE); ColorAction blueAction = new ColorAction(Color.BLUE); ColorAction greyAction = new ColorAction(Color.GRAY); // 將操作與按鈕相關聯 orangeButton.addActionListener(orangeAction); blueButton.addActionListener(blueAction); greyButton.addActionListener(greyAction); } /** * 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); } } }
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.*; /** * 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); buttonPanel = new JPanel(); 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. */ private class ColorAction implements ActionListener { private Color backgroundColor; public ColorAction(Color c) { backgroundColor = c; } public void actionPerformed(ActionEvent event) { buttonPanel.setBackground(backgroundColor); } } }
再度改進:(匿名內部類)
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); buttonPanel = new JPanel(); 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); button.addActionListener(new ActionListener() { 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(); //為了配置選單或為了初始應用程式設定而提供關於已安裝的 LookAndFeel 的少量資訊 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) { // 向面板新增按鈕 JButton button = new JButton(name); buttonPanel.add(button); //設定按鈕操作 button.addActionListener(event -> { // 按鈕動作:切換到新的外觀 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(); // 定義的行為 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(); 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); 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); } } }
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; //包含滑鼠游標的正方形 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; // 畫出所有方塊 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) { //如果游標不在正方形內,則新增一個新的正方形 current = find(event.getPoint()); if (current == null) add(event.getPoint()); } public void mouseClicked(MouseEvent event) { //如果雙擊,則刪除當前方塊 current = find(event.getPoint()); if (current != null && event.getClickCount() >= 2) remove(current); } } private class MouseMotionHandler implements MouseMotionListener { public void mouseMoved(MouseEvent event) { // 如果滑鼠在內部,則將滑鼠游標設定為十字線 // 一個矩形 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(); //拖動當前矩形,使其居中(x, y) current.setFrame(x - SIDELENGTH / 2, y - SIDELENGTH / 2, SIDELENGTH, SIDELENGTH); 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所示,點選停止按鈕後,文字輸入框不再變換同學姓名,此同學則是被點到的同學姓名。
圖1 點名器啟動介面
圖2 點名器點名介面
百度程式:
package personal; import java.awt.Color; import java.awt.Font; import java.awt.Rectangle; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.image.BufferedImage; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Random; import java.util.Scanner; import javax.imageio.ImageIO; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextArea; public class StartJFrame extends JFrame{ private static final long serialVersionUID = 1L; JFrame jframe= new JFrame("窗體生成"); JPanel jpanel=null; JPanel imagePanel = null; BufferedImage image= null; JLabel label3 = new JLabel(); ImageIcon background = new ImageIcon(); JTextArea jtext = new JTextArea(); JButton jbutton1=new JButton("開始"); JButton jbutton2=new JButton("暫停"); JButton jbutton3=new JButton("確定"); String strPath = ""; public static boolean flag = true;//判斷開始按鈕是否被點過 private static Thread t; private int count = 0; public StartJFrame(){ //新增文字 jpanel = (JPanel)this.getContentPane();//每次新增必須要加的語句 Font font = new Font("",Font.BOLD,30); //新增按鈕 jpanel=(JPanel)this.getContentPane(); jpanel.setLayout(null); //(左,上,寬,高) jbutton3.setBounds(new Rectangle(330,180,60,20)); jbutton3.addActionListener(new TextValue(this)); jpanel.add(jbutton3); //新增文字框(左,上,寬,高) jtext.setBounds(40, 180, 260, 20); jpanel.add(jtext); } /** * 重寫構造器 */ public StartJFrame(String str){ //將路徑傳入開始按鈕 strPath = str; //新增提示文字 jpanel = (JPanel)this.getContentPane();//每次新增必須要加的語句 JLabel label2 = new JLabel("點名開始啦!!!"); Font font = new Font("",Font.BOLD,30); label2.setFont(font); label2.setForeground(Color.black); label2.setBounds(100,20,450,100); jpanel.add(label2); //顯示名字資訊 label3.setBounds(150,120,450,100); //設定字型顏色 label3.setForeground(Color.yellow); //新增按鈕 jpanel=(JPanel)this.getContentPane(); jpanel.setLayout(null); jbutton1.setBounds(new Rectangle(100,300,75,25)); jpanel.add(jbutton1); jbutton1.addActionListener(new Action(this)); jbutton2.setBounds(new Rectangle(250,300,75,25)); jpanel.add(jbutton2); jbutton2.addActionListener(new Stop(this)); } /** * 從控制檯輸入路徑 */ public static String InputPath(){ String str =""; System.out.println("F:\\xll.txt"); Scanner sc= new Scanner(System.in); str = sc.nextLine(); return str; } /** * 讀取文件資料 * @param filePath * @return */ public static String ReadFile(String filePath){ String str = ""; try { String encoding="GBK"; File file = new File(filePath); if(file.isFile()&&file.exists()){ InputStreamReader reader = new InputStreamReader(new FileInputStream(file),encoding); BufferedReader bufferedReader = new BufferedReader(reader); String lineTxt = ""; while((lineTxt = bufferedReader.readLine()) != null){ str+=lineTxt+";\n"; } reader.close(); }else{ System.out.println("找不到指定的檔案"); } }catch (Exception e) { System.out.println("讀取檔案內容出錯"); e.printStackTrace(); } return str; } /** * 將字串轉換為String陣列 */ public static String[] ChangeType(String str){ ArrayList<String> list=new ArrayList<String>(); String[] string = str.split(";"); return string; } /** * main方法 * @param args */ public static void main(String args[]){ StartJFrame jframe=new StartJFrame(); jframe.setTitle("點名器"); jframe.setSize(550,400); jframe.setVisible(true); jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jframe.setResizable(false); jframe.setLocationRelativeTo(null); System.out.println(); } /** * 點選確定按鈕後的方法 */ public void chooseValue(ActionEvent e){ String str = ""; str = jtext.getText(); if(str != "" || str != null){ StartJFrame jframe = new StartJFrame(str); jframe.setTitle("點名器"); jframe.setSize(550,500); jframe.setVisible(true); jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jframe.setResizable(false); jframe.setLocationRelativeTo(null); System.out.println(str); } } /** * 點選開始按鈕後的方法 */ public void actionRun(ActionEvent e){ if(flag){ //執行緒開始 t = new Thread(new Runnable(){ public void run(){// while(count<=10000){ //檔案路徑 String strTest = strPath; //開始讀取資料 String strRead = ReadFile(strTest); //將讀取到的資料變為陣列 String[] strc = ChangeType(strRead); //獲取隨機的姓名 Random random = new Random(); int a = 0; a = random.nextInt(strc.length-1); String str = strc[a]; System.out.println("輸出名字為:"+str); label3.setFont(new java.awt.Font(str,1,60)); //設定名字標籤的文字 label3.setText(str); try{ t.sleep(20);//使執行緒休眠50毫秒 }catch(Exception e){ e.printStackTrace(); } count+=1;//顯示次數 } } }); t.start(); //設定字型顏色 jpanel.add(label3); flag = false; } flag = false; } /** * 點選暫停按鈕後的方法 */ @SuppressWarnings("deprecation") public void stopRun(ActionEvent e){ if(!flag){ t.stop(); flag = true; } flag = true; } } /** *確定按鍵監控類 */ class TextValue implements ActionListener { private StartJFrame startJFrame; TextValue(StartJFrame startJFrame) { this.startJFrame = startJFrame; } public void actionPerformed(ActionEvent e) { startJFrame.chooseValue(e); startJFrame.setVisible(false); } } /** *開始按鍵監控類 */ class Action implements ActionListener { private StartJFrame jFrameIng; Action(StartJFrame jFrameIng) { this.jFrameIng = jFrameIng; } public void actionPerformed(ActionEvent e) { jFrameIng.actionRun(e); } } /** *暫停按鍵監控類 */ class Stop implements ActionListener { private StartJFrame jFrameIng; Stop(StartJFrame jFrameIng) { this.jFrameIng = jFrameIng; } public void actionPerformed(ActionEvent e) { jFrameIng.stopRun(e); } }
未完成程式碼:
package c; import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.io.*; import java.util.*; import java.util.List; import java.util.Timer; import java.util.jar.Attributes.Name; /** * A frame with a button panel */ public class ButtonFrame extends JFrame { private JPanel buttonPanel; private static final int DEFAULT_WIDTH = 500; private static final int DEFAULT_HEIGHT = 400; public ButtonFrame() { try { String line = null; List<String> list = new ArrayList<String>(); BufferedReader in = new BufferedReader(new FileReader("F:\\xll.txt")); while ((line = in.readLine()) != null) { String temp = line.trim(); if (temp != null && !"".equals(temp)) list.add(temp); } String[] arr = (String[]) list.toArray(new String[list.size()]); setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT); buttonPanel = new JPanel(); buttonPanel.setLayout(null); JLabel jLabel = new JLabel(" "); JButton jButton = new JButton("開始"); jLabel.setBounds(130, 60, 200, 60); jButton.setBounds(110, 110, 60, 30); jButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { Timer timer = new Timer(); TimerTask timerTask = new TimerTask() { public void run() { String[] name = arr; jLabel.setText(name[(int) Math.round(Math.random() * 32)]); } }; timer.schedule(timerTask, 10, 10); } }); buttonPanel.add(jLabel); buttonPanel.add(jButton); add(buttonPanel); } catch (FileNotFoundException e1) { // TODO 自動生成的 catch 塊 e1.printStackTrace(); }catch (IOException e1) { // TODO 自動生成的 catch 塊 e1.printStackTrace(); } } }
package c; import java.awt.*; import javax.swing.*; 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); }); } }
實驗總結:
掌握了事件處理的基本原理、 AWT事件模型的工作機制; 掌握了事件處理的基本程式設計模型;瞭解了GUI介面元件觀感設定方法;學習了WindowAdapter類、AbstractAction類的用法 以及GUI程式中滑鼠事件處理技術。
能夠觸發動作事件的動作,主要包括:
(1) 點選按鈕
(2) 雙擊一個列表中的選項;
(3) 選擇選單項;
(4) 在文字框中輸入回車。
通過實驗結對程式設計練習我初步的瞭解到了一些