您的位置:首页 > 编程语言 > Java开发

Java读书笔记12 事件处理基础 Observer设计模式

2012-09-08 13:34 501 查看
本文主要内容:事件处理的基础概念,基本流程,还有一个简单的按钮事件例子(《Core Java》书中例子)。

1.事件处理基础知识

  JDK 1.1开始,Java的事件处理采用事件委托(代理)模型(event delegation)。

  在这个模型中,比较重要的几个概念如下:

  (也可以看完第二部分的事件处理过程之后再来看这些概念,或许思路明朗一些)。

  1.事件源(event source

    事件源是一个能够注册监听器对象并发送事件对象的对象。例如按钮或者滚动条就是事件源。

  2.事件,事件类型和事件对象

    事件一般是用户在界面上的一个操作,当一个事件发生时,该事件用一个事件对象来表示,事件对象有对应的事件类。

    不同的事件类描述不同类型的用户动作,不同的事件源可以产生不同类别的事件。例如,按钮可以发送ActionEvent对象,而窗口可以发送WindowEvent对象。

    在Java中,所有的事件对象都最终派生于java.util.EventObject类。

  3.事件监听器(event listener

    监听器对象是一个实现了特定监听器接口(listener interface)的类的实例。

    事件监听器类(监听器对象所属的类必须实现事件监听器接口或继承事件监听器适配器类。

    事件监听器接口定义了处理事件必须实现的方法。

    事件监听器适配器类是对事件监听器接口的简单实现。目的是为了减少编程的工作量。

    处理事件的方法被称为事件处理器,即事件监听器接口定义,并在事件监听器类中实现的方法。

  4.注册事件监听器

    为了能够让事件监听器检查某个组件(事件源)是否发生了某些事件,并且在发生时激活事件处理器进行相应的处理,必须在事件源上注册事件监听器。

    这是通过使用事件源组件的以下方法来完成的:

    addXxxListener(事件监听器对象)

    Xxx对应相应的事件类。

  5.再论事件和监听器

    每一类事件有一个相应的事件监听器接口,该接口定义了接收和处理事件的抽象方法。实现该接口的类,就是监听器类。其对象可作为监听器对象向相应的组件注册。

    事件的类名通常为:XxxEvent

    对应的事件监听器接口名通常为:XxxListener

    一个监听器接口定义了一种以上的抽象事件处理方法(事件处理器)。

    事件监听器类实现事件监听器接口,其类名可以由我们自己取。事件监听器类需要我们自己编写。

2.事件处理过程

  在这个event delegation模型中,事件源产生事件对象,然后将其发送给所有注册的的事件监听器对象,监听器对象利用事件对象中的信息决定如何对事件做出响应。

  从网上找的ppt中的一个图:

              例8-1 ButtonTest

/**
@version 1.32 2004-05-04
@author Cay Horstmann
*/

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ButtonTest
{
public static void main(String[] args)
{
ButtonFrame frame = new ButtonFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}

/**
A frame with a button panel
*/
class ButtonFrame extends JFrame
{
public ButtonFrame()
{
setTitle("ButtonTest");
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

// add panel to frame

ButtonPanel panel = new ButtonPanel();
add(panel);
}

public static final int DEFAULT_WIDTH = 300;
public static final int DEFAULT_HEIGHT = 200;
}

/**
A panel with three buttons.
*/
class ButtonPanel extends JPanel
{
public ButtonPanel()
{
// create buttons

JButton yellowButton = new JButton("Yellow");
JButton blueButton = new JButton("Blue");
JButton redButton = new JButton("Red");

// add buttons to panel

add(yellowButton);
add(blueButton);
add(redButton);

// 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
{
public ColorAction(Color c)
{
backgroundColor = c;
}

public void actionPerformed(ActionEvent event)
{
setBackground(backgroundColor);
}

private Color backgroundColor;
}
}


  例如,如果有一个用户在标有“Yellow”的按钮上点击了一下,那么yellowAction对象的actionPerformed方法就会被调用。这个对象的backgroudColor实例域设置为Color.YELLOW,然后就将面板的颜色设置为黄色了。

  有一个需要考虑的问题,是ColorAction对象(监听器对象)没有权限访问panel变量。可以采用两种方式解决这个问题:

    1.将面板存储在ColorAction对象中,并在ColorAction构造器中设置它;

    2.将ColorAction作为ButtonPanel类的内部类。这样一来,ColorAction就自动地拥有访问外部类的权限了。

  这里使用的就是第二种方法,ColorAction类中调用过了外部类ButtonPanel中的setBackground方法。这种情形十分常见,事件监听器对象通常需要执行一些对其他对象可能产生影响的操作,可以策略性地将监听器类放置在需要修改状态的那个类中。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: