免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2070 | 回复: 0
打印 上一主题 下一主题

java事件介绍 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2004-02-23 14:43 |只看该作者 |倒序浏览
1. java事件介绍

1.1 什么是事件

事件的正式定义:事件是代表另一对象抽象状态下变化的对象。事件本身就是抽象。生成事件的过程是无法从对象外部直接观察的。

怎样来理解???(我个人理解)事件就像是信使,它是负责传递信息,具体信息是什么它不管。它们从事件源、GUI组件或其它与之相互作用的对象那里将信息传输到任何对此感兴趣的时间监听器。(事件并不仅限于GUI组件,这点我想不用多说了)
事件它有构造函数、字段和类方法。事件通常有系统自动生成,而且自动传递到适当的事件处理程序;事件已可以有用户自定义来适应特殊应用程序的特定需要。

1.2 本地事件和分布事件

本地事件是使用单一Java虚拟且运行于单一平台上的应用程序生成的事件。
本地事件应用于Java API中的大多数GUI组件,也是Java Bean架构的一部分。

分布事件就是由远程目标生成的事件,运行于不同的平台使用不同的JVM。
它发生在使用Jini或JavaSpaces技术的基于网络或Web的应用程序中。将在以后又介绍。

1.3 Java事件处理的演变
略。……

1.4 Java事件的模型

1.4.1 Java1.0事件模型
略。……从GUI本身分理处事件处理代码是一件很困难的事情。

1.4.2 Java1.1事件模型

