實驗十三 圖形界面事件處理技術java
實驗時間 2018-11-22編程
1、實驗目的與要求小程序
(1) 掌握事件處理的基本原理,理解其用途;安全
(2) 掌握AWT事件模型的工做機制;網絡
(3) 掌握事件處理的基本編程模型;框架
(4) 瞭解GUI界面組件觀感設置方法;dom
(5) 掌握WindowAdapter類、AbstractAction類的用法;ide
(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-四、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:處理事件的接口:監聽器負責處理事件源發生的事件。爲了處理事件源發生的事件,監聽器會自動調用一個方法來處理事件,該方法定義在相應事件的接口中。