项目

内容

这个作业属于哪个课程

<任课教师博客主页链接>https://www.cnblogs.com/nwnu-daizh/

这个作业的要求在哪里

<作业链接地址>https://www.cnblogs.com/nwnu-daizh/p/11888568.html

作业学习目标

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

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

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

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

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

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

第一部分:理论知识学习部分

 第11章 事件处理

11.1 事件处理基础

  • 事件源(event source):能够产生事件的对象都可 以成为事件源,如文本框、按钮等。一个事件源是一个 能够注册监听器并向监听器发送事件对象的对象。
  • 事件监听器(event listener):事件监听器对象接 收事件源发送的通告(事件对象),并对发生的事件作 出响应。一个监听器对象就是一个实现了专门监听器接 口的类实例,该类必须实现接口中的方法,这些方法当 事件发生时,被自动执行。
  • 事件对象(event object):Java将事件的相关信息 封装在一个事件对象中,所有的事件对象都最终派生于 java.util.EventObject类。不同的事件源可以产生不 同类别的事件。
  • 1、AWT事件处理机制的概要:

    1>监听器对象是一个实现了特定监听器接口的类的实例;

    2>事件源是一个能够注册监听器对象并发送事件对象的对象;

    3>当事件发生时,事件源将事件对象传递给所有注册的监听器;

    4>监听器对象将利用事件对象中的信息决定如何对事件做出响应

  • GUI设计中,程序员需要对组件的某种事件进行响应和处理时,必须完成两个步骤:

    1> 定义实现某事件监听器接口的事件监听器类,具体化接口中声明的事件处理抽象方法。

   2>为组件注册实现了规定接口的事件监听器对象;

  • 注册监听器方法 eventSourceObject.addEventListener(eventListenerObject)。
  • 下面是监听器的一个示例: ActionListener listener = …;

    JButton button=new JButton(“Ok”); button.addActionListener(listener);

  • 动作事件(ActionEvent):当特定组件动作(点 击按钮)发生时,该组件生成此动作事件。

    该 事 件 被 传 递 给 组 件 注 册 的 每 一 个 ActionListener 对象, 并 调 用 监 听 器 对 象 的 actionPerformed方法以接收这类事件对象。

    能够触发动作事件的动作,主要包括:

    (1) 点击按钮

    (2) 双击一个列表中的选项;

    (3) 选择菜单项;

    (4) 在文本框中输入回车。

11.2 动作

  • 动作事件

激活一个命令可以有多种方式,如用户可以通过菜单、击键或工具栏上的按钮选择特定的功能。

在AWT事件模型中,可设置用不同方式下达命令: (如:点击按钮、菜单选项、按下键盘),其操作动作都是一-样的。

  • 动作接口及其类

Swing包提供了非常实用的机制来封装动作命令,并将它们连接到多个事件源,这就是Action接口。

动作对象是一个封装下列内容的对象:1.命令的说明: 一个文本字符串和一个可选图标;2.执行命令所需要的参数。

Action扩展于ActionListener接口,包含下列方法:

void act ionPerformed (Act ionEvent event)void setEnabled(bool ean b);
       void isEnabled();
       void putValue (String key, 0bject va lue);
       void getValue (String key);
       void addPropertyChangeListner (ProperChangeL istener listener);
       void removePropertyChangeListener (Pr operChangeListener listener);

Action是一个接口,而不是一个类,实现这个接口的类必须要实现它的7个方法。

AbstractAction类实现了Action接口中除actionPerformed方法之外的所有方法,这个类存储了所有名/值对,并管理着属性变更监听器。

在动作事件处理应用中,可以直接扩展AbstractAction类,并在扩展类中实现act ionPerformed方法。

  • 击键关联映射

将一个动作对象添加到击键中,以便让用户敲击键盘命令来执行这个动作。

将动作与击键关联起来,需生成KeyStroke类对象。
           KeyStroke ctrBKey = KeyStroke.getKeyStroke("Ctrl B");
 下面是将击键与动作对象关联起来的方式(桥梁:动作键对象描述字符串)

InputMap imap = buttonPanel.getInputMap(JComponent.WHEN_ ANCESTOR_ OF_FOCUSED_ COMPONENT);

imap.put(KeyStroke. getKeyStroke("ctrl Y"), "panel.yellow");

ActionMap amap = buttonPanel.getActionMap();

