版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
本章内容图形用户界面开发概述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年大学地理(气候学原理)试题及答案
- 2025年中职饲草栽培与加工(饲草品质提升技术)试题及答案
- 2025四川雅安石棉县佳业劳务派遣有限公司招聘石棉县应急救援指挥中心辅助人员1人备考题库及答案详解(考点梳理)
- 2026四川遂宁市船山区中医医院招聘备考题库及答案详解1套
- 《中国传统能源地区低碳转型》专题政策研究报告
- 云南省部分学校2025-2026学年七年级上学期第一次月考历史试题(含答案)
- 2024届河南省濮阳市范县高三下学期模拟测试(二)历史试题(含答案)
- 2026浙江丽水学院招聘(引进)高层次人才71人备考题库(2026年第1号)及答案详解参考
- 2025云南昆明市盘龙区人民政府滇源街道办事处公益性岗位招聘5人备考题库含答案详解
- 2026“梦工场”招商银行银川分行寒假实习生招聘备考题库及答案详解(夺冠系列)
- 产品供货方案、售后服务方案
- 十八而志梦想以行+活动设计 高三下学期成人礼主题班会
- 2023年上海华东理工大学机械与动力工程学院教师岗位招聘笔试试题及答案
- TOC供应链物流管理精益化培训教材PPT课件讲义
- 医院18类常用急救药品规格清单
- 放弃公开遴选公务员面试资格声明
- 2023-2024学年江苏省海门市小学语文五年级期末点睛提升提分卷
- GB/T 1685-2008硫化橡胶或热塑性橡胶在常温和高温下压缩应力松弛的测定
- 北京城市旅游故宫红色中国风PPT模板
- DB42T1319-2021绿色建筑设计与工程验收标准
- 经济学原理 第一章课件
评论
0/150
提交评论