现在好了Java1.1事件模型使用了“委托”,事件源生成事件,随后把事件处理过程“委托”给另一段代码。这样事件处理对象可与GUI组件本身完全分离出来。
Java1.1事件模型还允许开发者通过使用定义再EventQueue类中的方法来检测事件队列中的内容。

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class SimpleExample extends Frame implements ActionListener
{
Button b;

public SimpleExample()
{

b = new Button("quit";
b.addActionListener(this);

setLayout(new FlowLayout());
add(b);

setBounds(100, 100, 200, 200);
setVisible(true);
}

public void actionPerformed(ActionEvent ae)
{
System.exit(0);
}

public static void main(String args[])
{
SimpleExample se = new SimpleExample();
}
}

2. Java事件的生命周期


图2-1 Java事件生命周期


3. 事件类
3.1 事件和支持类层次结构


图3-1 Java事件类层次结构
3.2 低层次与高层次事件类型

事件分为两大类:低层次与高层次。高层次事件称为语义事件;低层次时间就是那些任何组件类型都能生成的事件。
低层次事件的例子是键盘或鼠标事件,定义在Component和JComponent类内。
高层次事件是组件特定事件,高层次事件只由GUI组件的一小部分子集生成。

3.3 对J2SE中的事件类的描述
略……

3.4 用户生成的事件
有个示例:

import java.awt.event.*;
import java.awt.*;

public class UserGenerated2 extends Frame
{
Button b1, b2;
TextField jtf;

public UserGenerated2()
{
/* Two buttons and a textfield are created and placed on a frame */
/* The buttons register an ActionListener. */

b1 = new Button("b1";
b1.addActionListener(new ButtonListener());

b2 = new Button("b2";
b2.addActionListener(new ButtonListener());

jtf = new TextField(30);

Panel p = new Panel();
p.setLayout(new FlowLayout(FlowLayout.CENTER, 20, 20));
p.add(b1);
p.add(b2);

add(p, BorderLayout.CENTER);
add(jtf, BorderLayout.SOUTH);

addWindowListener(new WinClosing());
setBounds(100, 100, 200, 200);
setVisible(true);

/* After a delay of 5 seconds, an ActionEvent is instantiated and */
/* dispatched by the b1 button using the dispatchEvent() method. */

try
{
Thread.sleep(5000);
ActionEvent ae =
new ActionEvent(b1,ActionEvent.ACTION_PERFORMED,"disable";
b1.dispatchEvent(ae);
}
catch (InterruptedException ie) {}
}

/* The ActionListener is implemented as an inner class. If the */
/* action command of the incoming event is "disable", the object */
/* that dispatched the event is disabled. Otherwise, a message */
/* indicating which button was pressed is written in the textfield */

public class ButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{
if ( ae.getActionCommand().equals("disable" )
{
Button b = (Button)ae.getSource();
b.setEnabled(false);
}
else
{
jtf.setText( ae.getActionCommand()+" was pressed";
}
}
}

public static void main(String args[])
{
UserGenerated2 ug = new UserGenerated2();
}
}

/* The WinClosing class is a WindowListener that terminates the */
/* program if the application window is closed. */

class WinClosing extends WindowAdapter
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
}

4. 事件监听器
4.1 对接口的简要回顾
略……。
4.2 J2SE中的事件监听器接口

4.2.1 EventListener接口
EventListener接口来自java.util包,是所有事件侦听器的父接口。它并不声明任何方法或常量,而是作为标记用来表明任何扩展EventListener的接口都是一个事件监听器。

4.2.2包含在java.awt.event包内的监听器接口

public interface ActionListener extends EventListener
public interface AdjustmentListener extends EventListener
public interface AWTEventListener extends EventListener
public interface ComponentListener extends EventListener
public interface ContainerListener extends EventListener
public interface FocusListener extends EventListener
public interface HierarchyBoundsListener extends EventListener
public interface HierarchyListener extends EventListener
public interface InputMethodListener extends EventListener
public interface ItemListener extends EventListener
public interface KeyListener extends EventListener
public interface MouseListener extends EventListener
public interface MouseMotionListener extends EventListener
public interface TextListener extends EventListener
public interface WindowListener extends EventListener

4.2.3包含在javax.swing.event包内的监听器接口

public interface AncestorListener extends EventListener
public interface CaretListener extends EventListener
public interface cellEditorListener extends EventListener
public interface ChangeListener extends EventListener
public interface DocumentListener extends EventListener
public interface HyperlinkListener extends EventListener
public interface InternalFrameListener extends EventListener
public interface ListDataListener extends EventListener
public interface ListSelectionListener extends EvnetListener
public interface MenuDragMouseListener extends EventListener
public interface MenuKeyListener extends EventListener
public interface MenuListener extends EventListener
public interface MouseInputListener extends EventListener,MouseMotionListener
public interface PopupMenuListener extends EventListener
public interface TableColumnModelList extends EventListener
public interface TableModelListener extends EventListener
public interface TreeExpansionListener extends EventListener
public interface TreeSelectionListener extends EventListener
public interface TreeWillExpandListener extends EventListener
public interface UndoableEditListener extends EventListener

4.2.4包含在java.beans包内的监听器接口

public interface PropertyChangeListener extends EventListener
public interface VetoableChangeListener extends EventListener

4.2.5 定义在J2SE中的其它监听器接口

public interface BeanContextMumbershipListener extends EventListener
public interface BeanContextServiceRevokedListener extends EventListener
public interface BeanContextSerbicesListener extends EventListener
public interface ControllerEventListener extends EventListener
public interface DragGestureListener extends EventListener
public interface DragSourceListener extends EventListener
public interface DropTargetListener extends EventListener
public interface LineListener extends EventListener
public interface MetaEventListener extends EventListener
public interface NamespaceChangeListener extends EventListener
public interface NamingListener extends EventListener
public interface ObjectChangeListener extends EventListener
public interface UnsolicitedNotificationListener extends EventListener

4.3 事件监听器对象

事件监听器类就是实现一个或多个事件监听器接口的类。任何类都能充当事件监听器,而且实现事件监听器的方法不只一个。
由于事件监听器要接收事件源生成的事件,事件源必须把此监听器添加到它的监听器列表。事件源通过调用addListener()方法来实现这一目的。同样也可以删除监听器,事件源调用removeListener()方法来实现这一目的。

4.4 创建事件监听器
4.4.1 方法1:让GUI组建充当事件监听器

import java.awt.*;
import java.awt.event.*;

public class TestListener extends Frame implements ActionListener
{
private Button b;

public TestListener()
{

b = new Button("quit";
b.addActionListener(this);

add(b);
setLayout(new FlowLayout());
setBounds(100, 100, 200, 200);
setVisible(true);
}

public void actionPerformed(ActionEvent ae)
{
System.exit(0);
}

public static void main(String args[])
{
TestListener tl = new TestListener();
}
}

4.4.2方法2:把事件监听器作为单独类来实现

import java.awt.*;
import java.awt.event.*;

public class TestListener2 extends Frame
{
Button b;

public TestListener2()
{

b = new Button("quit";
b.addActionListener(new QuitHandler());

add(b);
setLayout(new FlowLayout());
setBounds(100, 100, 200, 200);
setVisible(true);
}

public static void main(String args[])
{
TestListener2 tl = new TestListener2();
}
}

class QuitHandler implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{
System.exit(0);
}
}

4.4.3 方法3:把事件监听器作为内部类来实现

import java.awt.*;
import java.awt.event.*;

public class TestListener3 extends Frame
{
private Button b;
private TextField textField;
private int count;

public TestListener3()
{

count = 0;

b = new Button("Add";
b.addActionListener(new ButtonHandler());

textField = new TextField(20);
textField.setEditable(false);
textField.setText("count is "+count);

setLayout(new FlowLayout());
add(b);
add(textField);

addWindowListener(new WinAdapter());
setBounds(100, 100, 200, 200);
setVisible(true);
}

class ButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{
++count;
textField.setText("count is "+count);
}
}

public static void main(String args[])
{
TestListener3 tl = new TestListener3();
}
}

/* This makes sure the application terminates if the window is closed */

class WinAdapter extends WindowAdapter
{
public void windowClosing(WindowEvent event)
{
System.exit(0);
}
}

4.4.4 方法4:把事件监听器作为匿名内部类来实现。

import java.awt.*;
import java.awt.event.*;

public class TestListener4 extends Frame
{
Button b;

public TestListener4()
{

b = new Button("quit";
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae)
{
System.exit(0);
}
});

add(b);
setLayout(new FlowLayout());
setBounds(100, 100, 200, 200);
setVisible(true);
}

public static void main(String args[])
{
TestListener4 tl = new TestListener4();
}
}

4.5 监听器适配器类
这里用两个示例大家就明白了
示例一:

import java.awt.*;
import java.awt.event.*;

public class NoAdapter extends Frame
{
private Label lbl;

public NoAdapter()
{
lbl = new Label("No Adapters Here");

setLayout(new FlowLayout());
add(lbl);

addWindowListener(new WinListener());
setBounds(100, 100, 200, 200);
setVisible(true);
}

public static void main(String args[])
{
NoAdapter na = new NoAdapter();
}
}

class WinListener implements WindowListener
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}

public void windowActivated(WindowEvent we) {}
public void windowClosed(WindowEvent we) {}
public void windowDeactivated(WindowEvent we) {}
public void windowDeiconified(WindowEvent we) {}
public void windowIconified(WindowEvent we) {}
public void windowOpened(WindowEvent we) {}
}

示例二:

import java.awt.*;
import java.awt.event.*;

public class WithAdapter extends Frame
{
private Label lbl;

public WithAdapter()
{
lbl = new Label("Adapters are nice");

setLayout(new FlowLayout());
add(lbl);

addWindowListener(new WinListener());
setBounds(100, 100, 200, 200);
setVisible(true);
}

public static void main(String args[])
{
WithAdapter na = new WithAdapter();
}
}

class WinListener extends WindowAdapter
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
}