amap.put("panel,.yellow", yellowAction);

11.3 鼠标事件

a)鼠标事件 – MouseEvent

b)鼠标监听器接口 – MouseListener – MouseMotionListener

c) 鼠标监听器适配器 – MouseAdapter – MouseMotionAdapter

11.4 AWT事件继承层次

a) 所有的事件都是由java.util包中的EventObject 类扩展而来。

b) AWTEevent 是所有AWT 事件类的父类, 也是 EventObject的直接子类。

c) 有些Swing组件生成其他类型的事件对象,一般直 接 扩 展 于 EventObject, 而不是AWTEvent,位于 javax.swing.event.*。

d) 事件对象封装了事件源与监听器彼此通信的事件 信息。在必要的时候,可以对传递给监听器对象的 事件对象进行分析。

第二部分:实验部分

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

测试程序1:

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

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

l 用lambda表达式简化程序;

l 掌握JButton组件的基本API;

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

代码如下:

ButtonFrame.java:

  1. package button;
  2.  
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. import javax.swing.*;
  6.  
  7. /**
  8. * 一个带按钮面板的框架
  9. */
  10. public class ButtonFrame extends JFrame
  11. {
  12. private JPanel buttonPanel;//定义JPanel属性
  13. private static final int DEFAULT_WIDTH = 300;
  14. private static final int DEFAULT_HEIGHT = 200;
  15.  
  16. public ButtonFrame()
  17. {
  18. setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);//setSize更改宽度和高度的属性值
  19.  
  20. // 创建按钮
  21. var yellowButton = new JButton("Yellow");
  22. var blueButton = new JButton("Blue");
  23. var redButton = new JButton("Red");
  24.  
  25. buttonPanel = new JPanel();//使new JPanel指向对象buttonPanel
  26.  
  27. //在面板中添加按钮,使用add方法
  28. buttonPanel.add(yellowButton);
  29. buttonPanel.add(blueButton);
  30. buttonPanel.add(redButton);
  31.  
  32. // 添加面板到框架
  33. add(buttonPanel);
  34.  
  35. //创建按钮动作
  36. var yellowAction = new ColorAction(Color.YELLOW);
  37. var blueAction = new ColorAction(Color.BLUE);
  38. var redAction = new ColorAction(Color.RED);
  39.  
  40. //用按钮关联动作,监听器对象与组件之间的注册机制
  41. yellowButton.addActionListener(yellowAction);
  42. blueButton.addActionListener(blueAction);
  43. redButton.addActionListener(redAction);
  44. }
  45.  
  46. /**
  47. * 设置面板背景颜色的动作侦听器
  48. */
  49. private class ColorAction implements ActionListener
  50. {
  51. private Color backgroundColor;
  52.  
  53. public ColorAction(Color c)
  54. {
  55. backgroundColor = c;
  56. }
  57.  
  58. public void actionPerformed(ActionEvent event)
  59. {
  60. buttonPanel.setBackground(backgroundColor);
  61. }
  62. }
  63. }

ButtonTest.java:

  1. package button;
  2.  
  3. import java.awt.*;
  4. import javax.swing.*;
  5.  
  6. /**
  7. * @version 1.35 2018-04-10
  8. * @author Cay Horstmann
  9. */
  10. public class ButtonTest
  11. {
  12. public static void main(String[] args)
  13. {
  14. EventQueue.invokeLater(() -> {
  15. var frame = new ButtonFrame();//生成ButtonFrame对象
  16. frame.setTitle("ButtonTest");//设置组建的自定义标题测试按钮
  17. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置默认的关闭操作,参数在关闭动作时退出
  18. frame.setVisible(true);//图形界面设置为可见
  19. });
  20. }
  21. }

实验结果如图:

lambda表达式简化程序:

