版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
本章内容图形用户界面开发概述Java事件模型常用组件与事件处理
图形用户界面开发概述人机交互的窗口,由一组图形界面成分和界面元素组成。主要工作:
创建界面,设置并布局,形成物理外观;定义事件和响应,实现交互功能。组成:容器、控制组件和用户自定义成分
容器:组织界面成分和元素。控制组件:图形用户界面组成单位。用户自定义部分:根据需要可自定义图形、标志图案等。AWT组件AWT——abstractwindowtoolkit(抽象窗口工具包)建立JFC的主要基础;Swing组件:建立在AWT之上,新的、功能更强大的图形组件包。事件类字体类GraphicsComponent颜色类布局管理类java.lang.ObjectCheckboxContainerButton…WindowPanelAppletDialogFrameList图象类菜单类JavaApplet一个JavaApplet程序中必须有一个类是Applet的子类publicclassExample8_1extendsAppletApplet类是包java.applet中的一个类,也是java.awt中Container的子类importjava.applet.*;publicvoidinit(){….}publicvoidstart(){……}publicvoidstop(){……}publicvoiddestroy(){….}publicvoidpaint(Graphicsg){……}publicvoidrepaint(Graphicsg){……}初始化init()启动start()中止stop()销毁destroy()浏览器下载并实例化Applet转入后台重新激活Applet退出浏览器Applet生命周期:
常用组件
Component类importjava.awt.*;importjava.awt.event.*;importjava.applet.*;publicclassMyWindow1extendsApplet
implementsActionListener{
privateButtonm_btn=newButton(“test”);publicvoidinit(){add(m_btn);
m_btn.addActionListener(this);}//init()
publicvoidactionPerformed(ActionEvente){System.out.println(“Hello”);}//实现接口
}//MyWindowJava事件模型事件源注册:btn.addActionListener(监听者对象)监听者类实现接口:publicvoidactionPerformed(ActionEvente)ActionEvent类型注册触发事件调用并传递参数ActionEvent类常用方法publicObjectgetSource(){…}
获取发生ActionEvent事件的事件源对象例:publicvoidactionPerformed(ActionEvente){if(e.getSource()==text1)label1.setText(“It’stext1!”);elseif(e.getSource()==text2)label1.setText(“It’stext2!”);…….}publicStringgetActionCommand(){…}发生事件时,获取文本框中的文本字符串例:publicvoidactionPerformed(ActionEvente){Strings=e.getActionCommand();label1.setText(s);}事件模型实现方法:方法1:定义类时实现监听接口publicclassMyWindow1extendsApplet
implementsActionListener{
privateButtonm_btn=newButton(“test”);publicvoidinit(){
add(m_btn);m_btn.addActionListener(this);}
publicvoidactionPerformed(ActionEvente){……}}方法2:在另一个类中实现监听接口
publicclassLess6extendsApplet{privateButtonm_btn=newButton(“test”);publicvoidinit(){
add(m_btn);m_btn.addActionListener(newbtnListener());}//init()}//Less6classbtnListenerimplementsActionListener{publicvoidactionPerformed(ActionEvente){
m_btn.setLabel(“事件被触发”);}//实现事件接口
}//btnListener
方法3:创建匿名内部类importjava.applet.*;importjava.awt.*;Importjava.awt.event.*;
publicclasstestextendsApplet
{
privateButtonm_btn=newButton(“test”);publicvoidinit(){add(m_btn);m_btn.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){
m_btn.setLabel(“事件被触发”);
}//actionPerformed
});}//init()}//testEventObjectActionEventItemEventAdjustmentEventComponentEventTextEventAWTEventMouseEventKeyEventContainerEventPaintEventFocusEventInputEventWindowEvent事件类体系结构图:事件类监听器接口增加监听器方法删除监听器方法ActionEventActionListeneraddActionListener()removeActionListener()AdjustmentEventAdjustmentListeneraddAdjustmentListener()removeAdjustmentListener()ComponentEventComponentListeneraddComponentListener()removeComponentListener()ContainerEventContainerListeneraddContainerListener()removeContainerListener()FocusEventFocusListeneraddFocusListener()removeFocusListener()KeyEventKeyListeneraddKeyListener()removeKeyListener()MouseEventMouseListeneraddMouseListener()removeMouseListener()MouseMotionListeneraddMouseMotionListener()removeMouseMotionListener()WindowEventWindowListeneraddWindowListener()removeWindowListener()ItemEventItemListeneraddItemListener()removeItemListener()TextEventTextListeneraddTextListener()removeTextListener()事件与监听器对照表监听器接口接口中的函数ActionListeneractionPerformedAdjustmentListeneradjustmentValueChangedComponentListenercomponentHidden,componentShown,componentMoved,componentResizedContainerListenercomponentAdded,componentRemovedFocusListenerfocusGained,focusLostKeyListenerkeyPressed,keyReleased,keyTypedMouseListenermouseClicked,mouseEntered,mouseExited,mousePressed,mouseReleasedMouseMotionListenermouseDragged,mouseMovedWindowListenerwindowOpened,windowClosing,windowClosed,windowActivated,windowDeactivated,windowIconified,windowDeiconifiedItemListeneritemStateChangedTextListenertextValueChanged标签(Label)创建标签
Label
prompt
=
newLabel(“你好”);常用方法
getText()setText(Strings)
if(prompt.getText()==“你好”)
prompt.setText(“再见”);产生事件
标签不能接收用户输入,所以不能引发事件。按钮(Button)创建按钮
Button
btn=
newButton(“操作”);常用方法
getLabel()setLabel(Strings)
Strings=btn.getLabel();btn.setLabel(“你好”);产生事件
单击按钮引发动作事件ActionEvent
对应的监听接口是ActionListener
文本框(TextField)P131创建文本框
TextFieldtxt=newTextField();TextFieldtxt=newTextField(“hello”);TextFieldtxt=newTextField(10);TextFieldtxt=newTextField(“hel
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年海南省公需课学习-中国居民膳食指南科学解读995
- 超声波热量表的温度补偿
- 2025年应急救援员理论知识考试题库(含答案)
- 2025年招聘网格员考试题及答案
- 主题作业评价(三) 隋唐时期的制度创新
- 2025年大自然的奇观题库及答案
- 合同范本已经填好
- 2025年番禺美术面试真题及答案
- 2025年人际认知理论题库及答案
- 2025年武汉初中政治真题及答案
- 口腔正畸学课件
- 血常规报告单模板
- 物联网就在身边初识物联网课件
- 路基拼接技术施工方案
- 宏观经济学PPT完整全套教学课件
- 陕09J02 屋面标准图集
- 2023年上海清算登记托管结算试题试题
- 动车组受电弓故障分析及改进探讨
- GB/T 41932-2022塑料断裂韧性(GIC和KIC)的测定线弹性断裂力学(LEFM)法
- 2023年浙江省大学生物理竞赛试卷
- GB/T 2007.1-1987散装矿产品取样、制样通则手工取样方法
评论
0/150
提交评论