Java GUI编程中的事件处理机制详细讲解文档格式.docx
- 文档编号:17763095
- 上传时间:2022-12-09
- 格式:DOCX
- 页数:10
- 大小:19.24KB
Java GUI编程中的事件处理机制详细讲解文档格式.docx
《Java GUI编程中的事件处理机制详细讲解文档格式.docx》由会员分享,可在线阅读,更多相关《Java GUI编程中的事件处理机制详细讲解文档格式.docx(10页珍藏版)》请在冰豆网上搜索。
MouseEvent类对应MouseMotionListener接口和MouseListener接口;
WindonEvent类对应WindonListener接口---即发生了XXXEvent类型的事件,那么处理该事件的接口为XXXListener);
它们的父类为EventObject类。
各个事件类的说明:
EventObject:
所有事件类的超类
最重要的方法--getSource(),返回产生某事件的对象
AWTEvent:
所有AWT事件类的超类
最重要的方法--getID(),返回某事件的ID号,事件的ID是一个整数,它指定事件的类型,例如按钮事件或鼠标点击事件
ActionEvent:
激活组件时发生的事件
AdjustmentEvent:
调节可调整的组件(如移动滚动条)时发生的事件
ComponentEvent:
操纵某组件时发生的一个高层事件
ContainerEvent:
向容器添加或删除组件时发生
InputEvent:
由某输入设备产生的一个高层事件
ItemEvent:
从选择项,复选框或列表中选择时发生
KeyEvent:
操作键盘时发生
MouseEvent:
操作鼠标时发生
PaintEvent:
描绘组件时发生的一个事件
TextEvent:
更改文本时发生
WindowEvent:
操作窗口时发生的事件,如最大化或最小化某一窗口。
4)事件编程的基本原则:
事件处理的类代码要对某一类事件加以处理,则应实现它们所对应的接口,并且给出该接口中定义的全部事件响应函数的功能实现(重写其函数体);
然后在创建组件时注册该事件的监听器(响应者)。
5)事件注册:
事件源通过对特定的事件进行注册,以指定该事件的监听器(响应者)是谁。
6)事件注册函数:
函数名由“add+事件类型对应的监听器接口名称”组成;
函数参数为监听器对象(实现事件响应的类的对象,如容器组件自身响应该事件,则监听器对象应用this代表)。
publicvoidadd<
listenerType>
(<
ListenerObj)
{
}
7)事件编程步骤:
①实现某一事件的监听器接口(定义事件处理类并实现监听器接口)。
②在事件处理类中重写(实现)其事件处理的函数体。
③在创建AWT组件时注册事件处理代码以指定该事件的监听器(响应者)是谁。
publicclassButtonActionEventextendsApplet
implementsActionListener,MouseMotionListener,MouseListener
{
publicvoidinit()
{this.addActionListener(this);
this.addMouserMotionListener(this);
this.addMouseListener(this);
publicvoidactionPerFormed(ActionEventevent)
if(event.getSource()==SourceObject)
{//利用事件响应函数中的事件对象获取事件产生时的相关信息
elseif(event.getActionCommand()=="
组件的标签名字符文字"
publicvoidmouseDragged(MouseEventevent)
intx=event.getX();
inty=event.getY();
publicvoidmouseMove(MouseEventevent)
8)事件编程的基本规则:
(1)组件对事件的响应形式:
忽略它(本类不实现对应的监听器接口)或编程事件函数以处理它(可根据应用需要替换某一个组件的相应缺省事件处理函数,从而响应用户对该组件的操作。
本类实现某类事件对应的监听器接口,并实现对应的响应函数),也可屏蔽它(将其事件响应函数体置空)。
(2)事件响应类(监听器)可以实现多个监听器接口,以响应多组不同事件,从而可使同一个组件可以注册多种事件。
(3)利用事件响应函数中的事件对象获取事件产生时的相关信息(event.getSource())事件源对象,event.getX(),event.getY(),事件产生时的鼠标位置,event.getActionCommand(),获取组件的字符串名称。
9)各个事件监听器接口
ActionListener
使用它的常见AWT类Button,List,TextField
接口定义的方法
publicvoidactionPerformed(ActionEvente)
添加到组件的方法
Obj.addActionListener(ActionListenerl)
AdjustmentListner
使用它的常见AWT类ScrollBar
publicvoidadjustmentValueChanged(AdjustmentEvente)
Obj.addAdjustmentListener(AdjustmentListenerl)
ComponentListener
使用它的常见AWT类
publicvoidcomponentResized(ComponentEvente)
publicvoidcomponentMoved(ComponentEvente)
publicvoidcomponentShown(ComponentEvente)
publicvoidcomponentHidden(ComponentEvente)
Obj.addComponentListener(ComponentListenerl)
ContainerListener
publicvoidcomponentAdded(ContainerEvente)
publicvoidcomponentRemoved(ContainerEvente)
ItemListener
使用它的常见AWT类Checkbox,Choice,List
publicvoiditemStateChanged(ItemEvente)
FocusListener
publicvoidfocusGained(FocusEvente)
publicvoidfocusLost(FocusEvente)
KeyListener
publicvoidkeyTyped(KeyEvente)
publicvoidkeyPressed(KeyEvente)publicvoidkeyReleased(KeyEvente)
MouseListener
publicvoidmouseClicked(MouseEvente)
publicvoidmousePressed(MouseEvente)
publicvoidmouseReleased(MouseEvente)
publicvoidmouseEntered(MouseEvente)
publicvoidmouseExited(MouseEvente)
MouseMotionListener
publicvoidmouseDragged(MouseEvente)
publicvoidmouseMoved(MouseEvente)
TextListener
使用它的常见AWT类TextField,TextArea
publicvoidtextValueChanged(TextEvente)
WindowListener
publicvoidwindowOpened(WindowEvente)
publicvoidwindowClosing(WindowEvente)
publicvoidwindowClosed(WindowEvente)
publicvoidwindowIconified(WindowEvente)
publicvoidwindowDeiconified(WindowEvente)
publicvoidwindowActivated(WindowEvente)
publicvoidwindowDeactivated(WindowEvente)
10)事件适配器
由于事件监听器接口是Abstract类型,意谓着实现该接口的类应全部实现其各个成员函数,但实际应用中可能只需处理某些事件响应代码;
此时再采用实现事件监听器接口可能会导致编程复杂。
JDK中提供事件适配器方式来实现事件编程。
①每一种监听器接口有相应的适配器类,程序员只需让事件处理类从某一适配器类派出而不采用实现监听器接口的方式,这样仅需重写用户感兴趣的相应函数体代码。
各个事件监听器接口相相应的事件适配器类如下:
WindowListener------WindowAdapter
MouseListener-----MouseAdapter
MouseMotionListener-----MouseMotionAdapter。
②在事件注册时,应将监听器设为适配器的派生类(子类)对象,该适配器类中事件响应函数将被执行。
(责任编辑:
龚勋)
下面笔者以一个程序为例,来介绍如何编写具有“纯”JAVA的程序。
该程序的关键语句都有解释:
importjava.awt.*;
importjava.awt.event.WindowAdapter;
importjava.awt.event.WindowEvent;
importcom.sun.java.swing.*;
publicclassmyGUIextendscom.sun.java.swing.JFrame{
//此处设置环境常量,如:
字体,颜色等略去。
com.sun.java.swing.JPanelMainPanel;
com.sun.java.swing.JPanelTopPanel;
com.sun.java.swing.JButtonClearButton;
com.sun.java.swing.JButtonSearchButton;
com.sun.java.swing.JTextFieldSearchText;
//构造器:
对setMenu()和setGUI()数椐和调用的初始化和实例化
publicmyGUI(){
super("
IdealGUI"
);
addWindowListener(newWindowAdapter(){publicvoid
windowClosing(WindowEvente){dispose();
System.exit(0);
}});
//创
建窗口事件
setMenu();
setGUI();
}
protectedvoidClearButtonClicked(){
System.out.println("
ClearButtonClicked"
SearchText.requestFocus();
SearchText.selectAll();
}//处理ClearButton按钮事件子程序
protectedvoidSearchButtonClicked(){
System.out.println("
SearchButtonClicked"
}//处理SearchButton按钮事件子程序
classButtonActionimplementsjava.awt.event.ActionListener{
publicvoidactionPerformed(java.awt.event.ActionEvent
event){
Objectobject=event.getSource();
if(object==ClearButton)
ClearButtonClicked();
elseif(object==SearchButton)
SearchButtonClicked();
}//endpublicvoidactionPerformed(...
}//监听事件的发生,如按钮的按下,鼠标的按动等,转向相应的处
理。
...
publicvoidsetGUI(){
TopPanel=newcom.sun.java.swing.JPanel();
//TopPanel面板
java.awt.GridBagLayoutTopLayout=newjava.awt.GridBagLayout
();
TopPanel.setLayout(TopLayout);
ClearButton=newcom.sun.java.swing.JButton
("
Clear"
//ClearButton按钮
SearchText=newcom.sun.java.swing.JTextField("
Entertext
here."
//SearchText文本框
SearchButton=newJButton("
Search"
//SearchButton按钮
TopPanel.add(SearchText);
//在TopPanel上安装SearchText文本框
TopPanel.add(SearchButton);
///在TopPanel上安装SearchButton按
钮
TopPanel.add(ClearButton);
//在TopPanel上安装ClearButton按钮
MainPanel=newcom.sun.java.swing.JPanel();
//主面板MainPanel
java.awt.GridBagLayoutMainLayout=new
java.awt.GridBagLayout();
MainPanel.setLayout(MainLayout);
java.awt.GridBagConstraintsMainConstraints=new
java.awt.GridBagConstraints();
SetConstraints(MainConstraints,
GBCBOTH,TOPPANELX,TOPPANELY,1,1);
//TopPanel
MainLayout.setConstraints(TopPanel,MainConstraints);
MainPanel.add(TopPanel);
//将TopPanel装入主面板MainPanel
this.getContentPane().add(MainPanel);
//将主面板装入Jframe框架
this.setSize(640,480);
//设置显示大小,可在程序中更改
this.setVisible(true);
//设置可显示
//文本框聚焦(选中)
ButtonActionBAction=newButtonAction();
ClearButton.addActionListener(BAction);
//将按钮加入到活动监听器中
SearchButton.addActionListener(BAction);
publicstaticvoidmain(Stringargs[]){
myGUImGUI=newmyGUI();
}//启动主程序
JAVA的GUI界面是由一系列嵌入组件组成的,它使用了JDK和JFC(SWING)组件。
本例首先建立框架(JFRAME),在上面再建一个主面板(MAINPANEL),再往下建立一个子面板(TOPPANEL),然后将两个按钮和一个文本框插入到子面板中,这是一个典型的OOP实例,在程序分别捕捉到两个按钮的按动后,并显示按钮信息后再选取文本框中的内容。
这个软件经扩充,可加入更多的子面板,和更多更复杂的组件(如JTABLE-表格或JTREE-树)。
注意这个myGUI.java文件可用任何文本编辑软件编辑,以纯文本方式存盘。
JFC的组件比AWT的组件更美观,同样的按钮,JFC的好看,并能进行更多的设置。
在安装好JAVA1.2(JDK)和JFC(SWING1.1.1或更高版本,这些系统可在有关网站下载)后即可在WINDOWS的DOS方式下编译和运行文件了。
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Java GUI编程中的事件处理机制详细讲解 GUI 编程 中的 事件 处理 机制 详细 讲解