• 201871010119-帖佼佼《面向对象程序设计(java)》第十三周学习总结


    博客正文开头格式:

    项目

    内容

    这个作业属于哪个课程

     https://www.cnblogs.com/nwnu-daizh/

    这个作业的要求在哪里

       https://www.cnblogs.com/nwnu-daizh/p/11435127.html

    作业学习目标

    (1) 掌握事件处理的基本原理,理解其用途;

    (2) 掌握AWT事件模型的工作机制;

    (3) 掌握事件处理的基本编程模型;

    (4) 了解GUI界面组件观感设置方法;

    (5) 掌握WindowAdapter类、AbstractAction类的用法;

    (6) 掌握GUI程序中鼠标事件处理技术。

    第一部分:第十一章理论知识总结:(35分)

    1、事件处理基础:
      事件源:能够产生事件的组件对象都可以称为事件源,如文本框、按钮等,事件源能够注册监听器并向监听器发送事件对象;
      事件监听器:事件监听器监听事件源发送的通告(事件对象)后,对发生的事件做出响应。一个事件监听器就是一个实现了专门监听器接口类的实例,该类必须实现接口中的方法,这些方法当事件发生时,被自动执行;
      事件对象:Java将事件的相关信息封装在事件对象中,不同事件源可以产生不同类别的事件。所有的事件对象都派生于Java.util.EventObject类;
    2、AWT事件处理机制的概要:
      监听器对象:事件监听器就是一个实现了专门监听器接口类(Listener interface)的实例;
      事件源:能够注册监听器并向监听器发送事件对象;
      当事件发生时,事件源将事件对象自动传递给其所有注册的监听器;
      监听器对象利用事件对象中的信息决定如何对事件做出响应。
    3、事件源与监听器之间的关系:

    4、GUI设计中,需要对组件某种事件进行响应和处理时,程序员必须完成两个步骤:
      (1)定义实现某事件监听器接口的事件监听器类,并具体化接口中声明的事件处理抽象方法;
      (2)为组件注册实现了规定接口的事件监听器对象;
    5、注册监听器方法:
      EventSourceObject.addEventListener(eventListenerObject)
      下面是注册监听器的一个示例:
      ActionListener Listener = 。。。。。;
      Jbutton button = new JButton(“OK”);
      button.addActionListener(Listener);

    6、动作事件:当特定组件动作(点击按钮)发生时,该组件自动生成此动作事件;
      该事件被传递给组件注册的每一个ActionListener对象,并调用监听器对象的actionPerformed方法以接收这类事件对象;
    7、动作事件主要包括:
    (1)点击按钮;
    (2)双击一个列表中的选项;
    (3)选择菜单按钮;
    (4)在文本框中输入回车;
    8、监听器接口的实现
    (1)监听器类必须实现与事件源相应的监听器接口,即必须提供接口中方法的实现;
    (2)监听器接口中方法的实现:
      class Mylistener implements ActionListener
        {
      public void actionPerfromed (ActionEvent event)
        {。。。。。。}
      }
    9、命令按钮JButton主要API:
    (1)创建按钮对象:
    JButton类常用构造方法:
      1)JButton(String text): 创建一个带文本的按钮;
      2)JButton (Icon icon):创建一个带图标的按钮;
      3)JButton(String text,Icon icon ):创建一个带文本和图标的按钮;
    (2)按钮对象的常用方法:
      1)getLable():返回按钮的标签字符串;
      2)setLable(String s):设置按钮的标签为字符串s;
    10、用匿名内部类、Lambda表达式简化程序:
      1)使用字符串构建按钮对象;
      2)把按钮添加到面板上;
      3)用相应的颜色构造一个动作监听器;
      4)注册动作监听器;
    11、改变观感的方法:
    Swing程序默认使用Metal观感,采用两种方式改变观感:
    (1)在Java安装的子目录下jre/lib下的文件swing.properties中,将属性swing.defaultlaf设置为所希望的观感类名 ;
    swing.defaultlaf=com.sun.java.swing.plaf.motif.MotifLookAndFeel
    (2)调用静态的UIManager.setLookAndFeel方法,动态的改变观感,提供所想要的观感类名,再调用静态方法SwingUtilities.updateComponentTreeUI来刷新全部的组件集;
    12、适配器类:
    (1)当程序用户试图关闭一个框架窗口时,JFrame对象就是WindowEvent的事件源;
    (2)捕获窗口事件的监听器:
        WindowListener listener = 。。。。。。。;
        frame.addWindowListener(listener);
    (3)窗口监听器必须是实现WindowListener接口的类的一个对象,WindowListener接口中有七个方法,它们的名字是自解释的。
    (4)WindowListener接口:
      public interface WindowListener
      {
        void windowOpened(WindowEvent e);
        void windowClosing (WindowEvent e);
        void windowClosed(WindowEvent e);
        void windowconified(WindowEvent e);
        void windowDeiconified(WindowEvent e);
        void windowActivated (WindowEvent e);
        void windowDeactivated(WindowEvent e);
      }
    (5)鉴于简化代码的要求,对于不止一个方法的AWT监听器接口都有实现了它的所有方法,但却不做任何工作的适配器类;
    (6)适配器类动态的满足了Java中实现监听器类的技术要求;
    (7)通过扩展适配器类来实现窗口事件需要的动作。
    1、(1)扩展WindowAdapter类:

      (2)扩展WindowAdapter类,继承六个空方法,并覆盖WindowClosing()方法;
    2、(3)注册事件监听器:
      1)可将一个Terminator对象注册为事件监听器:
      WindowListener listener = new Terminator();
      frame.addWindowListener(listener);
      2)只要框架产生一个窗口事件,该事件就会传递给事件监听器对象;
      3)创建扩展于WindowAdapter的监听器类是很好的改进,但还可以进一步将上面的语句简化:
      frame.addWindowListener(new Terminator());
    3、用匿名内部类:
      frame.addWindowListener(new WindowAdapter()
        {
          public void windowClosing (WindowEvent e)
            {
              System.exit(0);
            }
         });
    13、动作事件:
       (1)激活一个命令可以有很多方式,如用户可以通过菜单、击键或工具栏上的按钮选择特定的功能;
       (2)在AWT事件模型中,可设置用不同的方式下达命令(如:点击按钮,菜单选择、按下键盘),其操作动作都是一样的;
    14、动作接口及其类:
      (1)Swing包提供了非常实用的机制来封装动作命令,并将它们连接到多个事件源,这就是Action接口;
      (2)动作对象是一个封装下面内容的对象:
        1)命令的说明:一个文本字符串和一个可选图标;
        2)执行命令所需要的参数。
      (3)Action扩展于ActionListener接口,包含下面方法:
    ——void actionPerfromed(ActionEvent event)
    ——void setEnable(boolean b)
    ——void isEnable()
    ——void getValue(String key, Object value)
    ——void getValue(String key)
    ——void addPropertyChangeListener(PropertyChangeListener listener)
    ——void removePropertyChangeListener(PropertyChangeListener listener)
    说明:    (1)Action是一个接口,而不是一个类,实现这个接口的类必须要实现它的七个方法;
        (2)AbstractAction类实现了Action接口中除了actionPerformed方法之外的所有方法,这个类存储了所有名/值对,并管理着属性变更监听器;
        (3)在动作事件处理应用中,可以直接扩展AbstractAction类,并在扩展类中实现actionPerformed方法。
    15、击键关联映射:
      (1)将一个动作对象添加到击键中,以便让用户敲击键盘命令时来执行这个动作;
      (2)将动作与击键关联起来,需生成KeyStroke类对象;
        KeyStroke ctrBKey = KeyStroke.getKeyStroke("Ctrl B");
    16、鼠标事件:(MouseEvent)
      (1)鼠标事件:(MouseEvent)
      (2)鼠标监听器接口:
        ——MouseListener
        ——MouseMotionListener
      (3)鼠标监听器适配器:
        ——MouseAdapter
        ——MouseMotionAdapter
    17、鼠标点击监听器接口:
        public interface MouseListener extends EventListener{
        public void mouseClicked(MouseEvent e);
        public void mousePressed(MouseEvent e);
        public void mouseReleased(MouseEvent e);
        public void mouseEntered(MouseEvent e);
        public void mouseExited(MouseEvent e);
      }
    18、鼠标移动监听器接口:
        public interface MouseListener extends EventListener{
        public void mouseDragged(MouseEvent e)
        public void mouseMoved(MouseEvent e)
      }
    说明:(1)用户点击鼠标按钮时,会调用三个监听器方法:
      ——鼠标第一次被按下时调用mousePressed方法;
      ——鼠标被释放时调用mouseReleased方法;
      ——两个动作完成之后,再调用mouseClicked方法。
        (2)鼠标在组件上移动时,会调用mouseMoved方法;
        (3)如果鼠标在移动的时候还按下了鼠标,则会调用mouseDragged方法。
        (4)鼠标事件返回值:
      ——鼠标事件的类型是MouseEvent,当发生鼠标事件时:MouseEvent类将会自动创建一个事件对象,以及事件发生位置的x和y坐标,作为事件返回值;
      ——MouseEvent类中的重要方法:
      public int getx();
      public int gety();
      public Point getPoint();
      public int getClickCount();
    19、设置光标:
      java.awt.Component;
      public void setCursor(Cursor c);
      java.awt.Cursor;
      public static Cursor getDefaultCursor();
      public static Cursor getPredefinedCursor(int type);
    20、AWT事件继承层次:
    (1)所有的事件都是由java.util包中的EventObject类扩展而来;
    (2)AWTEvent是所有AWT事件类的父类,也是EventObject的直接子类;
    (3)有些Swing组件生成其他类型的事件对象,一般直接扩展于EventObject,而不是AWTEvent,位于javax.swing.event.*;
    (4)事件对象封装了事件源与监听器彼此通信的事件信息,在必要的时候,可以传递给监听器对象的事件对象进行分析;
    21、AWT中的事件分类:
    (1)AWT将事件分为低级事件和语义事件;
      语义事件:表达用户动作的事件;
      AWT事件中常用的语义事件:
      ActionEvent (对应按钮点击、菜单选择、选择列表项或在文本域键中键入ENTER);
      AdjustmentEvent(用户调节滚动条);
      ItemEvent(用户从复选框或列表项中选择一项);
      低级事件:形成语义事件的事件;
      AWT事件中常用的5个低级事件:
      KeyEvent(一个键被按下或释放);
      MouseEvent(鼠标被按下、释放、移动或者拖动);
      MouseWheelEvent(鼠标滚轮被转动);
      FocusEvent(某个组件获得或失去焦点);
      WindowEvent(窗口状态改变)。

    第二部分:

    2、实验内容和步骤

    实验1: 导入第11章示例程序,测试程序并进行代码注释。

    测试程序1:(5分)

    l  在elipse IDE中调试运行教材443页-444页程序11-1,结合程序运行结果理解程序;

    l  在事件处理相关代码处添加注释;

    实验代码如下:

    package button;
    
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    /**
     * A frame with a button panel.
     */
    public class ButtonFrame extends JFrame   //ButtonFrame类继承JFrame类
    {
       private JPanel buttonPanel;      //设置私有属性
       private static final int DEFAULT_WIDTH = 300;   //设置长和宽为常量值
       private static final int DEFAULT_HEIGHT = 200;
    
       public ButtonFrame()     //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类对象buttonPanel
    
          // add buttons to panel
          buttonPanel.add(yellowButton);      //通过buttonPanel来调用add方法添加按钮到平板上
          buttonPanel.add(blueButton);
          buttonPanel.add(redButton);
    
          // add panel to frame
          add(buttonPanel);    //将平板添加到框架里
    
          // create button actions
          ColorAction yellowAction = new ColorAction(Color.YELLOW);    //创建颜色动作监听器ColorAction
          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   //创建一个实现ActionListener监听器接口的类ColorAction
       {
          private Color backgroundColor;    //创建一个颜色类  背景颜色
    
          public ColorAction(Color c)//内部类,ColorAction构造器
          {
             backgroundColor = c;
          }
    
          public void actionPerformed(ActionEvent event)    //事件对象的入口参数,按钮单击方法
          {
             buttonPanel.setBackground(backgroundColor);//通过调用setBackground方法设置背景
          }
       }
    }
    

      

    package button;
    import java.awt.*;
    import javax.swing.*;
    
    /**
     * @version 1.35 2018-04-10
     * @author Cay Horstmann
     */
    public class ButtonTest
    {
       public static void main(String[] args)
       {
          EventQueue.invokeLater(() -> {    //Lambda表达式
             ButtonFrame frame = new ButtonFrame();   //创建一个ButtonFrame类对象 frame
             frame.setTitle("ButtonTest");//通过对象名调用setTitle方法来设置标题
             frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  //通过frame来调用setDefaultCloseOperation,用来设置关闭窗口的操作 
             frame.setVisible(true);  //调用setVisible方法设置组件是否可见
          });
       }
    }
    

      运行结果如下:

    l  用lambda表达式简化程序;

    实验代码如下:

    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();
          
          makeButton("yellow",Color.YELLOW);
          makeButton("blue",Color.BLUE);
          makeButton("red",Color.RED);
                add(buttonPanel);
    
       }
       
        public void makeButton(String name,Color backgroundColor)
       {
           JButton button=new JButton(name);
           buttonPanel.add(button);
           button.addActionListener(event->
               buttonPanel.setBackground(backgroundColor));
       }
    }
    

      运行结果如下:

    l  掌握JButton组件的基本API;

    l  掌握Java中事件处理的基本编程模型。

    测试程序2:(5分)

    l  在elipse IDE中调试运行教材449页程序11-2,结合程序运行结果理解程序;

    l  在组件观感设置代码处添加注释;

    l  了解GUI程序中观感的设置方法。

    实验代码如下:

    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
    import javax.swing.UIManager;
    
    public class PlafFrame extends JFrame
    {
       private JPanel buttonPanel;    //私有属性的定义
    
       public PlafFrame()//构造器
       {
             buttonPanel = new JPanel();   //创建一个JPanel类对象
             UIManager.LookAndFeelInfo[] infos = UIManager.getInstalledLookAndFeels();  //列举安装的所有观感实现的数组对象,调用此方法
             for (UIManager.LookAndFeelInfo info : infos)    	 //利用给定的类名设置当前的观感
             makeButton(info.getName(), info.getClassName());    //调用getName和getClassName方法得到每一种观感的名字和类名
             add(buttonPanel);
             pack();
       }
       private void makeButton(String name, String className)    //辅助方法makeButton指定按钮动作
       {
          //添加按钮到面板
          JButton button = new JButton(name);
          buttonPanel.add(button);
          //设置按钮要进行的操作
          button.addActionListener(event -> {
             // 按钮操作结果: 切换到新的外观
             try //可能出错的代码放入try子句中
             {
                UIManager.setLookAndFeel(className);
                SwingUtilities.updateComponentTreeUI(this);   //this指示外围对象
                pack();  //pack方法调整此窗口的大小,以适合其子组件的首选大小和布局
             }
             catch (Exception e)
             {
                e.printStackTrace();
             }
          });
       }
    }
    

     

    import java.awt.*;
    import javax.swing.*;
    
    public class PlafTest
    {
       public static void main(String[] args)
       {
          EventQueue.invokeLater(() -> {
             JFrame frame = new JFrame();   //创建一个JFrame类对象frame
             frame.setTitle("PlafTest");     //调用setTitle方法设置标题
             frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   //调用setDefaultCloseOperation方法设置窗口是否可见
             frame.setVisible(true);  //调用setVisible方法设置组件可见
          });
       }
    }

      运行结果如下:

     

    测试程序3:(5分)

    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   			//ActionFrame类继承JFrame类
    {
       private JPanel buttonPanel;       				//设置私有属性
       private static final int DEFAULT_WIDTH = 300;	//常量长和宽的定义
       private static final int DEFAULT_HEIGHT = 200;
       
       public ActionFrame()    							//ActionFrame构造器
       {
          setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);   	//设置长和宽分别为 DEFAULT_WIDTH, DEFAULT_HEIGHT大小的框架
          buttonPanel = new JPanel();  					//创建一个JPanel类对象
          // define actions
          ColorAction yellowAction = new ColorAction("Yellow", new ImageIcon("yellow-ball.gif"),
                Color.YELLOW);    						//创建了三个ColorAction类的对象
          ColorAction blueAction = new ColorAction("Blue", new ImageIcon("blue-ball.gif"), Color.BLUE);
          ColorAction 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 inputMap = buttonPanel.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);   
          //调用getInputMap方法从组件中得到输入映射
          //输入映射,对应当这个组件包含了拥有键盘焦点的组件时这个条件
          //inputMap不能直接的将KeyStroke对象映射到ColorAction对象
          inputMap.put(KeyStroke.getKeyStroke("ctrl Y"), "panel.yellow");  //将KeyStroke对象映射到任意对象
          inputMap.put(KeyStroke.getKeyStroke("ctrl B"), "panel.blue");
          inputMap.put(KeyStroke.getKeyStroke("ctrl R"), "panel.red");
        //当在运行时按下Ctrl+Y,Ctrl+B,Ctrl+R会改变面板的背景颜色
          // associate the names with actions
          ActionMap actionMap = buttonPanel.getActionMap();   //由ActionMap类实现将对象映射到动作上的第二个映射
          actionMap.put("panel.yellow", yellowAction);
          actionMap.put("panel.blue", blueAction);
          actionMap.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);        //将颜色存储在AbstractAction类提供的名/值对表中
             putValue(Action.SMALL_ICON, icon);  //putValue方法允许存储动作中的任意名/值
             putValue(Action.SHORT_DESCRIPTION, "Set panel color to " + name.toLowerCase());
             putValue("color", c);
          }
    
          public void actionPerformed(ActionEvent event)   //actionPerformed方法执行改变颜色的动作
          {
             Color color = (Color) getValue("color");     //检索动作对象中的任意名/值对,强转为Color类
             buttonPanel.setBackground(color);  //调用setBackground方法设置背景颜色
          }
       }
    }
    

      

    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(() -> {   //
             ActionFrame frame = new ActionFrame();   //创建一个ActionFrame类对象
             frame.setTitle("ActionTest");   //调用setTitle方法设置标题
             frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//调用setDefaultCloseOperation方法来设置关闭窗口
             frame.setVisible(true);//调用setVisible方法设置组件是否可见
          });
       }
    }
    

      运行结果如下:

    总结:

    用同一个动作按钮响应按钮、菜单项或按键的方式:

    (1)实现一个扩展于AbstractAction类的类,多个相关的动作可以使用同一个类;

    (2)构造一个多做类的对象;

    (3)使用动作对象创建按钮或菜单项,构造器将从动作对象中读取标签文本和图标;

    (4)为了能够通过按键触发动作,必须额外地执行几步操作,首先定位顶层窗口组件,例如:包含所有其他组件地面板;

    (5)然后,得到顶层组件WHEN_ANCESTOR_OF_FOCUSED_COMPONENT输入映射。为需要的按键创建一个KeyStrike对象。创建一个描述动作字符串这样的动作键对象。将(按键,动作键)对添加到输入映射中;

    (6)最后,得到顶层组件地动作映射。将(动作键,动作对象)添加到映射中;

    上面程序在运行的过程中,按下Ctrl+Y、Ctrl+B、Ctrl+R会改变面板的背景颜色。

    测试程序4:(5分)

    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   //MouseComponent类继承JComponent类
    {
       private static final int DEFAULT_WIDTH = 300;     //私有整型常量DEFAULT_WIDTH和DEFAULT_HEIGHT的定义
       private static final int DEFAULT_HEIGHT = 200;
    
       private static final int SIDELENGTH = 10;
       private ArrayList<Rectangle2D> squares;      //定义Rectangle2D类型的泛型数组ArrayList类对象squares
       private Rectangle2D current; 		// the square containing the mouse cursor
       //包含鼠标光标的方块
       public MouseComponent()       		//MouseComponent类的构造器
       {
          squares = new ArrayList<>();      //创建一个泛型数组类对象squares
          current = null;      				//current 函数返回 数组中的当前元素,设置为null
         //MouseListener和addMouseMotionListener是两个独立的接口
          addMouseListener(new MouseHandler());   //MouseHandler和MouseMotionHandler演示了鼠标的点击监听、鼠标移动和拖动事件监听:
          addMouseMotionListener(new MouseMotionHandler());//鼠标移动与拖动事件定义在一个MouseMotionListener接口中。 
       }
    
       public Dimension getPreferredSize() //getPreferredSize方法定义,要覆盖这个方法,(返回)获取组件的首选大小。
       //Dimension类用来封装单个对象中组件的宽度和高度,与组件的某个属性关联.用Dimension类封装起来以方便管理和使用这两个属性。
       {
          return new Dimension(DEFAULT_WIDTH, DEFAULT_HEIGHT);    //返回一个宽和长分别为DEFAULT_WIDTH, DEFAULT_HEIGHT的组件
       }   
       
       public void paintComponent(Graphics g)      //覆盖这个方法paintComponent方法,没有实例化,只是一个入口参数为Graphics类型的 g。来描述应该如何绘制自己的组件
       //paintComponent就是本身这个容器自己画出自己组件的方法,
       {
          Graphics2D g2 = (Graphics2D) g;
          //Graphics2D是Graphics的子类,父类型可以转换为子类型,因为子类包含了父类的所以属性,子类本身多出来的属性放空就可以
          // draw all squares
          for (Rectangle2D r : squares)     
             g2.draw(r);   //调用draw方法画出squares正方形
       }
    
       /**
        * Finds the first square containing a point.
        * @param p a point
        * @return the first square that contains p
        */
       public Rectangle2D find(Point2D p)    //find方法,入口参数为一个Point2D类型的p
       {
          for (Rectangle2D r : squares)      //创建一个Rectangle2D类对象r
          {
             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)    //add方法来添加小方块
       {
          double x = p.getX();     //返回正方形左上角的x和y坐标
          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)    	//remove方法删除小方块,入口参数为Rectangle2D类型的s,
       {
          if (s == null) return;    			//当s为空时,不删除
          if (s == current) current = null;     //当当前光标所指位置和s相等时,将当前的光标所指的位置置为null
          squares.remove(s);    				//调用remove方法将s删除
          repaint();
       }
    
       private class MouseHandler extends MouseAdapter      //MouseHandler内部类继承MouseAdapter类
       {
          public void mousePressed(MouseEvent event)    //mousePressed方法的定义,当鼠标点击在所有小方块的像素之外,就会绘制出一个新的小方块,
          {
             // add a new square if the cursor isn't inside a square
             current = find(event.getPoint());     //调用getPoint方法获取这个图形环境绘制属性
             if (current == null) add(event.getPoint()); 	//如果当前光标所指地正方形为空,添加正方形
          }
    
          public void mouseClicked(MouseEvent event)      //mouseClicked方法,当鼠标双击某个小方块时,就会将它擦除,
          {
             // remove the current square if double clicked
             current = find(event.getPoint());
             if (current != null && event.getClickCount() >= 2) remove(current);
             //当不为空,并且调用getClickCount方法,如果单击超过两次,就删除这个正方形
          }
       }
    
       private class MouseMotionHandler implements MouseMotionListener   //MouseMotionHandler类实现MouseMotionListener接口的内部类
       {
          public void mouseMoved(MouseEvent event)   //MouseMotionListener类中的mouseMoved方法
          {
             // 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)     //mouseDragged方法,入口参数是MouseEvent类型,event
          {
             if (current != null)    //如果当前光标所指向的方块不为空。
             {
                int x = event.getX();        //调用getX和getY方法可以获得鼠标被按下时所在的x和y坐标
                int y = event.getY();
    
                // drag the current rectangle to center it at (x, y)
                 current.setFrame(x - SIDELENGTH / 2, y - SIDELENGTH / 2, SIDELENGTH, SIDELENGTH);
                //调用setFrame方法得到
                repaint();     //尽可能快的重新绘制组件
             }
          }
       }   
    }
    

      

    package mouse;
    import javax.swing.*;
    /**
     * A frame containing a panel for testing mouse operations
     */
    public class MouseFrame extends JFrame     //MouseFrame类继承JFrame类
    {
       public MouseFrame()    //MouseFrame内部类
       {
          add(new MouseComponent());    //添加新创建的鼠标组件
          pack();     //pack方法调整此窗口的大小,以适合其子组件的首选大小和布局。
       }
    }
    

      

    package mouse;
    
    import java.awt.*;
    import javax.swing.*;
    
    /**
     * @version 1.35 2018-04-10
     * @author Cay Horstmann
     */
    public class MouseTest
    {
       public static void main(String[] args)
       {
          EventQueue.invokeLater(() -> {     //Lambda表达式
             MouseFrame frame = new MouseFrame();     //创建一个MouseFrame类的对象frame
             frame.setTitle("MouseTest");  //通过调用setTitle方法来设置标题
             frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //通过调用setDefaultCloseOperation方法来设置窗口是否可见
             frame.setVisible(true);    //调用setVisible方法设置组件是否可见
          });
       }
    }
    

      运行结果如下:

     

    总结:

    这是一个简单的图形编辑器地应用程序,它允许用户在画布上放置、移动和添加方块;

    (1)当用户点击鼠标按钮时,将会调用三个监听器方法,鼠标第一次被按下时调用mousePressed;鼠标被释放时调用mouseReleased,最后调用mouseClicked。

    (2)如果只对最终地点击事件感兴趣,就可以忽略前两个方法,用mouseEvent类对象作为参数,调用getX和getY方法得到鼠标被按下时鼠标指针所指的x和y坐标。

    (3)使用getClickCount方法得到单击、双击还是三击。

    实验2:结对编程练习包含以下4部分:(20分)

    1)   程序设计思路简述;

    (1)首先创建一个控件,上面是一个随机点名器的标签和一个开始按钮,字体设置为宋体,并且在标签的中间,取消按钮文字周围的边框;

    (2)初始化图形用户界面,设置背景的颜色,设置图形用户界面的标题等等,设置pane面板布局为空,自己调整组件位置;

    (3)添加控件,添加按钮的点击事件,获取学生姓名,然后是一个计时器,每个50毫秒就是一个同学的名字,在运行的过程中,会自动生成0-30之间的随机数;

    (4)定义一个数组用来保存学生姓名,读取文件,里面包含了读文件时会遇到的异常,用try  catch语句将其捕获,

    2)   符合编程规范的程序代码;

    3)   程序运行功能界面截图;

    4)   结对过程描述,提供两人在讨论、细化和编程时的结对照片(非摆拍)。

    利用班级名单文件、文本框和按钮组件,设计一个有如下界面(图1)的点名器,要求用户点击开始按钮后在文本输入框随机显示2018级计算机科学与技术(1)班同学姓名,如图2所示,点击停止按钮后,文本输入框不再变换同学姓名,此同学则是被点到的同学姓名,如图3所示。

       

                                          图3 点名器点名界面

    手动点名,没有计时器实验代码如下:

    import java.awt.BorderLayout;
    import java.awt.EventQueue;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.border.EmptyBorder;
    import javax.swing.JButton;
    import javax.swing.JTextField;
    import javax.swing.JLabel;
    import java.awt.Color;
    import java.awt.Font;
    import javax.swing.SwingConstants;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyAdapter;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.Random;
    public class Dianming extends JFrame {
    	private JPanel contentPane;
    	private static JTextField textField;
    	private JLabel label;
    	private ArrayList<String> names = new ArrayList<String>();
    	private String File = "D:/2019studentlist.txt";
    	private File f;
    	private JButton btnNewButton;
    	private FileReader fr;
    	private BufferedReader br;
    	private String strName;
    
    /**
    * Launch the application.
    */
    public static void main(String[] args) {
    
    	EventQueue.invokeLater(new Runnable() {
    		public void run() {
    			try {
    				Dianming frame = new Dianming();
    				frame.setTitle("点名器");
    				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    				frame.setVisible(true);
    			} catch (Exception e) {
    				e.printStackTrace();
    			}
    		}
    	});
    }
    /**
    * Create the frame.
    */
    public Dianming() {
    	setBounds(100, 100, 427, 328);
    	contentPane = new JPanel();
    	contentPane.setBackground(Color.pink);
    	contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    	setContentPane(contentPane);
    	contentPane.setLayout(null);
    
    	btnNewButton = new JButton("开始");
    	btnNewButton.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
    	write();
    	textField.setText(strName);
    	}
    });
    	btnNewButton.setForeground(Color.green);
    	btnNewButton.setBackground(Color.magenta);
    	btnNewButton.setBounds(142, 188, 113, 52);
    	contentPane.add(btnNewButton);
    	textField = new JTextField();
    	textField.setHorizontalAlignment(SwingConstants.CENTER);
    	textField.setForeground(Color.green);
    	textField.setBounds(131, 13, 135, 101);
    	contentPane.add(textField);
    	textField.setColumns(10);
    	label = new JLabel("中奖了!!!");
    	label.setBackground(Color.pink);
    	label.setBounds(159, 127, 88, 36);
    	contentPane.add(label);
    }
    
    public void read() {
    	try {
    		f = new File(File);
    		if (!f.exists()) {
    			f.createNewFile();
    		}
    		fr = new FileReader(f);
    		br = new BufferedReader(fr);
    		String str = br.readLine();
    		while (str != null) {
    			names.add(str);
    			str = br.readLine();
    		}
    	} catch (IOException e) {
    		e.printStackTrace();
    	}
    
    }
    
    public void write() {
    	read();
    	int index = (int) (Math.random() * names.size());
    	strName = names.get(index);	
    }
    public void text(){
    	}
    }
    

      运行结果如下:

    有随机点名器的实验代码如下:

    import java.awt.Color;
    import java.awt.Container;
    import java.awt.Dimension;
    import java.awt.FlowLayout;
    import java.awt.Font;
    import java.awt.GridBagLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.ArrayList;
    import java.util.Timer;
    import java.util.TimerTask;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.SwingConstants;
    import java.io.BufferedReader;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.awt.Container;
    import java.awt.Dimension;
    import java.awt.Panel;
    import java.awt.Toolkit;
    public class Main{
    	public static void main(String[] args) {
    		GUI gui = new GUI();
    	}
    }
    
    @SuppressWarnings("serial")
     class GUI extends JFrame {
    	//创建控件
    	private JLabel jLabel;
    	private JButton jButton;
    	private ArrayList<String> namelist;
    	public GUI() {
    		jLabel = new JLabel("随机点名器",SwingConstants.CENTER);
    		jButton = new JButton("开始");
    		Font font = new Font("宋体", SwingConstants.CENTER, 16);
    		jLabel.setFont(font);
    		jButton.setFont(font);
    		//取消按钮文字周围的边框
    		jButton.setFocusPainted(false);
    		jLabel.setOpaque(true);
    		jLabel.setBackground(Color.pink);
    		jButton.setBackground(Color.magenta);
    		// 初始化图形界面
    		Container pane = this.getContentPane();
    		this.setSize(600,400  );
    		Center1.setCenter(this);;
    		this.setTitle("点名器");
    		//设置pane面板布局为空,自己调整组件位置
    		pane.setLayout(null);
    		//添加控件
    		this.getContentPane().setBackground(Color.pink);
    		this.add(jLabel);
    		jLabel.setBounds(260, 100, 80, 36);
    		this.add(jButton);
    		jButton.setBounds(260, 180, 80, 36);
    		this.setVisible(true);
    		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    		//添加按钮的点击事件
    		jButton.addActionListener(new myActionListener());
    		//获取学生姓名
    		ReadFile1 rf = new ReadFile1();
    		namelist = rf.getList();
    
    	}
    	public class myActionListener implements ActionListener{
    
    		@Override
    		public void actionPerformed(ActionEvent e) {
    			// TODO 自动生成的方法存根
    			//生成0-30之间的随机数
    			if (!jButton.getText().equals("停止")) {
    				jButton.setText("停止");
    				jButton.setBackground(Color.green);
    			}
    			else {
    				jButton.setText("开始");
    				jButton.setBackground(Color.magenta);
    			}
    			Timer timer = new Timer();
    			TimerTask task = new TimerTask() {
    				
    				@Override
    				public void run() {
    					// TODO 自动生成的方法存根
    					int temp=(int)(Math.random()*30);
    					System.out.println(temp);
    					jLabel.setText(namelist.get(temp));
    					jLabel.setBackground(Color.blue);
    					if (jButton.getText().equals("开始")) {
    						timer.cancel();
    						jLabel.setBackground(Color.pink);
    					}
    				}
    			};
    			//做定时任务,每隔50毫秒刷新一次名字
    			timer.schedule(task, 10, 50);
    		}
    		
    	}
    }
    class ReadFile1 {
    	private ArrayList<String> list;
    
    	//定义一个数组用来保存学生姓名
    	@SuppressWarnings("resource")
    	public ReadFile1() {
    		list = new ArrayList<>();
    		try {
    			BufferedReader br = new BufferedReader(new FileReader("2019studentlist.txt"));
    			String temp;
    			while ((temp=br.readLine()) != null) {
    				list.add(temp);
    			}
    		} catch (FileNotFoundException e) {
    			// TODO 自动生成的 catch 块
    			System.out.println("文件studentlist.txt不存在");
    		} catch (IOException e) {
    			// TODO 自动生成的 catch 块
    			System.out.println("文件studentlist.txt读取异常");
    		}
    	}
    	public ArrayList<String> getList() {
    		return list;
    	}
    }
    class Center1 {
    	public static void setCenter(Container pane) {
    		Dimension size = Toolkit.getDefaultToolkit().getScreenSize();
    		int width = size.width;
    		int height = size.height;
    		int Pwidth = pane.getWidth();
    		int Pheight = pane.getHeight();
    		int Cwidth=(width-Pwidth)/2;
    		int Cheight=(height-Pheight)/2;
    		pane.setLocation(Cwidth, Cheight);
    		
    	}
    }
    

      运行结果如下:

    实验总结:(20分)

                这周学习了事件处理,理论知识知道但是不会运用,自己写不出来正确的代码。验证实验中需要些注释的地方,有好些不知道它具体是什么作用,通过查书以及网上搜索才知道一些代码的具体作用,这周的代码理解对我来说比较费劲,可能是上课的时候没听明白。至于结对编程,上实验课的时候有一部分学长大概讲了,在课下自己和搭档写的时候还是不太顺利。

  • 相关阅读:
    SSH整合
    JQuery
    MVC框架与增强
    通用分页(二)
    通用分页(一)
    自定义标签
    反射(一)
    Xml解析作业与Xml建模andXml建模作业
    Xml与Map之间的相互转换
    Xml解析
  • 原文地址:https://www.cnblogs.com/-8tjj/p/11908058.html
Copyright © 2020-2023  润新知