ButtonFrame1.java:

  1. package button;
  2.  
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. import javax.swing.*;
  6.  
  7. /**
  8. * 一个带按钮面板的框架
  9. */
  10. public class ButtonFrame1 extends JFrame {
  11. private JPanel buttonPanel;//定义JPanel属性
  12. private static final int DEFAULT_WIDTH = 400;
  13. private static final int DEFAULT_HEIGHT = 300;
  14.  
  15. public ButtonFrame1() {
  16. setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);//setSize更改宽度和高度的属性值
  17. buttonPanel = new JPanel();//使new JPanel指向对象buttonPanel
  18. makeButton("gray", Color.gray);
  19. makeButton("pink", Color.pink);
  20. add(buttonPanel);
  21.  
  22. }
  23.  
  24. protected void makeButton(String name,Color backgound) {
  25. //创建按钮
  26. JButton button = new JButton(name);
  27. //在面板中添加按钮,使用add方法
  28. buttonPanel.add(button);
  29. //Lambda表达式
  30. button.addActionListener((e)->{
  31. buttonPanel.setBackground(backgound);
  32. });
  33.  
  34. }
  35.  
  36. /**
  37. * 设置面板背景颜色的动作侦听器
  38. */
  39. /*private class ColorAction implements ActionListener {
  40. private Color backgroundColor;
  41.  
  42. public ColorAction(Color c) {
  43. backgroundColor = c;
  44. }
  45.  
  46. public void actionPerformed(ActionEvent event) {
  47. buttonPanel.setBackground(backgroundColor);
  48. }
  49. }*/
  50. }

运行结果如图:

测试程序2:

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

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

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

实验代码如下:

PlafFrame.java:

  1. package plaf;
  2.  
  3. import javax.swing.JButton;
  4. import javax.swing.JFrame;
  5. import javax.swing.JPanel;
  6. import javax.swing.SwingUtilities;
  7. import javax.swing.UIManager;
  8.  
  9. /**
  10. *带有按钮面板的框架,用于改变外观和感觉
  11. */
  12. public class PlafFrame extends JFrame
  13. {
  14. private JPanel buttonPanel;
  15.  
  16. public PlafFrame()
  17. {
  18. buttonPanel = new JPanel();
  19. //获取所有的显示样式UIManager.setLookAndFeel(infos[0].getClassName())
  20. UIManager.LookAndFeelInfo[] infos = UIManager.getInstalledLookAndFeels();
  21. for (UIManager.LookAndFeelInfo info : infos)
  22. makeButton(info.getName(), info.getClassName());
  23.  
  24. add(buttonPanel);//增加按键点击事件
  25. pack();
  26. }
  27.  
  28. /**
  29. * 制作一个按钮来改变可插入的外观和感觉。
  30. * @param name the button name
  31. * @param className the name of the look-and-feel class
  32. */
  33. private void makeButton(String name, String className)
  34. {
  35. // 添加按钮至Panel
  36. JButton button = new JButton(name);
  37. buttonPanel.add(button);
  38.  
  39. //设置按钮操作
  40. button.addActionListener(event -> {
  41. // 按钮操作:切换到新的外观和感觉
  42. try
  43. {
  44. //设置成你所使用的平台的外观。 java的图形界面外观有3种,默认是java的金属外观,还有就是windows系统,motif系统外观.
  45. UIManager.setLookAndFeel(className);
  46. SwingUtilities.updateComponentTreeUI(this);
  47. pack();//依据放置的组件设定窗口的大小, 使之正好能容纳放置的所有组件
  48. }
  49. catch (Exception e)//抛出异常
  50. {
  51. e.printStackTrace();//深层次的输出异常调用的流程
  52. }
  53. });
  54. }
  55. }

PlafTest.java:

  1. package plaf;
  2.  
  3. import java.awt.*;
  4. import javax.swing.*;
  5.  
  6. /**
  7. * @version 1.32 2015-06-12
  8. * @author Cay Horstmann
  9. */
  10. public class PlafTest
  11. {
  12. public static void main(String[] args)
  13. {
  14. EventQueue.invokeLater(() -> {
  15. JFrame frame = new PlafFrame();//生成PlafFrame对象
  16. frame.setTitle("PlafTest");//设置组建的自定义标题测试按钮
  17. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置默认的关闭操作
  18. frame.setVisible(true);
  19. });
  20. }
  21. }

运行结果如图:

测试程序3:

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

l 掌握AbstractAction类及其动作对象;

l 掌握GUI程序中按钮、键盘动作映射到动作对象的方法。

实验代码如下:

