建立窗口和菜单_第1页
建立窗口和菜单_第2页
建立窗口和菜单_第3页
建立窗口和菜单_第4页
建立窗口和菜单_第5页
已阅读5页,还剩18页未读 继续免费阅读

下载本文档

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

文档简介

1、建立窗口和菜单第十五章回顾异常的定义异常的分类 try、catch 和 finally 语句的用法throw、throws子句的用法如何定义自己的异常Java异常机制的优点2目标Component 类常用方法建立窗口和菜单3Component 类常用方法Component 类是所有组件的父类,其方法为所有组件公共功能的体现。所有组件都继承这些方法。因此,掌握Component 类常用方法是很“值得”的。常用方法组件颜色设置组件字体设置组件大小与位置组件激活与可见性组件上的光标paint方法与repaint方法4组件颜色设置颜色Color 类Color(intr, intg, intb) / r

2、,g,b-0,255定义了常用颜色:BLACK 、BLUE 、CYAN等组件颜色设置public void setBackground(Colorc) public Color getBackground() public void setForeground(Colorc) public Color getForeground() 5组件字体设置字体Font 类public Font(Stringname, intstyle, intsize) 常见样式定义:BOLD、ITALIC 等获取计算机上可用字体GraphicsEnvironment en = GraphicsEnvironment

3、. getLocalGraphicsEnvironment();String fontNames = en.getAvailableFontFamilyNames() ;组件字体设置public void setFont(Fontf) public Font getFont() 6组件大小与位置 2-1常用方法public void setSize(intwidth, intheight) public Dimension getSize() public void setLocation(intx, inty) public Point getLocation ()public void s

4、etBounds(intx, inty, intwidth, intheight) public Rectangle getBounds() 7组件大小与位置 2-2相关类Pointpublic Point(intx, inty) public double getX() public double getY() Rectanglepublic Rectangle(intx, inty, intwidth, intheight) public double getX() public double getY() public double getWidth() public double ge

5、tHeight() public boolean contains(intx, inty) public boolean contains(Rectangler) public boolean intersects(Rectangler) public Rectangle union(Rectangler) 8组件激活与可见性激活public void setEnabled(booleanb) public boolean isEnabled() 可见性public void setVisible(booleanb) public boolean isVisible() 9组件上的光标设置光标

6、public void setCursor(Cursorcursor) public Cursor getCursor() 光标Cursorpublic Cursor(inttype) public static Cursor getPredefinedCursor(inttype) 常见光标类型定义:CROSSHAIR_CURSOR、HAND_CURSOR 等等10paint方法与repaint方法public void paint(Graphicsg)可以在子类中重写 程序需要显示外观时自动调用public void repaint() 先调用 public void update(Gra

7、phicsg) 清除以前画的内容再调用paint(Graphicsg)方法重画11建立窗口和菜单Java 窗口窗口与屏幕菜单条、菜单、菜单项打印使用剪贴板12Java窗口Frame类Container类的间接子类,是容器默认布局为BorderLayout Frame不能添加到另一个容器中常用方法Frame();Frame(String s);setTitle(String s); getTitle(); setResizable(booleam b); isResizable(); setSize(int width,int height); setBounds(int x,int y,int

8、 width,int height); setVisible(booleam b); validate();dispose();13窗口与屏幕/ 获取屏幕的大小Toolkit tool= getToolkit() ; Dimension dim=tool.getScreenSize();/ 设置窗口大小和屏幕的大小相同setBounds(0,0,dim.width,dim.height);Dimension 类public Dimension(intwidth, intheight)public double getWidth() public double getHeight() 14菜单条

9、菜单条MenuBar 类用于容纳菜单Frame类的方法setMenuBar(MenuBar bar);将菜单条设置到窗口。一个窗口只能添加一个菜单条。15菜单菜单Menu 类用于容纳菜单项主要方法Menu(); Menu(String s); add(MenuItem item); add(String s); getItem(int n); getItemCount(); insert(MenuItem item,int n); insert(String s,int n); remove(int n); removeAll();16菜单项菜单项MenuItem菜单项将被放在菜单里,单击某个

10、菜单项可以发生ActionEvent事件是Menu的父类。所以Menu也可以作为菜单项,从而实现菜单的嵌套主要方法MenuItem(); MenuItem(String s); setEnabled(boolean b); getLabel(); addActionListener(ActionListener lis);17菜单技巧 2-1增加菜单分割线使用Menu类的 addSeparator(); 方法复选框菜单项选择一个菜单项时出现一个对号标记产生 ItemEvent 事件CheckboxMenuItem 类主要方法CheckboxMenuItem() CheckboxMenuItem

11、(Stringlabel) CheckboxMenuItem(Stringlabel, booleanstate) boolean getState() void setState(booleanb)18菜单技巧 2-2嵌入子菜单Menu是MenuItem的子类,因此菜单也可以作为菜单项添加到另一个菜单中。称这样的菜单项为子菜单。设置菜单项的快捷键MenuShotcut 类MenuShortcut(intkey)key值可以取值KeyEvent.VK_A KeyEvent.VK_A。见P220表18.1一个菜单项使用setShortcut(MenuShotcut k) 来设置快捷键同时按下ct

12、rl和快捷键来选择菜单设置快捷键后菜单项后自动添加类似CTRL+M的显示内容19打印组件方法:public void print(Graphicsg) / 不包括子组件public void printAll(Graphicsg)基本过程:PrintJob p=getToolkit().getPrintJob(this,”PrintTest”,null);Graphicsg=p .getGraphics();g.translate(120, 200) ;/ 指定打印位置con. printAll(g);20剪贴板java.awt.datatransfer. ClipBoard复制到剪贴板ClipBoard clip=getToolkit().getSystemClipboard();StringSelection text = new StringSelection(s);clip.setContents(text);从剪贴板获取Transferable cts = clip.getContents(null);try

温馨提示

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

评论

0/150

提交评论