实验十三 图形界面事件处理技术
实验时间 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) 掌握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();//生成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表达式的简便性,简化代码的好处和代码的多变性;在实验中,通过助教学长的演示和在书上源代码基础上进行的改编、添加,做出了点名器的代码程序,感觉又是一次充实的实验。
原文地址:https://www.cnblogs.com/dhlll/p/10014805.html