




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
JavaGUI程序设计(下)单击此处编辑副标题样式版权声明华清远见教育集团;未经华清远见明确许可,不得为任何目的以任何形式复制或传播此文档的任何部分;本文档包含的信息如有更改,恕不另行通知;华清远见教育集团保留所有权利。目录JavaAWT事件处理机制窗口事件处理事件类型和相应的接口及其方法事件适配器处理事件的方式Awt常用组件Awt中的视觉控制Jar命令的使用事件处理事件(Event)–一个对象,它描述了发生什么事情事件源(Eventsource)–产生事件的组件事件处理方法(Eventhandler)–能够接收、解析和处理事件类对象、实现和用户交互的方法awt中的事件处理方式委派式事件处理(delegation)----个别的组件将整个事件处理委托给特定的对象,当该组件发生指定的事件时,就通知所委托的对象,有这个对象来处理这个事件。这个受委托处理事件的对象称为事件监听对象(eventlistener)每个组件均可以针对特定的事件指定一个或多个事件监听对象,由这些事件监听对象负责处理事件awt事件分类
EventObjectActionEventAdjustmentEventAWTEventItemEventTextEventContainerEventFocusEventInputEventPaintEventWindowEventKeyEventMouseEventComponentEvent事件处理例子……b.addActionListener(newButtonListener());classButtonListenerimplementsActionListener{ publicvoidactionPerformed(ActionEvente){ clickCount++; t.setText(“点击‘ClickMe’按钮:”+clickCount+“次。"); } }……一个事件多个处理器例子b1.addActionListener(newButtonListener1());b2.addActionListener(newButtonListener2());窗口事件当一个窗口被激活、撤销激活、打开、关闭、最大化、最小化时,发生窗口事件。从WindowEvent类中创建的对象表示窗口事件。不同事件类型的接口和方法分类接口名方法ActionActionListeneractionPerformed(ActionEvent)ItemItemListeneritemStateChanged(ItemEvent)MouseMotionMouseMotionListenermouseDragged(MouseEvent)mouseMoved(MouseEvent)MouseMouseListenermousePressed(MouseEvent)mouseReleased(MouseEvent)mouseEntered(MouseEvent)mouseClicked(MouseEvent)mouseExited(MouseEvent)KeyKeyListenerkeyPressed(KeyEvent)keyReleased(KeyEvent)keyTyped(KeyEvent)FocusFocusListenerfocusGained(FocusEvnet)focusLost(FocusEvent)分类接口名方法AdjustmentAdjustmentListeneradjustmentValueChanged(AdjustmentEvent)ComponentComponentListenercomponentMoved(ComponentEvent)componentHidden(ComponentEvent)componentResized(ComponentEvent)componentShown(ComponentEvent)WindowWindowListenerwindowClosing(WindowEvent)windowOpened(WindowEvent)windowIconified(WindowEvent)windowDeiconified(WindowEvent)windowClosed(WindowEvent)windowActivated(WindowEvent)windowDeactivated(WindowEvent)ContainerContainerListenercomponentAdded(ContainerEvent)componentRemoved(ContainerEvent)TextTextListenertextValueChanged(TextEvent)不同事件类型的接口和方法事件适配器(Adapter)为简化编程,针对大多数事件监听器接口定义了相应的实现类----事件适配器类,在适配器类中,实现了相应监听器接口中所有的方法,但不做任何事情。在定义监听器类时就可以继承事件适配器类,并只重写所需要的方法。事件处理类和产生事件的类的关系(1)利用内部类来处理事件:…Buttonb=newButton(“确定”);b.addActionListener(newAddListener());classAddListenerimplementsActionListener{ publicvoidactionPerformed(ActionEvente) {……}}…事件处理类和产生事件的类的关系(2)用匿名类处理事件:…this.addWindowListener(newWindowAdapter(){ publicvoidwindowClosing(WindowEvente){ System.exit(0); } } );……事件处理类和产生事件的类的关系(con.)通过其他类处理事件:publicclassEventHandler
implementsActionListener{ …}publicclassTestEvent{ … Buttonb=newButton(“Test”); b.addActionListener(newEventHandler()); …}事件处理类和产生事件的类的关系(3)直接在本类里处理事件:publicclassTestimplementsActionListener{ … Buttonb=newButton(“Cancel”); b.addActionListener(this); publicvoidactionPerformed(ActionEvente){ … } …}awt常用组件(1)Button 可接收点击操作的矩形GUI组件Canvas 用于绘图的面板Checkbox 复选框组件CheckboxMenuItem 复选框菜单项组件Choice 下拉式列表框,内容不可改变Component 组件类Container 容器类awt常用组件(2)Dialog 对话框组件,顶级窗口、带标题栏Frame 基本的JavaGUI窗口组件Label 标签类List 包含内容可变的条目的列表框组件Menu 菜单组件MenuItem 菜单项(二级菜单)组件Panel 基本容器类,不能单独存在awt常用组件(3)Scrollbar 滚动条组件ScrollPane 带水平及垂直滚动条的容器组件TextArea 多行文本域TextField 单行文本框Window 抽象的GUI窗口类,无布局管理器Button(按钮)的创建构造器:Button()Button(Stringlabel)TextField(文本框)创建构造器:TextField()TextField(StringDefaultText)TextField(intcolumns)TextField(StringDefaultText,intcolumns)TextField例子…… Framef=newFrame("MyTextField"); TextFieldt1=newTextField("MyTextField",20); TextFieldt2=newTextField("",20); f.setLayout(newBorderLayout()); f.add(t1,BorderLayout.NORTH); f.add(t2,BorderLayout.SOUTH);……TextArea例子importjava.awt.*;publicclassMyTextArea{ Framef=newFrame("MyTextArea"); TextAreat1=newTextArea("MyTextArea",4,20); publicMyTextArea() { f.setLayout(newBorderLayout()); f.add(t1,BorderLayout.NORTH); f.pack(); f.show(); } publicstaticvoidmain(Stringargs[]){ MyTextAreatf=newMyTextArea(); }}Dialog例子f=newFrame("MyDialogshow");f.setBackground(Color.black);f.setSize(100,200);d=newDialog(f,"MyDialog",true);……f.setVisible(true);d.setVisible(true);PopupMenu例子(1) p=newPopupMenu(); p.add(mi1=newMenuItem("ViewSource")); //addActionListenerforeachMenuItem mi1.addActionListener(newmiActionListener()); p.addSeparator(); p.add(mi2=newMenuItem("Print")); mi2.addActionListener(newmiActionListener()); p.add(mi3=newMenuItem("Refresh")); mi3.addActionListener(newmiActionListener()); p.addSeparator(); p.add(mi4=newMenuItem("Properties")); mi4.addActionListener(newmiActionListener()); //addthePopupMenutotheFrame add(p); //Enablethemouseevent enableEvents(AWTEvent.MOUSE_EVENT_MASK);PopupMenu例子(2)ScrollPane默认情况下,Frame、Panel等容器是没有滚动条的,即使这些容器中放置的其他组件已经无法完全显示。可以通过ScrollPane来实现滚动条功能:将原来放到Frame、Panel等容器中的组件改而放到ScrollPane中,然后将ScrollPane放到Frame、Panel等容器中。ScrollPane例子…… f=newFrame(); p=newPanel(); p.setLayout(newGridLayout(3,5,3,3)); sp=newScrollPane(); for(inti=0;i<name.length;i++){ b[i]=newButton(name[i]); p.add(b[i]); } sp.add(p); f.add(sp);……创建菜单创建一个菜单步骤:创建一个MenuBar对象,将其放置到菜单容器中(如Frame)创建若干个Menu对象,将其放置到MenuBar对象中创建若干个MenuItem对象,将其放置到Menu对象中MenuItem包括:MenuItem:普通的菜单项CheckboxMenuItem:可以选择的菜单项创建菜单例子f=newFrame("Menu");mb=newMenuBar();m1=newMenu("File");m2=newMenu("Edit");m3=newMenu("Help");mb.add(m1);mb.add(m2);mb.setHelpMenu(m3);f.setMenuBar(mb);mi1=newMenuItem("New");……m1.add(mi1);m1.add(mi2);……awt中的视觉控制(1)颜色类Color和字体类Font:Color类Color类将颜色按照sRGB标准格式进行封装,该格式中红、绿、蓝三原色的取值范围都是0~255。Color类定义了多个构造方法,常用的有:publicColor(intr,intg,intb)publicColor(intr,intg,intb,inta)//a--透明度参数Colorc=newColor(200,170,90);Colord=newColor(200,170,90,120);在GUI设计中使用Color类Buttonb=newButton(“Test”);Colorc=newColor(200,170,90);b.setBackground(c)awt中的视觉控制(2)Font类Fontf=newFont(“TimesRoman”,Fo
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 产品设计中生物材料的创新应用研究
- 人力资源管理在公共部门的挑战与机遇
- 产品研发与创新流程
- 个性化餐饮包装设计研究
- 中华民族的精神文明史略
- 中国互联网广告市场的现状与趋势
- 中国食品饮料行业市场分析报告
- 中国民间体育活动与推广方式研究
- 产品发布会中的安全与应急预案
- 企业信息管理与数据安全
- 数字经济税收征管挑战与对策-全面剖析
- 学校灭火及应急疏散预案
- 营养师考试中高级题型及答案释疑
- 第19课《十里长街送总理》 统编版语文(五四学制)六年级上册
- 2025年华侨港澳台学生联招考试英语试卷试题(含答案详解)
- 2025年临床医师定期考核必考复习题库及答案(1080题)
- ASTM-D3359-(附著力测试标准)-中文版
- GA 1800.5-2021电力系统治安反恐防范要求第5部分:太阳能发电企业
- 税务稽查管理-税务稽查实施
- 基于单片机的智能路灯控制系统外文文献
- 食材配送售后服务方案
评论
0/150
提交评论