ActionFrame.java:

  1. package action;
  2.  
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. import javax.swing.*;
  6.  
  7. /**
  8. * 具有显示颜色变化动作的面板的框架
  9. */
  10. public class ActionFrame extends JFrame
  11. {
  12. private JPanel buttonPanel;
  13. private static final int DEFAULT_WIDTH = 300;
  14. private static final int DEFAULT_HEIGHT = 200;
  15.  
  16. public ActionFrame()
  17. {
  18. setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);//设置宽度和高度
  19.  
  20. buttonPanel = new JPanel();
  21.  
  22. var yellowAction = new ColorAction("Yellow", new ImageIcon("yellow-ball.gif"),
  23. Color.YELLOW);
  24. var blueAction = new ColorAction("Blue", new ImageIcon("blue-ball.gif"), Color.BLUE);
  25. var redAction = new ColorAction("Red", new ImageIcon("red-ball.gif"), Color.RED);
  26.  
  27. //创建一个按钮,其属性从所提供的 Action中获取
  28. buttonPanel.add(new JButton(yellowAction));
  29. buttonPanel.add(new JButton(blueAction));
  30. buttonPanel.add(new JButton(redAction));
  31.  
  32. //将添加好按钮的面板添加到原框架中
  33. add(buttonPanel);
  34.  
  35. // associate the Y, B, and R keys with names
  36. InputMap inputMap = buttonPanel.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
  37. inputMap.put(KeyStroke.getKeyStroke("ctrl Y"), "panel.yellow");
  38. inputMap.put(KeyStroke.getKeyStroke("ctrl B"), "panel.blue");
  39. inputMap.put(KeyStroke.getKeyStroke("ctrl R"), "panel.red");
  40.  
  41. // associate the names with actions
  42. ActionMap actionMap = buttonPanel.getActionMap();
  43. actionMap.put("panel.yellow", yellowAction);
  44. actionMap.put("panel.blue", blueAction);
  45. actionMap.put("panel.red", redAction);
  46. }
  47.  
  48. public class ColorAction extends AbstractAction
  49. {
  50. /**
  51. *构造颜色动作
  52. * @param name the name to show on the button
  53. * @param icon the icon to display on the button
  54. * @param c the background color
  55. */
  56. public ColorAction(String name, Icon icon, Color c)
  57. {
  58. putValue(Action.NAME, name);
  59. putValue(Action.SMALL_ICON, icon);
  60. putValue(Action.SHORT_DESCRIPTION, "Set panel color to " + name.toLowerCase());
  61. putValue("color", c);
  62. //在构造器中设置一些键值对映射,这些设置的属性将会被JPanel读取
  63. }
  64.  
  65. public void actionPerformed(ActionEvent event)
  66. {
  67. var color = (Color) getValue("color");
  68. buttonPanel.setBackground(color);//设置背景颜色
  69. }
  70. }
  71. }

ActionTest.java:

  1. package action;
  2.  
  3. import java.awt.*;
  4. import javax.swing.*;
  5.  
  6. /**
  7. * @version 1.34 2015-06-12
  8. * @author Cay Horstmann
  9. */
  10. public class ActionTest
  11. {
  12. public static void main(String[] args)
  13. {
  14. EventQueue.invokeLater(() -> {
  15. var frame = new ActionFrame();
  16. frame.setTitle("ActionTest");//设置标题
  17. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  18. frame.setVisible(true);
  19. });
  20. }
  21. }

运行结果如图:

测试程序4:

l 在elipse IDE中调试运行教材462页程序11-4、11-5,结合程序运行结果理解程序;

l 掌握GUI程序中鼠标事件处理技术。

实验代码如下:

