




已阅读5页,还剩107页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
目标重点难点,【课前思考】1java语言是跨平台的编程语言,那么图形用户界面如何做到跨平台?2AWT有哪些组件和容器?它们各自的使用方法是什么?3AWT的事件处理模型是什么?原理又如何?【学习目标】掌握用AWT来设计图形用户界面的方法,尤其是组件、容器、布局管理器等概念。学习AWT事件处理模型,掌握事件源、事件、事件处理者等概念,让程序能够响应用户的操作。最后了解AWT各个组件的用法及所采用的事件处理接口。【学习指南】理解概念,多实践,勤思考,举一反三。【难重点】重点:事件处理模型。难点:内部类匿名类在AWT中的应用。,图形用户界面设计,使用AWT构造GUI组件容器布局管理AWT事件处理模型AWT组件,使用AWT构造,java.awt包提供了基本的java程序的GUI设计工具。ComponentContainerLayoutManager,Java.lang.Object,AWTEvent,Font,Componet,Graphics,MenuComponent,各种布局管理器类,Container,Panel,Applet,Window,Frame,Component(组件),Java的图形用户界面的最基本组成部分是组件,组件是一个可以以图形化的方式显示在屏幕上并能与用户进行交互的对象,例如一个按钮,一个标签等。组件不能独立地显示出来,必须将组件放在一定的容器中才可以显示出来。组件的一般功能基本的绘画支持外形控制大小和位置控制图像处理组件的状态控制,Container(容器),容器(Container)实际上是Component的子类,因此容器本身也是一个组件,具有组件的所有性质,另外还具有容纳其它组件和容器的功能。主要功能组件的管理布局管理,Graphics类,Graphics是所有用来在组件上进行图形绘制时所使用的图形环境上下文的父类.它提供了对组件进行绘制的一般方法和接口封装了用来进行图形绘制时必须的状态信息要绘制的组件对象当前颜色当前字体当前逻辑点操作的功能(Xor或者paint)当前XOR方式的替代颜色。,LayoutManager(布局管理器),为了使我们生成的图形用户界面具有良好的平台无关性,Java语言中,提供了布局管理器这个工具来管理组件在容器中的布局,而不使用直接设置组件位置和大小的方式。每个容器都有一个布局管理器,当容器需要对某个组件进行定位或判断其大小尺寸时,就会调用其对应的布局管理器。,在程序中安排组件的位置和大小时,应该注意:容器中的布局管理器负责各个组件的大小和位置,因此用户无法在这种情况下设置组件的这些属性。如果试图使用Java语言提供的setLocation(),setSize(),setBounds()等方法,则都会被布局管理器覆盖。如果用户确实需要亲自设置组件大小或位置,则应取消该容器的布局管理器,方法为:setLayout(null);,常用容器,FramePanelApplet,Frame,java.lang.Object|+-java.awt.Component|+-java.awt.Container|+-java.awt.Window|+-java.awt.Frame,importjava.awt.*;publicclassMyFrameextendsFramepublicstaticvoidmain(Stringargs)MyFramefr=newMyFrame(HelloOutThere!);fr.setSize(200,200);fr.setBackground(Color.red);fr.setVisible(true);publicMyFrame(Stringstr)super(str);,运行结果,Panel,java.lang.Object|+-java.awt.Component|+-java.awt.Container|+-java.awt.Panel,importjava.awt.*;publicclassFrameWithPanelextendsFramepublicFrameWithPanel(Stringstr)super(str);publicstaticvoidmain(Stringargs)FrameWithPanelfr=newFrameWithPanel(FramewithPanel);Panelpan=newPanel();,fr.setSize(200,200);fr.setBackground(Color.red);fr.setLayout(null);pan.setSize(100,100);pan.setBackground(Color.yellow);fr.add(pan);fr.setVisible(true);,运行结果:,LayoutManager,FlowLayoutBorderLayoutGridLayoutCardLayoutGridBagLayout,importjava.awt.*;publicclassExGuiprivateFramef;privateButtonb1;privateButtonb2;publicstaticvoidmain(Stringargs)ExGuithat=newExGui();that.go();,publicvoidgo()f=newFrame(GUIexample);f.setLayout(newFlowLayout();b1=newButton(PressMe);b2=newButton(DontPressMe);f.add(b1);f.add(b2);f.pack();f.setVisible(true);,运行结果,FlowLayout,Panel,Applet的缺省布局管理器。setLayout(newFlowLayout(FlowLayout.RIGHT,20,40);setLayout(newFlowLayoutFlowLayout.LEFT);setLayout(newFlowLayout();第1个参数设置对齐方向(alignment),缺省情况是居中,第2,3个参数设置横向和纵向的间隔(gap),缺省值是5,importjava.awt.*;publicclassmyButtonspublicstaticvoidmain(Stringargs)Framef=newFrame();f.setLayout(newFlowLayout();Buttonbutton1=newButton(Ok);Buttonbutton2=newButton(Open);Buttonbutton3=newButton(Close);f.add(button1);f.add(button2);f.add(button3);,f.setSize(300,100);f.setVisible(true);运行结果为:,BorderLayout,Window,Frame和Dialog的缺省布局管理器。BorderLayout布局管理器包括5个区域:North,South,East,West和Center。添加组件时,必须指明添加位置.否则无法显示.importjava.awt.*;publicclassbuttonDirpublicstaticvoidmain(Stringargs)Framef=newFrame(BorderLayout);f.setLayout(newBorderLayout();,f.add(North,newButton(North);f.add(South,newButton(South);f.add(East,newButton(East);f.add(West,newButton(West);f.add(Center,newButton(Center);f.setSize(200,200);f.setVisible(true);,运行结果,GridLayout,使容器中各个组件呈网格状布局。从上到下,从左到右一次排列.importjava.awt.*;publicclassButtonGridpublicstaticvoidmain(Stringargs)Framef=newFrame(GridLayout);f.setLayout(newGridLayout(3,2);f.add(newButton(1);f.add(newButton(2);,f.add(newButton(3);f.add(newButton(4);f.add(newButton(5);f.add(newButton(6);f.setSize(200,200);f.setVisible(true);,运行结果,CardLayout,CardLayout布局管理器能够帮助用户处理两个以至更多的成员共享同一显示空间。,importjava.awt.*;importjava.awt.event.*;publicclassThreePagesimplementsMousListenerCardLayoutlayout=newCardLayout();Framef=newFrame(“CardLayout”);Buttonpage1Button;Labelpage2Label;TextAreapage3Text;Buttonpage3Top;Buttonpage3Bottom;publicstaticvoidmain(Stringargs)newThreePages().go();,Publicvoidgo()f.setLayout(layout);f.add(page1Button=newButton(“Buttonpage”),“page1Button”);page1Button.addMouseListener(this);f.add(page2Label=newLabel(“Labelpage”),“page2Label”);page2Label.addMouseLisener(this);/注册监听器Panelpanel=newPanel();panel.setLayout(newBorderLayout();panel.add(page3Text=newTextArea(“Compositepage”),“Center”);,page3Text.addMouseListener(this);panel.add(page3Top=newButton(“Topbutton”),“North”);page3Top.addMouseListener(this);panel.add(page3Bottom=newButton(“Bottombutton”),“South”);page3Bottom.addMouseListener(this);f.add(panel,“panel”);f.setSize(200,200);f.setVisible(true);,publicvoidmouseClicked(MouseEvente)layout.next(f);publicvoidmouseEntered(MouseEvente)publicvoidmouseExited(MouseEvente)publicvoidmousePressed(MouseEvente)publicvoidmouseReleased(MouseEvente),模拟考题,Question31)Whichofthefollowingstatementsaretrue?1)ThedefaultlayoutmanagerforanAppletisFlowLayout2)ThedefaultlayoutmanagerforaFrameisFlowLayout3)AlayoutmanagermustbeassignedtoanAppletbeforethesetSizemethodiscalled4)TheFlowLayoutmanagerattemptstohonorthepreferredsizeofanycomponents,模拟考题,AnswertoQuestion31)1)ThedefaultlayoutmanagerforanAppletisFlowLayout4)TheFlowLayoutmanagerattemptstohonorthepreferredsizeofanycomponents,容器的嵌套,importjava.awt.*;publicclassExGui3privateFramef;privatePanelp;privateButtonbw,bc;privateButtonbfile,bhelp;publicstaticvoidmain(Stringargs)ExGui3gui=newExGui3();gui.go();,publicvoidgo()f=newFrame(GUIexample3);bw=newButton(West);bc=newButton(Workspaceregion);f.add(bw,West);f.add(bc,Center);p=newPanel();f.add(p,North);,bfile=newButton(File);bhelp=newButton(Help);p.add(bfile);p.add(bhelp);f.pack();f.setVisible(true);,运行结果:,FrameFrame是一个顶级窗口。Frame的缺省布局管理器为BorderLayout。PanelPanel无法单独显示,必须添加到某个容器中。Panel的缺省布局管理器为FlowLayout。当把Panel作为一个组件添加到某个容器中后,该Panel仍然可以有自己的布局管理器。因此,可以利用Panel使得BorderLayout中某个区域显示多个组件。,无布局管理器,setLayout(null),AWT事件处理机制,什么是事件?,Event事件,就是发生在用户界面上的用户交互行为所产生的一种效果。EventSource产生事件的对象。Eventhandler(Listener)接收事件对象并对其进行处理的方法。,事件处理模型,Hierachicalmodel(JDK1.0)事件传递机制。Delegationmodel(JDK1.1,1.2),授权(委托)处理机制。,DelegationModel(事件授权处理模型),将事件源对象和事件处理器(事件监听器)分开。,importjava.awt.*;importjava.awt.event.*;publicclassTestButtonpublicstaticvoidmain(Stringargs)Framef=newFrame(Test);Buttonb=newButton(PressMe!);b.addActionListener(newButtonHandler();f.setLayout(newFlowLayout();f.add(b);,f.setSize(200,100);f.setVisible(true);classButtonHandlerimplementsActionListenerpublicvoidactionPerformed(ActionEvente)System.out.println(Actionoccurred);,使用JDK1.1授权处理模型进行事件处理的一般方法:对于某种类型的事件XXXEvent,要想接收并处理这类事件,必须定义相应的事件监听器类,该类需要实现针对特定事件的特定接口XXXListener;ActionEventActionListener事件源中产生事件后,必须注册相应于该类事件的监听器,使用addXXXListener(XXXListener)方法来注册监听器。事件发生后,产生表示特定事件的事件对象,事件对象被传递给已经注册的事件监听器,调用监听器中的特定方法处理事件,事件对象,java.util.EventObject类EventObject类是所有事件对象的基础类,所有的事件类都是由它派生出来的。publicclassEventObjectimplementsjava.io.SerializableprotectedtransientObjectsource;publicEventObject(Objectsource);publicObjectgetSource();publicStringtoString();,java.awt.AWTEvent,和AWT有关的所有事件类都由java.awt.AWTEvent类派生,它也是EventObject类的子类。AWT事件共有10类,可以归为两大类:低级事件和高级事件。,低级事件ComponentEvent(组件事件:组件尺寸的变化,移动)ContainerEvent(容器事件:组件增加,移动)WindowEvent(窗口事件:关闭窗口,窗口闭合,图标化)FocusEvent(焦点事件:焦点的获得和丢失)KeyEvent(键盘事件:键按下、释放)MouseEvent(鼠标事件:鼠标单击,移动),高级事件(语义事件)ActionEvent(动作事件:按钮按下,TextField中按Enter键)AdjustmentEvent(调节事件:在滚动条上移动滑块以调节数值)ItemEvent(项目事件:选择项目,不选择“项目改变”)TextEvent(文本事件,文本对象改变),事件监听器,每类事件都有对应的事件监听器监听器是接口,根据动作来定义方法。interfaceKeyListenerextendsjava.util.EventListenerpublicvoidkeyPressed(KeyEventev);publicvoidkeyTeleased(KeyEventev);publicvoidkeyTyped(KeyEventev);,注册和注销监听器,注册监听器:publicvoidadd(listener);注销监听器:publicvoidremove(listener);,注册和注销监听器,AWT的组件类中提供注册和注销监听器的方法,例如Button类:(查API)publicclassButtonextendsComponent.publicsynchronizedvoidaddActionListener(ActionListenerl);publicsynchronizedvoidremoveActionListener(ActionListenerl);,AWT事件及其相应的监听器接口,ActionEvent激活组件ActionListenerActionPerformed(ActionEvent),ItemEvent选择了某些项目ItemListenerItemStateChanged(ItemEvent),MouseEvent鼠标移动MouseListenermouseDragged(MouseEvent)mouseMoved(MouseEvent),MouseEvent鼠标点击等MouseListenermousePressed(MouseEvent)mouseReleased(MouseEvent)mouseEntered(MouseEvent)mouseExited(MouseEvent)mouseClicked(MouseEvent),KeyEvent键盘输入KeyListenerKeyPressed(KeyEvent)KeyReleased(KeyEvent)KeyTyped(KeyEvent),FocusEvent组件收到或失去焦点FocusListenerfocusGained(FocusEvent)focusLost(focusEvent),AdjustementEvent移动了滚动条等组件AdjustmentListeneradjustmentValueChanged(AdjustmentEvent),ComponentEvent对象移动缩放显示隐藏等ComponentListenercomponentMoved(ComponentEvent)componentHidden(ComponentEvent)componentResized(ComponentEvent)componentShown(ComponentEvent),WindowEvent窗口收到窗口级事件WindowListenerwindowClosing(WindowEvent)windowOpened(WindowEvent)windowIconified(WindowEvent)windowDeiconified(WindowEvent)windowClosed(WindowEvent)windowActivated(WindowEvent)windowDeactivated(WindowEvent),ContainerEvent容器中增加删除了组件ContainerListenercomponentAdded(containerEvent)componentRemoved(containerEvent),TextEvent文本字段或文本区发生改变TextListenertextValueChanged(TextEvent),例12.12importjava.awt.*;importjava.awt.event.*;publicclassTwoListenimplementsMouseMotionListener,MouseListener,WindowListenerprivateFramef;privateTextFieldtf;publicstaticvoidmain(Stringargs)TwoListentwo=newTwoListen();two.go();,publicvoidgo()f=newFrame(Twolistenersexample);f.add(newLabel(Clickanddragthemouse),North);tf=newTextField(30);f.add(tf,South);f.addMouseMotionListener(this);f.addMouseListener(this);f.addWindowListener(this);f.setSize(300,200);f.setVisible(true);,publicvoidmouseDragged(MouseEvente)Strings=Mousedragging:X=+e.getX()+Y=+e.getY();tf.setText(s);publicvoidmouseMoved(MouseEvente)publicvoidmouseClicked(MouseEvente)publicvoidmouseEntered(MouseEvente)Strings=Themouseentered;tf.setText(s);,publicvoidmouseExited(MouseEvente)Strings=Themousehasleftthebuilding;tf.setText(s);publicvoidmousePressed(MouseEvente)publicvoidmouseReleased(MouseEvente)publicvoidwindowClosing(WindowEvente)System.exit(1);,publicvoidwindowOpened(WindowEvente)publicvoidwindowIconified(WindowEvente)publicvoidwindowDeiconified(WindowEvente)publicvoidwindowClosed(WindowEvente)publicvoidwindowActivated(WindowEvente)publicvoidwindowDeactivated(WindowEvente),可以声明多个接口implementsMouseMotionListener,MouseListener,WindowListener可以同时监听一个事件源上发生的多种事件:f.addMouseMotionListener(this);f.addMouseListener(this);f.addWindowListener(this);可以通过事件对象获得详细资料publicvoidmouseDragged(MouseEvente)Strings=“Mousedragging:X=”+e.getX()+“Y=”+e.getY();tf.setText(s);,事件适配器(EventAdapters),一种简单的实现监听器的手段,缺点是只能单一继承.importjava.awt.*;importjava.awt.event.*;publicclassMouseClickHandlerextendsMouseAdaperpublicvoidmouseClicked(MouseEvente),事件适配器(EventAdapters),Java.awt.event包中定义的适配器类有:ComponentAdapter(组件适配器)ContainerAdapter(容器适配器)FocusAdapter(焦点适配器)KeyAdapter(键盘适配器)MouseAdapter(鼠标适配器)MouseMotionAdapter(鼠标运动适配器)WindowAdapter(窗口适配器),模拟考题,Question19)Whichofthefollowingaretrue?1)Acomponentmayhaveonlyoneeventlistenerattachedatatime2)Aneventlistenermayberemovedfromacomponent3)TheActionListenerinterfacehasnocorrespondingAdapterclass4)Theprocessingofaneventlistenerrequiresatry/catchblock,模拟考题,AnswertoQuestion19)2)Aneventlistenermayberemovedfromacomponent3)TheActionListenerinterfacehasnocorrespondingAdapterclass,AWT组件库,按钮(Button),Buttonb=newButton(Quit);ActionEvent事件ActionListener接口getActionCommand()setActionCommand(),复选框(Checkbox),setLayout(newGridLayout(3,1);add(newCheckbox(“one”,null,true);add(newCheckbox(“two”);add(newCheckbox(“three”);,复选框(Checkbox),ItemEventItemListenergetStateChange()获取当前状态getItem()获得被修改复选框的字符串对象,classHandlerimplementsItemListenerpublicvoiditemStateChanged(ItemEventev)Stringstate=deselected;if(ev.getStateChange()=ItemEvent.SELECTED)state=selectedSystem.out.println(ev.getItem()+state);,复选框组(CheckboxGroup),setLayout(newGridLayout(3,1);CheckboxGroupcbg=newCheckboxGroup();add(newCheckbox(one,cbg,true);add(newCheckbox(two,cbg,false);add(newCheckbox(three,cbg,false);,下拉式菜单,ChoiceColorchooser=newChoice();Colorchooser.add(“Green”);Colorchooser.add(“Red”);Colorchooser.add(“Blue”);用ItemListener接口来进行监听,Canvas,如创建一个自定义组件,一个应用程序必须继承Canvas类才能获得有用的功能。如果想在画布上完成一些图形处理,则Canvas类中的paint()方法必须被重写。Canvas组件监听各种鼠标,键盘事件。当在Canvas组件中输入字符时,必须先调用requestFocus()方法。,例12.14importjava.awt.*;importjava.awt.event.*;importjava.util.*;publicclassMyCanvasimplementsKeyListener,MouseListenerCanvasc;Strings=“”;publicstaticvoidmain(Stringargs)Framef=newFrame(“Canvas”);MyCanvasmc=newMycanvas();mc.c=newCanvas();f.add(“Center”,mc.c);,f.setSize(150,150);mc.c.addMouseListerner(mc);mc.c.addKeyListener(mc);f.setVisible(true);publicvoidmouseClicked(MouseEventev)System.out.println(“MouseClicked”);c.requestFocus();publicvoidkeyTyped(KeyEventev)System.out.println(“KeyTyped”);s+=ev.getKeyChar();c.getGraphics().drawString(s,0,20);,publicvoidkeyPressed(KeyEventev)System.out.println(“KeyPressed”);publicvoidkeyReleased(KeyEventev)System.out.println(“KeyReleased”);publicvoidmousePressed(MouseEventev)System.out.println(“MousePressed”);publicvoidMouseReleased(MouseEventev)System.out.println(“MouseReleased”);publicvoidmouseEntered(MouseEventev)System.out.println(“MouseEntered”);publicvoidmouseExited(MouseEventev)System.out.println(“MouseExited”);,TextField,单行文本输入区当回车键被按下时,会发生ActionEvent事件,可以通过ActionListener中的actionPerformed()方法对事件进行相应处理。可以使用setEditable(boolean)方法设置为只读属性。,TextField,TextFieldtf1,tf2,tf3,tf4:tf1=newTextField();tf2=newTextField(“”,20);/显示区域为20列tf3=newTextField(“Hello!”);/按文本区域显示tf4=newTextField(“Hello!”,30);/*初始文本为Hello!,显示区域为30列,TextArea,TextArea可以显示多行多列的文本。使用setEditable(boolean)方法,可以将其设置为只读的。在TextArea中可以显示水平或垂直的滚动条。要判断文本是否输入完毕,可以在TextArea旁边设置一个按钮,通过按钮点击产生的ActionEvent对输入的文本进行处理。,列表(List),Listlst=newList(4,false);lst.add(“Venus”);lst.add(“Earth”);lst.add(“JavaSoft”);lst.add(“Mars”);cnt.add(lst);,Frame,顶级窗口,可以显示标题WindowEvent事件Frame无法直接监听键盘输入事件。,Dialog,是Window类的子类。对话框和一般窗口的区别在于它依赖于其它窗口。对话框分为非模式(non-modal)和模式(modal)两种。,文件对话框(Filedialog),FileDialogd=newFileDialog(ParentFr,“FileDialog);d.setVisible(true);Stringfilename=d.getFile();,菜单,无法直接将菜单添加到容器的某一位置,也无法使用布局管理器对其加以控制。菜单只能被添加到“菜单容器”中。,MenuBar,只能被添加到Frame对象中,作为整个菜单树的根基。Framefr=newFrame(MenuBar);MenuBarmb=newMenuBar();fr.setMenuBar(mb);fr.setSize(150,100);fr.setVisible
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 银行消防培训试题及答案
- 地质勘查专业试题及答案
- 电气资料专业试题及答案
- 专业测试题及答案
- 安徽省江淮名校2024-2025学年高二上学期期中考试物理试卷(含答案)
- 网络内容行业技术规范
- 客户见面致辞示例
- 个人工作总结副科长
- 集土坑施工方案
- 老旧小区临水施工方案
- 用药错误应急演练
- 考前速记-专升本英语240个高频词汇
- 九年级《道德与法治》(上册)教学计划及教学进度
- 医院课件:《抗肿瘤药物分级管理培训》
- 电气防爆专项检查表
- 教科版小学科学六年级上册《1.7微生物与健康》课件
- (正式版)JBT 106-2024 阀门的标志和涂装
- 便利店委托运营协议
- 汉语言文学专业就业方向及就业前景调研报告
- 招商加盟营销方案
- 红军长征感人红色故事3-10分钟10篇
评论
0/150
提交评论