




已阅读5页,还剩114页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
图形用户界面设计GraphicalUserInterface GUI AWT AbstractWindowToolKit 抽象窗口工具包来源 Java1 0跨平台性能差 组件不够丰富 存在很多缺陷Swing来源 Java1 2前身 Netscape创建IFC的GUI类经SUN和网景的共同努力 发展成SwingAWT的组件在Swing都有相应实现 J 原名 AWT Swing BuildingGUIswithAWT AbstractWindowToolkit java awt包提供了基本的java程序的GUI设计工具 ComponentContainerLayoutManager Java lang Object AWTEvent Font Component Graphics MenuComponent 各种布局管理器类 Container Panel Applet Window Frame 分层界面 最底层容器 Applet Dialog Frame 面板 组件2 组件1 Component 组件 Java的图形用户界面的最基本组成部分是组件 组件是一个可以以图形化的方式显示在屏幕上并能与用户进行交互的对象 例如一个按钮 一个标签等 组件不能独立地显示出来 必须将组件放在一定的容器中才可以显示出来 Container 容器 容器 Container 实际上是Component的子类 因此容器本身也是一个组件 具有组件的所有性质 另外还具有容纳其它组件和容器的功能 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 Frame框架 Frame是Window的一个子类 它是带有标题和缩放角的窗口 继承于Java awt Container 因此 可以用add 方式来给框架添加组件缺省布局管理器就是BorderLayout 可以用setLayout 方式来改变布局管理器构造函数Frame String 用由String规定的标题来创建一个新的不可见的框架对象 importjava awt publicclassMyFrameextendsFrame publicstaticvoidmain 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 Panel提供空间来捆绑GUI组件 包括其它面板 每个面板都可以有它自己的布管理程序 面板对象必须添加到窗口或框架对象上 才了能看得见 importjava awt publicclassFrameWithPanelextendsFrame publicFrameWithPanel 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布局管理器 容器中组件的布局由布局管理器控制FlowLayout 流式布局管理器 BorderLayout 边框布局管理器 GridLayout 网格布局管理器 CardLayout 卡片布局管理器 GridBagLayout 网袋布局管理器 容器的默认布局管理器 importjava awt publicclassExGui privateFramef privateButtonb1 privateButtonb2 publicstaticvoidmain Stringargs ExGuithat newExGui that go publicvoidgo f newFrame GUIexample f setLayout newFlowLayout b1 newButton PressMe b2 newButton Don tPressMe f add b1 f add b2 f pack 设定框架大小 能恰好密封它所包含的组件f setVisible true 运行结果 FlowLayout 对组件逐行地定位 不限制它所管理的组件的大小 允许它们有自己的最佳大小 Flow布局构造程序参数允许将组件左对齐或右对齐 缺省为居中 如果想在组件之间创建一个更大的最小间隔 可以规定一个界限 Panel Applet的缺省布局管理器 setLayout newFlowLayout FlowLayout RIGHT 20 40 setLayout newFlowLayout FlowLayout LEFT setLayout newFlowLayout importjava awt publicclassmyButtons publicstaticvoidmain 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 构造 setLayout newBorderLayout 在组件之间没有间隙setLayout newBorderLayout inthgap intvgap 组件之间有由hgap和vgap规定的间隙 importjava awt publicclassbuttonDir publicstaticvoidmain 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 publicclassButtonGrid publicstaticvoidmain 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 publicclassThreePagesimplementsMousListener CardLayoutlayout 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 publicclassExGui3 privateFramef 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事件处理机制 WhatisanEvent Event事件 就是发生在用户界面上的用户交互行为所产生的一种效果 EventSource产生事件的对象 Eventhandler接收事件对象并对其进行处理的方法 事件处理模型 Hierachicalmodel JDK1 0 事件传递机制 Delegationmodel JDK1 1 1 2 授权处理机制 事件源可以将事件的处理委托给一个或多个事件处理器 DelegationModel 组件 事件源 事件处理器 外部作用 事件对象 生成事件对象 把事件对象传入事件处理器 事件监听器注册 1 3 2 4 importjava awt importjava awt event publicclassTestButton publicstaticvoidmain Stringargs Framef newFrame Test Buttonb newButton PressMe b addActionListener newButtonHandler f setLayout newFlowLayout f add b f setSize 200 100 f setVisible true classButtonHandlerimplementsActionListener publicvoidactionPerformed ActionEvente System out println Actionoccurred 使用JDK1 1授权处理模型进行事件处理的一般方法 对于某种类型的事件XXXEvent 要想接收并处理这类事件 必须创建相应的事件监听器类 该类需要实现此种类型事件的接口XXListener 在事件监听器类中编写要处理的具体事件的方法在组件上注册相应于该类事件的监听器 使用addXXXListener XXXListener 方法来注册事件监听器 事件对象 事件对象 java util EventObject类EventObject类是所有事件对象的基础类 所有的事件类都是由它派生出来的 publicclassEventObjectimplementsjava io Serializable protectedtransientObjectsource 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 文本事件 文本对象改变 事件监听接口 对于每类事件 都有一个事件监听接口 这个接口定义一个或多个方法 当发生特定的事件时 就会调用这些方法 表9 1列出了这些 事件 类型 并给出了每个类型对应的接口名称 以及所要求定义的方法 这些方法的名称是易于记忆的 名称表示了会引起这个方法被调用的源或条件 注册和注销监听器 注册监听器 publicvoidadd listener 注销监听器 publicvoidremove listener 注册和注销监听器 AWT的组件类中提供注册和注销监听器的方法 例如Button类 查API publicclassButtonextendsComponent publicsynchronizedvoidaddActionListener ActionListenerl publicsynchronizedvoidremoveActionListener ActionListenerl AWT事件及其相应的监听器接口 ActionEvent行动事件ActionListenerActionPerformed ActionEvent 例 H52 ItemEvent选项事件ItemListenerItemStateChanged ItemEvent 7 1鼠标事件 7 1 1MouseListener接口鼠标监听接口 MouseListener 用于监听发生在一个GUI构件上的鼠标事件 包括鼠标的按下 释放 单击 进入和退出 Java同时也提供了与该接口相对应的称作事件剪裁器的抽象类MouseAdapter 在一个实现了MouseListener接口或继承了MouseAdapter类的类中可以定义事件的处理方法 而该类的一个对象则应该用addMouseListener 方法注册到发生鼠标事件的构件上 MouseListener接口包含的方法如下 1 publicvoidmouseClicked MouseEvent节e 当在一个构件上单击鼠标时被调用 2 publicvoidmousePressed MouseEvente 当在一个构件上按下鼠标按钮时被调用 3 publicvoidmouseReleased MouseEvente 当在一个构件上释放鼠标按钮时被调用 4 publicvoidmouseEntered MouseEvente 当鼠标指针进入构件时被调用 5 publicvoidmouseExited MouseEvente 当鼠标指针退出构件时被调用 7 1 2MouseMoutionListener接口鼠标移动监听接口 MouseMoutionListener 用于监听发生在一个GUI构件上的鼠标移动事件 包括鼠标的移动和拖动 其使用方法与MouseListener接口相同 它所包含的方法如下 1 publicvoidmouseDragged MouseEvente 当在一个构件上按下鼠标按钮并且拖动鼠标时该方法被调用 鼠标拖动事件持续到鼠标按钮被释放时为止 而不管鼠标的位置是否超出了原来构件的边界 2 publicvoidmouseMoved MouseEvente 当鼠标指针移动时该方法被调用 注意此时鼠标的按钮并没有被按下 在MouseListener接口和MouseMotionListener接口中的所有方法中都有一个参数 它是鼠标事件类MouseEvent的一个对象 当鼠标事件发生时 系统自动生成一个该类的对象 在鼠标事件的处理方法中 经常需在调用该类的方法来获得关于事件的一些信息 MouseEvent类的常用方法如下 1 publicintgetX 返回当前鼠标指针位置的x坐标值 2 publicintgetY 返回当前鼠标指针位置的y坐标值 3 publicintgetClickCount 返回事件中鼠标的点击次数 4 publicStringparamString 返回一个标识该事件的字符串 例子 Event12 7 java KeyEvent键盘输入KeyListenerKeyPressed KeyEvent KeyReleased KeyEvent KeyTyped KeyEvent 例子 Event12 6 java FocusEvent组件收到或失去焦点FocusListenerfocusGained FocusEvent focusLost focusEvent 例子 Event12 5 java 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 例子 Event12 4 java ContainerEvent容器事件 容器中增加删除了组件ContainerListenercomponentAdded containerEvent componentRemoved containerEvent TextEvent文本事件 文本字段或文本区发生改变TextListenertextValueChanged TextEvent 例12 12importjava awt importjava awt event publicclassTwoListenimplementsMouseMotionListener MouseListener WindowListener privateFramef 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 publicclassMouseClickHandlerextendsMouseAdaper publicvoidmouseClicked 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 Checkbox组件提供一种简单的 开 关 输入设备 它旁边有一个文本标签 选取或不选取 取消 一个复选框的事件将被送往ItemListener接口ItemEventItemListener获取状态 booleanCheckbox getState intItemEvent getStateChange 获取当前状态 ItemEvent SELECTED ItemEvent DESELECTED获取标签 StringCheckbox getLabel ObjectItemEvent getItem 获得被操作复选框的标签字符串 classHandlerimplementsItemListener publicvoiditemStateChanged ItemEventev Stringstate deselected if ev getStateChange ItemEvent SELECTED state selected System out println ev getItem state 复选框组 单选框 复选框组提供了将多个复选框作为互斥的一个集合的方法 这个集合中只有一个复选框的值是true publicCheckboxgetSelectedCheckbox 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 获取选择项 publicStringgetSelectedItem 用ItemListener接口来进行监听 画布 Canvas 画布提供了一个空白 背景色 的空间 画布的空间可以用来绘图 显示文本 接收键盘或鼠标的输入 通常 画布用来提供一个一般的绘图空间或者为客户组件提供工作区域 如创建一个自定义组件 一个应用程序必须继承Canvas类才能获得有用的功能 如果想在画布上完成一些图形处理 则Canvas类中的paint 方法必须被重写 Canvas组件监听各种鼠标 键盘事件 当在Canvas组件中输入字符时 必须先调用requestFocus 方法 例子 例12 14importjava awt importjava awt event importjava util publicclassMyCanvasimplementsKeyListener MouseListener Canvasc Strings publicstaticvoidmain Stringargs Framef newFrame Canvas MyCanvasmc newMyCanvas mc c newCanvas f add Center mc c f setSize 150 150 mc c addMouseListener 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 方法设置为只读属性 publicvoidsetText Stringt publicStringgetText TextField TextFieldtf1 tf2 tf3 tf4 tf1 newTextField tf2 newTextField 20 显示区域为20列tf3 newTextField Hello 按文本区域显示tf4 newTextField Hello 30 初始文本为Hello 显示区域为30列 文本区 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 两
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 农业物联网在精准种植中的应用2025年数据驱动决策分析报告
- 2025年生态修复工程生物多样性保护与生物入侵防控报告
- XX康复治疗器械集团2022合规监督报告:政策导向下的行业健康发展
- 2025年家用衡器项目提案报告模板
- 民宿节水控能系统2025年投资价值评估报告
- 安职辅导员考试题及答案
- 幼儿园保育知识试题及答案
- 宫颈癌筛查培训试题及答案
- 2025年汽车行业供应链风险管理与企业风险管理策略实施报告001
- 2025年工业互联网平台网络切片技术在工业互联网平台生态构建与技术创新中的应用实践报告
- 人教版初中全部英语单词表(含音标)
- Magic Tree House 神奇树屋词汇大全
- 《心系国防 有你有我》国防教育主题班会课件
- 普通外科临床路径(2019年版)
- 教师工作法律风险防范省公开课金奖全国赛课一等奖微课获奖课件
- A类《职业能力倾向测验》2024年事业单位考试湘西土家族苗族自治州泸溪县统考试题含解析
- 渭南万泉330千伏变电站-雷家洼110千伏线路工程环境影响报告
- 新编文学理论课件
- 小学数学北师大版三年级下册递等式计算练习300题及答案
- 企业后勤安全管理培训课件
- 驾驶员安全教育三超一疲劳驾驶案例培训课件
评论
0/150
提交评论