MouseComponent.java:

  1. package mouse;
  2.  
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. import java.awt.geom.*;
  6. import java.util.*;
  7. import javax.swing.*;
  8.  
  9. /**
  10. * 一个带有鼠标操作的用于添加和删除正方形的组件。
  11. */
  12. public class MouseComponent extends JComponent
  13. {
  14. private static final int DEFAULT_WIDTH = 300;
  15. private static final int DEFAULT_HEIGHT = 200;
  16.  
  17. private static final int SIDELENGTH = 10;
  18. private ArrayList<Rectangle2D> squares;//声明一个正方形集合
  19. private Rectangle2D current; // java类库中用来描述矩形的类,它的对象可以看作是一个矩形
  20.  
  21. public MouseComponent()
  22. {
  23. squares = new ArrayList<>();
  24. current = null;
  25.  
  26. addMouseListener(new MouseHandler());// 添加一个我们实现的类,这个类继承了监测鼠标点击情况的MouseListener
  27. addMouseMotionListener(new MouseMotionHandler());// 添加另一个实现类,这个类继承了监测鼠标移动情况的MouseMotionListener
  28. }
  29.  
  30. public Dimension getPreferredSize()
  31. {
  32. return new Dimension(DEFAULT_WIDTH, DEFAULT_HEIGHT);
  33. }
  34.  
  35. public void paintComponent(Graphics g)
  36. {
  37. var g2 = (Graphics2D) g;
  38.  
  39. // 绘制所有的正方形
  40. for (Rectangle2D r : squares)
  41. g2.draw(r);
  42. }
  43.  
  44. /**
  45. * 判断在这个坐标上是否有图形
  46. * @param p a point
  47. * @return the first square that contains p
  48. */
  49. public Rectangle2D find(Point2D p)
  50. {
  51. for (Rectangle2D r : squares)
  52. {
  53. if (r.contains(p)) return r; //contains方法:测定坐标是否在图形的边界内
  54. }
  55. return null;
  56. }
  57.  
  58. /**
  59. * 在这个坐标位置增加一个图形 .
  60. * @param p the center of the square
  61. */
  62. public void add(Point2D p)
  63. {
  64. //获取x,y的坐标
  65. double x = p.getX();
  66. double y = p.getY();
  67.  
  68. current = new Rectangle2D.Double(x - SIDELENGTH / 2, y - SIDELENGTH / 2,
  69. SIDELENGTH, SIDELENGTH);//用获得的坐标和既定的边长构建一个新的正方形,并将其赋值给current
  70. squares.add(current);
  71. repaint();
  72. }
  73.  
  74. /**
  75. * 从集合中移除正方形
  76. * @param s the square to remove
  77. */
  78. public void remove(Rectangle2D s)
  79. {
  80. if (s == null) return;//移除内容为空,返回
  81. if (s == current) current = null;//如果要移除的内容和current正指向的内容相同,则将current清空
  82. squares.remove(s);
  83. repaint();
  84. }
  85.  
  86. private class MouseHandler extends MouseAdapter
  87. {
  88. public void mousePressed(MouseEvent event)
  89. {
  90. // 如果光标不在正方形中,则添加一个新的正方形
  91. current = find(event.getPoint());
  92. if (current == null) add(event.getPoint());
  93. }
  94.  
  95. public void mouseClicked(MouseEvent event)
  96. {
  97. // 如果双击,删除当前方块
  98. current = find(event.getPoint());
  99. if (current != null && event.getClickCount() >= 2) remove(current);
  100. }
  101. }
  102.  
  103. private class MouseMotionHandler implements MouseMotionListener
  104. {
  105. public void mouseMoved(MouseEvent event)
  106. {
  107. // set the mouse cursor to cross hairs if it is inside a rectangle设置鼠标光标
  108.  
  109. if (find(event.getPoint()) == null) setCursor(Cursor.getDefaultCursor());
  110. else setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
  111. }
  112.  
  113. public void mouseDragged(MouseEvent event)
  114. {
  115. if (current != null)
  116. {
  117. //因为在调用这个方法之前肯定会调用点击鼠标的方法
  118. //所以我们直接判断:如果现在current不为空
  119. //像素(强制转换为int)
  120. int x = event.getX();
  121. int y = event.getY();
  122.  
  123. // drag the current rectangle to center it at (x, y)
  124. current.setFrame(x - SIDELENGTH / 2, y - SIDELENGTH / 2, SIDELENGTH, SIDELENGTH);
  125. repaint();
  126. }
  127. }
  128. }
  129. }

MouseTest.java:

  1. package mouse;
  2.  
  3. import java.awt.*;
  4. import javax.swing.*;
  5.  
  6. /**
  7. * @version 1.35 2018-04-10
  8. * @author Cay Horstmann
  9. */
  10. public class MouseTest
  11. {
  12. public static void main(String[] args)
  13. {
  14. EventQueue.invokeLater(() -> {
  15. var frame = new MouseFrame();
  16. frame.setTitle("MouseTest");
  17. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  18. frame.setVisible(true);
  19. });
  20. }
  21. }

MouseFrame.java:

  1. package mouse;
  2.  
  3. import javax.swing.*;
  4.  
  5. /**
  6. * A frame containing a panel for testing mouse operations
  7. */
  8. public class MouseFrame extends JFrame
  9. {
  10. public MouseFrame()
  11. {
  12. add(new MouseComponent());//向框架中添加一个JComponent的实例
  13. pack();//依据放置的组件设定窗口的大小, 使之正好能容纳放置的所有组件
  14. }
  15. }

运行结果如下:

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

1)   程序设计思路简述;

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

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

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

代码如下:

Test.java:

  1. import java.awt.*;
  2. import javax.swing.*;
  3. public class Test
  4. {
  5. public static void main(String[] args)
  6. {
  7. EventQueue.invokeLater(() -> {
  8. nameRardom i = new nameRardom();
  9. i.setTitle("点名器");
  10. i.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  11. i.setVisible(true);
  12.  
  13. });
  14. }
  15. }

 nameRardom.java:

  1. import java.awt.Color;
  2. import java.awt.Container;
  3. import java.awt.event.*;
  4. import java.io.*;
  5. import java.util.*;
  6. import java.util.Timer;
  7.  
  8. import javax.swing.*;
  9.  
  10. public class nameRardom extends JFrame {
  11. private ArrayList arrayList;
  12. {
  13. arrayList = new ArrayList<>();
  14. //读文件
  15. File file = new File("D:/2019studentlist.txt");
  16. FileInputStream fis;
  17. try {
  18. fis = new FileInputStream(file);
  19. InputStreamReader in = new InputStreamReader(fis);
  20. BufferedReader buf = new BufferedReader(in);
  21. String readLine;
  22. while ((readLine = buf.readLine())!=null) {
  23. arrayList.add(readLine);
  24.  
  25. }
  26. } catch (FileNotFoundException e1) {
  27.  
  28. e1.printStackTrace();
  29. } catch (IOException e1) {
  30.  
  31. e1.printStackTrace();
  32. }
  33. }
  34. private JPanel a;
  35. private static final int DEFAULT_WIDTH = 500;
  36. private static final int DEFAULT_HEIGHT = 300;
  37. public nameRardom() {
  38. setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
  39. a = new JPanel();
  40. a.setLayout(null);
  41. JLabel jLabel = new JLabel("随机点名器");
  42. JButton jButton = new JButton("开始");
  43. jLabel.setBounds(200, 50, 65, 40);
  44. jButton.setBounds(200, 90, 65, 40);
  45. jButton.addActionListener(new ActionListener() {
  46. Timer timer;
  47. public void actionPerformed(ActionEvent e) {
  48. if (jButton.getText().equals("开始")) {
  49. timer = new Timer();;
  50. TimerTask timerTask = new TimerTask() {
  51. public void run() {
  52. jButton.setText("停止");
  53. jButton.setBackground(Color.pink);
  54. jLabel.setText((String) arrayList.get((int) (Math.random() * 35)));
  55. }
  56. };
  57. timer.schedule(timerTask, 0, 100);
  58. }
  59. if (jButton.getText().equals("停止")) {
  60. timer.cancel();
  61. jButton.setText("开始");
  62. jButton.setBackground(Color.LIGHT_GRAY);
  63. }
  64. }
  65. });
  66. a.add(jLabel);
  67. a.add(jButton);
  68. add(a);
  69. }
  70. }

运行结果如图:

结对小伙伴:高文利。

摆拍:

实验总结:

本章我们学习了图形界面事件处理技术的知识,首先掌握了事件处理的基本原理,并学会了事件处理的基本编程模型;在老师的演示代码过程中,我清楚的学习到了lambda表达式的简便性,简化代码的好处和代码的多变性;在实验中,通过助教学长的演示和在书上源代码基础上进行的改编、添加,做出了点名器的代码程序,感觉又是一次充实的实验。

