




已阅读5页,还剩18页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
建立窗口和菜单 第十五章 回顾 异常的定义异常的分类try catch和finally语句的用法throw throws子句的用法如何定义自己的异常Java异常机制的优点 目标 Component类常用方法建立窗口和菜单 Component类常用方法 Component类是所有组件的父类 其方法为所有组件公共功能的体现 所有组件都继承这些方法 因此 掌握Component类常用方法是很 值得 的 常用方法组件颜色设置组件字体设置组件大小与位置组件激活与可见性组件上的光标paint方法与repaint方法 组件颜色设置 颜色Color类Color intr intg intb r g b 0 255 定义了常用颜色 BLACK BLUE CYAN等组件颜色设置publicvoidsetBackground Colorc publicColorgetBackground publicvoidsetForeground Colorc publicColorgetForeground 组件字体设置 字体Font类publicFont Stringname intstyle intsize 常见样式定义 BOLD ITALIC等获取计算机上可用字体GraphicsEnvironmenten GraphicsEnvironment getLocalGraphicsEnvironment String fontNames en getAvailableFontFamilyNames 组件字体设置publicvoidsetFont Fontf publicFontgetFont 组件大小与位置2 1 常用方法publicvoidsetSize intwidth intheight publicDimensiongetSize publicvoidsetLocation intx inty publicPointgetLocation publicvoidsetBounds intx inty intwidth intheight publicRectanglegetBounds 组件大小与位置2 2 相关类PointpublicPoint intx inty publicdoublegetX publicdoublegetY RectanglepublicRectangle intx inty intwidth intheight publicdoublegetX publicdoublegetY publicdoublegetWidth publicdoublegetHeight publicbooleancontains intx inty publicbooleancontains Rectangler publicbooleanintersects Rectangler publicRectangleunion Rectangler 组件激活与可见性 激活publicvoidsetEnabled booleanb publicbooleanisEnabled 可见性publicvoidsetVisible booleanb publicbooleanisVisible 组件上的光标 设置光标publicvoidsetCursor Cursorcursor publicCursorgetCursor 光标CursorpublicCursor inttype publicstaticCursorgetPredefinedCursor inttype 常见光标类型定义 CROSSHAIR CURSOR HAND CURSOR等等 paint方法与repaint方法 publicvoidpaint Graphicsg 可以在子类中重写程序需要显示外观时自动调用publicvoidrepaint 先调用publicvoidupdate Graphicsg 清除以前画的内容再调用paint Graphicsg 方法重画 建立窗口和菜单 Java窗口窗口与屏幕菜单条 菜单 菜单项打印使用剪贴板 Java窗口 Frame类Container类的间接子类 是容器默认布局为BorderLayoutFrame不能添加到另一个容器中常用方法Frame Frame Strings setTitle Strings getTitle setResizable booleamb isResizable setSize intwidth intheight setBounds intx inty intwidth intheight setVisible booleamb validate dispose 窗口与屏幕 获取屏幕的大小Toolkittool getToolkit Dimensiondim tool getScreenSize 设置窗口大小和屏幕的大小相同setBounds 0 0 dim width dim height Dimension类publicDimension intwidth intheight publicdoublegetWidth publicdoublegetHeight 菜单条 菜单条MenuBar类用于容纳菜单Frame类的方法setMenuBar MenuBarbar 将菜单条设置到窗口 一个窗口只能添加一个菜单条 菜单 菜单Menu类用于容纳菜单项主要方法Menu Menu Strings add MenuItemitem add Strings getItem intn getItemCount insert MenuItemitem intn insert Strings intn remove intn removeAll 菜单项 菜单项MenuItem菜单项将被放在菜单里 单击某个菜单项可以发生ActionEvent事件是Menu的父类 所以Menu也可以作为菜单项 从而实现菜单的嵌套主要方法MenuItem MenuItem Strings setEnabled booleanb getLabel addActionListener ActionListenerlis 菜单技巧2 1 增加菜单分割线使用Menu类的addSeparator 方法复选框菜单项选择一个菜单项时出现一个对号标记产生ItemEvent事件CheckboxMenuItem类主要方法CheckboxMenuItem CheckboxMenuItem Stringlabel CheckboxMenuItem Stringlabel booleanstate booleangetState voidsetState booleanb 菜单技巧2 2 嵌入子菜单Menu是MenuItem的子类 因此菜单也可以作为菜单项添加到另一个菜单中 称这样的菜单项为子菜单 设置菜单项的快捷键MenuShotcut类MenuShortcut intkey key值可以取值KeyEvent VK A KeyEvent VK A 见P220表18 1一个菜单项使用setShortcut MenuShotcutk 来设置快捷键同时按下ctrl和快捷键来选择菜单设置快捷键后菜单项后自动添加类似CTRL M的显示内容 打印 组件方法 publicvoidprint Graphicsg 不包括子组件publicvoidprintAll Graphicsg 基本过程 PrintJobp getToolkit getPrintJob this PrintTest null Graphicsg p getGraphics g translate 120 200 指定打印位置con printAll g 剪贴板 java awt datatransfer ClipBoard复制到剪贴板ClipBoardclip getToolkit getSystemClipboard StringSelectiontext newStringSelection s clip setContents text 从剪贴板获取Transferablects clip getContents null try String
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年规划投资专业考试题库及答案
- 2025年广西中小学教师招聘考试教育综合知识试题及答案解析
- 化验员基础考试题及答案
- 芜职美育课考试题及答案
- 东北三省二模考试试题及答案
- 《科目一》机动车驾驶员考试试题与参考答案(2025年)
- 人力六大模块笔试题及答案
- 心力衰竭中医护理试题及答案2025年解析
- 渠道经理笔试题库及答案
- 2025年一建建筑工程真题答案21
- 急诊科岗位职责
- 中国服用过兴奋剂运动员名单 兴奋剂真的是毒品吗
- 小学英语语法时态讲解与归纳
- 《生存与修炼》熊厚音讲《道德经》教学文案
- 淘宝新店运营计划书文献
- 产教融合校企合作[可修改版ppt]课件
- ICH Q6B 生物技术产品和生物制品的检验方法和可接受标准
- 12贮水花盆案例总结-2015天津中心修改43
- (精心整理)六方最密堆积空间利用率和密度的计算
- 练习太极拳的三个阶段
- 华为供应商质量管理体系考察报告(全)
评论
0/150
提交评论