图形用户界面的设计与实现 PPT课件_第1页
图形用户界面的设计与实现 PPT课件_第2页
图形用户界面的设计与实现 PPT课件_第3页
图形用户界面的设计与实现 PPT课件_第4页
图形用户界面的设计与实现 PPT课件_第5页
已阅读5页,还剩50页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

图形用户界面的设计与实现 Lecture9 用户自定义成分GUI标准组件Java事件处理布局设计容器 提纲 AWTAPIs java awtjava awt accessibilityjava awt colorjava awt datatransferjava awt dndjava awt eventjava awt fontjava awt geomjava awt imagejava awt peerjava awt printjavax swing 用户自定义成分 AWT AbstractWindowingToolkit 包Color Font FontMetrics Component Graphics Polygon Toolkit 坐标 0 0 绘制图形P185例7 1 显示文字P187例7 2 控制颜色P189例7 3 显示图像P190例7 4 AWT标准组件 1 组件Button Checkbox Choice List Menu TextField这些控件产生ACTION EVENT 程序会响应该事件其它控件 ScrollbarandTextAreaCanvasLable容器类型 Panel ScrollPane window常见的容器 Dialog FileDialog Applet Frame AWT标准组件 2 布局类选择预先定义好的组件排放格式事件处理类封装用户与应用程序交互的信息其它组件类SizeandShapeClassColorClassImageClassFontandFontMetricsClassGraphics AWT标准组件 3 AWT标准组件 4 AWT事件模型 用户发出命令 会引发一个系统预先定义好的事件用户程序需要编制代码定义每个特定事件发生时程序应做出如何响应 图形用户界面的每个可能产生事件的组件被称为事件源 不同事件源上发生的事件的种类不同 事件源必须注册给能够处理该事件源上那种类型事件的监听者监听者可以是包容事件源的容器 也可以是另外的对象监听者具有监听和处理某类事件的功能 是因为它实现了相关的接口 这些接口内部包含了若干处理相关事件的抽象方法 监听者需要写出具体的方法体 委托事件模型 1 Stimulus1 Stimulus2 XEvevtSource1 YEvevtSource1 XEvent1 XEvent2 YEvent1 XEvevtListener1 XevevtandYEvevntListener1 委托事件模型 2 java awt AWTEvent是所有AWT事件 asubclassofjava util EventObject 的根AWTEvent的子类有 ActionEvent WindowEvent ItemEvent KeyEvent MouseEvent TextEvent 等 AWT事件类 java awt AWTEvent是所有AWT事件 asubclassofjava util EventObject 的根AWTEvent的子类有 ActionEvent WindowEvent ItemEvent KeyEvent MouseEvent TextEvent 等 java awt event定义了11个监听者接口 ActionListener Button MenuItem ListWindowListener WindowItemListener Choice List CheckboxKeyListener ComponentMouseListener ComponentMouseMotionListener ComponentTxetListener TxetComponent AWT事件接口 一个对象必须通过调用addXXXListener方法将自己注册到一个事件源上 组件ButtonCheckboxChoiceDialogboxFrameLabelListMenuPanelCanvasSliderorScrollbar AWT组件 标签 Label P197按钮 Button P197 198动作事件 ActionEvent P199文本框与文本域 TextFeild与TextArea P200 203单 复选按钮和列表 Checkbox Choice和List P203 213滚动条 Scrollbar P213 217画布 Canvas P217 224 组件与动作事件 CommonRulesBorderLayoutCardLayoutFlowLayoutGridLayoutGridBagLayoutCustomizationLayoutAbsoluteLocation 布局组件 ContainerandLayoutDefault panelandapplet FlowLayoutWindows BorderLayout Non DefaultContainer setLayout newCardLayout WheninvokeLayoutadd remove directlyinvoke layout indirectlyinvoke preferredSize minimumSize CommonRules 2 TheBorderLayoutcontainsfivedistincareas North South East West and Center ConstructFunctionpublicBorderLayout publicBorderLayout inthqap intvgap BorderLayout North South West East Center Thecomponentsarestackedontopofeachother likeadeckofcards sothatonlyonecomponentisvisibleatatimeConstructFunctionpublicCardlayout publicCardLayout inthgap intvgap CardLayout one one Ifthecontainerusesaflowlayout componentsarearrangedinarowacrosstheareaofthecontainerConstructFunctionpublicFlowLayout publicFlowLayout intalign publicFlowLayout intalign inthgap intvgap three one two four five FlowLayout ThislayoutmanagerdividestheareaofthecontainerintoagridofequallysizedrowsandcolumnsConstructionFunctionpublicGridlayout introws intcols publicGridlayout introws intcols inthgap intvgap GridLayout one two four five three six ThislayoutmanagerismoreflexiblethanagridlayoutbecausecomponentscanbeputinanycellandcanspanmoreoneoneroworcolumnConstructionFunctionpublicGridBagLayout GridBagLayout one four two three AGUIprogramusuallycontain settingthebasiccontainersettinglayoutmanageraddingsomecomponentsprogramminginteractiveeventhandlingcodes TheStructureofJavaGUIProgram Example Converter html TheClassesinConverterConverterClass TheCoreoftheProgram includingmainmethod initmethod ConversionPanelClass OrganizeallControllertogether GridLayoutObject ManagetheComponentinConverterGridBagLayout ConversionPanelClassuseittomanageitsComponent Example 1 TheHierarchyofComponent AFrame AConverter AConversionPanel metricPanel AConversionPanel usaPanel ATextField ALable AScrollar AChoice ATextField AScrollar AChoice ALable Example 2 TheDisplayingProcessFramedrawsitselfConvertdrawsitselfandhisBoardFrameConversionPaneldrawsitselfandhisBoardFrameLabeletc drawitselfHandlingEventEventObjectEventType EventTargetObject Time Location Key andOtherinformationabout Shift and Control Example 3 HandleEventCommonMethodhandleEvent OtherMethodSuchasaction ParameterEvent returnBoolean Iffalse transitEventtohigherComponent elseno MethodhandleEvent alwaysreturnsuper handleEventGetInputFocusComponentcanuseMethodrequestFocus togetinputfocusWhenComponentgetfocus methodgetFocus invokedWhenComponentlostfocus methodlostFocus invoked Example 4 HowtoaddComponentintoContainerComponentbedisplayedafteraddingintoContainerContainerisaComponenttoo soitcanbeaddedintootherContainerWindowistheTopContainer soitcouldnotbeaddedintootherContainerTheFunctionsofClassComponentDrawFunctions Suchaspaint update andrepaint CommonRulesaboutUsingComponent 1 HandleEventFunctions suchascommonmethodhandleEvent andothersFontControlandColorControl suchassetForeground getForeground setBackground andgetBackground DisplayandLocationControl ThoughmostcontrolfunctionsareinLayout ComponentusepreferredSize andminimumSize Handleimage OnlyuseClassCanvasandContainerHowtochangetheDisplayfeaturesandbehaviorsofComponentUsingCanvasClass CommonRulesaboutUsingComponent 2 ConstructFunctionpublicButton publicButton StringLabel MethodpublicStringgetLabel publicvoidsetLabel Stringlabel protectedStringparamString publicvoiddisable publicvoidenable publicsynchronizedvoidaddNotify Example ButtonExample java Button privatejava awt PanelgetContentsPane if ivjContentsPane null try ivjContentsPane newjava awt Panel ivjContentsPane setName ContentsPane ivjContentsPane setLayout null getContentsPane add getButton1 getButton1 getName getContentsPane add getButton2 getButton2 getName getContentsPane add getButton3 getButton3 getName catch java lang ThrowableivjExc handleException ivjExc returnivjContentsPane Example 1 privatejava awt ButtongetButton1 返回Button1特性值if ivjButton1 null try ivjButton1 newjava awt Button ivjButton1 setName Button1 ivjButton1 setBounds 15 99 129 20 ivjButton1 setLabel Disablemiddlebutton catch java lang ThrowableivjExc handleException ivjExc returnivjButton1 Example 2 IncludingtwoAWTClass CheckboxClassandCheckboxGroupClassCheckboxConstructFunctionpublicCheckbox publicCheck Stringlabel publicCheck Stringlabel CheckboxGroup booleanstate MembermethodsofCheckboxClasspublicStringgetLabel publicvoidsetLabel Stringlabel publicbooleangetState publicvoidsetState booleanstate publicCheckboxGroupgetCheckGroup publicvoidsetCheckboxGroup CheckboxGroupg CheckBox 1 protectedStringparamString publicsynchronizedvoidassNotify ConstuctFuntionofCheckboxGrouppublicCheckboxGrouP MemberMethodsofCheckboxGroupClasspublicCheckboxgetCurrent publicStringtoString publicsynchronizedvoidsetCurrent Checkboxt Example CheckboxExample java CheckBox 2 privatejava awt CheckboxgetCheckbox1 if ivjCheckbox1 null try ivjCheckbox1 newjava awt Checkbox ivjCheckbox1 setName Checkbox1 ivjCheckbox1 setBounds 37 45 84 20 ivjCheckbox1 setLabel Checkbox1 catch java lang ThrowableivjExc handleException ivjExc returnivjCheckbox1 Example 1 privatejava awt CheckboxgetCheckbox4 if ivjCheckbox4 null try ivjCheckbox4 newjava awt Checkbox ivjCheckbox4 setName Checkbox4 ivjCheckbox4 setCheckboxGroup ivjCheckboxGroup1 ivjCheckbox4 setBounds 203 45 84 20 ivjCheckbox4 setLabel Checkbox4 catch java lang ThrowableivjExc handleException ivjExc returnivjCheckbox4 Example 2 Anothernamepopuplist ConstructFunctionofChoicepublicChoice MembermethodsofChoicepublicintcountItems publicStringgetItem intindex publicsynchronizedvoidaddItem Stringitem publicStringgetSelectdItem publicintgetSelectdIndex publicsynchronizedvoidselect intpos publicvoidselect stringstr protectedStringparamString publicsynchronizedvoidaddNotify Example Choice publicclassMyChoiceextendsApplet privateChoicechoiceButton privateTextFieldt privateFontf publicvoidinit choiceButton newChoice t newTextField SampleText 16 t setEditable false choiceButton addItem TimesRoman choiceButton addItem Courier choiceButton addItem Helvetica f newFont choiceButton getItem 0 Font PLAIN 14 t setFont f add choiceButton add t Example 1 DialogboxdependsonFrame ConstructFunctionofClassDialogboxpublicDialog Frameparent booleanmodal publicDialog Frameparent Stringtitle booleanmodal MemberMethodsofClassDialogpublilcbooleanisModal publicStringgetTitle publicvoidsetTitle Stringtitle publicbooleanisResizeable publicvoidsetResizable booleanresizeavle publicStringparamString publicsynchronizedvidaddNotify Dialogbox Example DialogExample java Example ConstructFunctionpublicFrame publicFrame Stringtitle MemberMethodsofClassFramepublicStringgetTitle publicvoidsetTitle Stringtitle publicImagegetIconImage publicImagesetIconImage publicMenuBargetMenuBar publicsynchronizedvoidsetMenuBar menuBarmenubat publicsynchronizedvoidremove MenuComponentm publicbooleanisResizable publicvoidsetResiable booleanresizable Frame publicvoidsetCursor Imageimg protectedStringparamString publicsynchronizedvoidaddNotify Example FrameExample java Example ConstuctFuntionpublicLabel publicLabel Stringlabel publicLabel Stringlabel intalignment MemberMethodspublicintgetAlignment publicvoidsetAlignment intalignment publicStringgetText publicvoidsetText StringLabel protectedStringparamString publicsynchronizedvoidaddNotify Label ConstructFunctionpublicList publicList introws booleanmultipleSelections MemberFunctionpublicintcountItems publicStringgetItem intindex publicsynchronizedvoidaddItem Stringitem publicsynchronizedvoidclear publicsynchronizedvoiddelItem intposition publicsynchronizedvoiddelItem intstart intend publicsynchronizedintgetSelectedIndex publicsynchronizedint getSelectedIndexs List 1 publicsynchronizedStringgetSelectedItem publicsynchronizedString getSelectedItems publicsynchronizedvoidselect intindex publicsynchronizedvoiddeselect intindex publicsynchronizedbooleanisSelected intindex publicintgetRows publicbooleanallowsMultipleSelections publicvoidsetMultiSelection booleanv publicintgetVisibleIndex publicvoidmakeVisible itIndex publicDimensionpreferredSize introws publicDimensionminimumSize introws publicDimensionminimumSize protectedStringparamString List 2 publicsynchronizedvoidaddNotify publicsynchronizedvoidremoveNotify Example ListExample java List 3 AWTprovidesfourclassestosupportmenu MenuItemConstructFunction publicMenuItem Stringlabel MemberFunction publicStringgetLabel publicvoidsetLabel Stringlabel publicbooleanisEnabled publicvoidenable publicvoidenable booleancond publicvoiddisable protectedStringparamString publicsynchronizedvoidaddNotify Menu 1 CheckboxMenuItemConstructFunction publicCheckboxMenuItem Stringlabel MemberMethod publicbooleangetState publicvoidsetState booleant protectedStringparamString publicsynchronizedvoidaddNotify MenuConstructFunction publicMenu Stringlabel publicMenu Stringlabel booleantearOff Menu 2 MemberMethod publicbooleanisTearOff publicintcountItems publicMenuItemgetItem intindex publicvoidadd Stringlabel publicsynchronizedMenuItemadd MenuItemmi publicvoidaddSeparator publicsynchronizedvoidremove intindex publicsynchronizedvoidremove MenuComponentitem publicsynchronizedvoidaddNotify Menu 3 MenuBarConstructFunction publicMenuBar MemberMethod publicvoidremoveNotify publicMenugetHelpMenu publicsynchronizedvoidsetHelpMenu Menum publicsynchronizedMenuadd Menum publicsynchronizedvoidremove intindex publicsynchronizedvoidremove MenuComponentm publicintcountMenus publicMenugetM

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论