201871010113-刘兴瑞《面向对象程序设计(java)》第十三周学习总结的更多相关文章

  1. 201771010134杨其菊《面向对象程序设计java》第九周学习总结

                                                                      第九周学习总结 第一部分:理论知识 异常.断言和调试.日志 1.捕获 ...

  2. 201871010132-张潇潇《面向对象程序设计(java)》第一周学习总结

    面向对象程序设计(Java) 博文正文开头 项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ 这个作业的要求在哪里 https://www.cn ...

  3. 扎西平措 201571030332《面向对象程序设计 Java 》第一周学习总结

    <面向对象程序设计(java)>第一周学习总结 正文开头: 项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ 这个作业的要求在哪里 ...

  4. 杨其菊201771010134《面向对象程序设计Java》第二周学习总结

    第三章 Java基本程序设计结构 第一部分:(理论知识部分) 本章主要学习:基本内容:数据类型:变量:运算符:类型转换,字符串,输入输出,控制流程,大数值以及数组. 1.基本概念: 1)标识符:由字母 ...

  5. 201871010124 王生涛《面向对象程序设计JAVA》第一周学习总结

    项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ 这个作业的要求在哪里 https://edu.cnblogs.com/campus/xbsf/ ...

  6. 201871010115——马北《面向对象程序设计JAVA》第二周学习总结

    项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ 这个作业的要求在哪里 https://www.cnblogs.com/nwnu-daizh/p ...

  7. 201777010217-金云馨《面向对象程序设计(Java)》第二周学习总结

    项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ 这个作业的要求在哪里 https://www.cnblogs.com/nwnu-daizh/p ...

  8. 201871010132——张潇潇《面向对象程序设计JAVA》第二周学习总结

    项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ 这个作业的要求在哪里 https://www.cnblogs.com/nwnu-daizh/p ...

  9. 201771010123汪慧和《面向对象程序设计Java》第二周学习总结

    一.理论知识部分 1.标识符由字母.下划线.美元符号和数字组成, 且第一个符号不能为数字.标识符可用作: 类名.变量名.方法名.数组名.文件名等.第二部分:理论知识学习部分 2.关键字就是Java语言 ...

  10. 201521123061 《Java程序设计》第十三周学习总结

    201521123061 <Java程序设计>第十三周学习总结 1. 本周学习总结 2. 书面作业 1. 网络基础 1.1 比较ping www.baidu.com与ping cec.jm ...

随机推荐

  1. Exceptionless应用--自定义插件

    遇到的问题/需求 这里会把一些敏感的参数记录下来,我们需要屏蔽掉,如图 我们希望日志里面有当前登录用户的信息,如图: 处理方法 tip:这里用的是.net非.net core 第一个问题(屏蔽敏感参数 ...

  2. How to: Use the Entity Framework Model First in XAF 如何:在 XAF 中使用EF ModelFirst

    This topic demonstrates how to use the Model First entity model and a DbContext entity container in ...

  3. [转]uipath orchestrator installation

    本文转自:https://dotnetbasic.com/2019/08/uipath-orchestrator-installation.html UiPath Orchestrator Insta ...

  4. 从0系统学Android--3.2四种基本布局

    从0系统学Android--3.2四种基本布局 本系列文章目录:更多精品文章分类 本系列持续更新中.... 3.3 系统控件不够用?创建自定义控件 上一节我们学习了 Android 中的一些常用的控件 ...

  5. Mysql 事务及其原理

    Mysql 事务及其原理 什么是事务 什么是事务?事务是作为单个逻辑工作单元执行的一系列操作,通俗易懂的说就是一组原子性的 SQL 查询.Mysql 中事务的支持在存储引擎层,MyISAM 存储引擎不 ...

  6. BIM工程信息管理系统-EF实体框架数据操作基类

    EF实体框架数据操作基类主要是规范增.改.查.分页.Lambda表达式条件处理,以及异步操作等特性,这样能够尽可能的符合基类这个特殊类的定义,实现功能接口的最大化重用和统一. 1.程序代码 /// & ...

  7. 软工第八次实验——Git

    hiahiahia我又来作恶了,莫名其妙的第八次实验还要更新! 文章目录 一.Git 1.1 概述 1.1.1 Git 1.1.2 分布式版本控制系统 1.1.3 指令集 1.2 版本控制系统 1.2 ...

  8. RHEL5.6静默安装oracle11.2.0数据库实例脚本

    脚本:单实例静默安装echo '[GENERAL] RESPONSEFILE_VERSION = "11.2.0" //查看虚拟机的版本,不能更改 OPERATION_TYPE = ...

  9. python 爬虫 随机换user-agent

    USER_AGENTS = [ "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3) AppleWebKit/535.20 (KHTML, like ...

  10. C++入门到理解阶段二基础篇(8)——C++指针

    1.什么是指针? 为了更加清楚的了解什么是指针?我们首先看下变量和内存的关系,当我们定义了int a=10之后.相当于在内存之中找了块4个字节大小的空间,并且存储10,要想操作这块空间,就通过a这个变 ...