4.6 包含在java.awt.event包内的监听器适配类

public abstract class ComponentAdapter extends Object implements ComponentListener
public abstract class ContainerAdapter exetends Object implements ContainerListener
public abstract class FocusAdapter exetends Object implements FocusListener
public abstract class HierarchyBoundsAdapter exetends Object implements HierarchyBoundsListener
public abstract class KeyAdapter exetends Object implements KeyListener
public abstract class MouseAdapter exetends Object implements MouseListener
public abstract class MouseMotionAdapter exetends Object implements MouseMotionListener
public abstract class WindowAdapter exetends Object implements WindowListener

4.7包含在javax.swing.event包内的监听器适配类

public abstract class InternalFrameAdapter exetends Object implements InternalFrameListener
public abstract class MouseInputAdapter exetends Object implements MouseInputListener

4.8 用户定义的事件监听器接口

创建用户自定的监听器接口的注意事项:一旦创建了用户定义的监听器接口,必须建立一个机制,已从事件源组建维护的监听器列表中添加或删除用户定义的监听器对象.可以使用EventListenerList或者AWTEventMulticaster类来实现这一目的。(在以后会有详细讲解)

4.9把事件源连接到事件监听器
addxxxListener();(例如:addFocusListener(FocusListener fl))

4.10 从事件监听器断开事件源
removexxxListener();(例如:removeFocusListener(FocusListener fl))

示例 组建如何添加和删除监听器

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

public class AddRemoveListener extends JFrame
{
JPanel panel;
JTextField jtf;
JButton button;
MMListener mml;

public AddRemoveListener()
{

mml = new MMListener();

jtf = new JTextField(30);
jtf.setEditable(false);

panel = new JPanel();
panel.setBackground(Color.yellow);
panel.addMouseMotionListener(mml);

button = new JButton("Remove");
button.setBorder(BorderFactory.createRaisedBevelBorder());
button.setFont(new Font("Serif", Font.PLAIN, 14));
button.addActionListener(new ButtonListener());

JPanel p = new JPanel();
p.add(jtf);
p.add(button);
getContentPane().add(panel, BorderLayout.CENTER);
getContentPane().add(p, BorderLayout.SOUTH);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 600, 250);
setVisible(true);
}

class ButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{
panel.removeMouseMotionListener(mml);
jtf.setText("");
}
}

class MMListener extends MouseMotionAdapter
{
public void mouseMoved(MouseEvent me)
{
jtf.setText("Mouse at position "+me.getX()+","+
me.getY());
}
}

public static void main(String args[])
{
AddRemoveListener arl = new AddRemoveListener();
}
}
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP