1 JavaGUI java awt Flow Layout Border Layout


1. 布局设计 • 布局概述 – Java的GUI组件在容器中放置,它们的位置由容器的布局管理器来 安排。后台 作的布局管理器能够将组件放置在正确的位置。 – java. awt • Flow. Layout • Border. Layout • Card. Layout • Grid. Bag. Layout – java. swing • Box. Layout • Scroll. Panel. Layout • Viewport. Layout • Over. Layout School of Computer Science and Technology


– 示例 School of Computer Science and Technology



– 实例 School of Computer Science and Technology


– 实例 School of Computer Science and Technology

–续 School of Computer Science and Technology

School of Computer Science and Technology


– 实例 School of Computer Science and Technology

–续 School of Computer Science and Technology



– 实例 School of Computer Science and Technology


– 一般方法 get. Layout. Alignment. X(Container con) 获得容器沿X轴的对齐方式:行排列 get. Layout. Alignment. Y(Container con) 获得容器沿Y轴的对齐方式:行排列 set. Layout. Alignment. X(Container con) 设置容器沿X轴的对齐方式:行排列 set. Layout. Alignment. Y(Container con) 设置容器沿Y轴的对齐方式:行排列 Layout. Container(Container con) 设置con窗口的布局方式为Box. Layout布局 Box. create. Horizontal. Box() 构建一个行型盒式布局的Box容器组件 Box. createvertical. Box() 构建一个列型盒式布局的Box容器组件 Box. create. Horizontal. Struct(int width) 构 建 一 个 不 可 见 的 宽 度 为 width的 水 平 Struct类 型 支 撑对象来控制组件之间的距离 Box. createvertical. Struct(int height) 构 建 一 个 不 可 见 的 宽 度 为 width的 水 平 Struct类 型 支 撑对象来控制组件之间的距离 Box. create. Horizontal. Glue() 构建一个水平方向的Glue组件 Box. createvertical. Glue() 构建一个垂直方向的Glue组件 School of Computer Science and Technology

– 实例 School of Computer Science and Technology




– 构造方法与一般方法: JMenu. Item类 JMenu. Item(String s) 创建名为s的菜单项 JMenu. Item(String s, Icon i) 创建名为s、图标为i的菜单项 JRadio. Button. Menu. Item(String s) 创建标题为s带单选按钮的菜单项 JCheck. Button. Menu. Item(String s) 创建标题为s带复选框的菜单项 set. Enabled(boolean b) 设置当前菜单项是否可被选择 get. Label() 获取菜单项名字 get. Key. Stroke(char key. Char) 返回一个字符型的Key. Stroke对象 set. Accelerator(Key. Stroke k) 为菜单项设置快捷键 Menu. Item(String s) 创建名为s的菜单项 get. Action. Command() 返回菜单项对象所产生的动作事件 关联的命令名称 disable() 设置当前菜单项不可用 JCheck. Button. Menu. Item(String s) 创建标题为s带复选框的菜单项 get. State() 获取复选框的状态 set. State(boolean b) School of Computer Science and Technology 设置复选框的状态



–续 School of Computer Science and Technology

–续 School of Computer Science and Technology


– swing中的包 javax. swing组件和实用 具 javax. swing. border swing轻组件的边框 javax. swing. colorchooser JColor. Choose的支持类 /接口 JColor. Choose的支持类/ javax. swing. event 事件和监听器类 javax. swing. plaf 抽象类,定义UI 代理的行为 抽象类,定义UI代理的行为 javax. swing. plaf. basic 实现所有标准界面样式公共功能的基类 javax. swing. plaf. metal 用户界面代理类,实现Metal 界面样式 用户界面代理类,实现Metal界面样式 javax. swing. table JTable组件的支持类 javax. swing. text 支持文档的显示和编辑 javax. swing. text. html 支持显示和编辑html 文件 支持显示和编辑html文件 javax. swing. text. html. parser html文件的分析器类 javax. swing. text. rtf 支持显示和编辑RTF 文件 支持显示和编辑RTF文件 javax. swing. tree JTree组件的支持类 javax. swing. undo 支持取消操作 com. sun. javax. swing. plaf. motif 用户界面代理类,它们实现Motif 界面样式 用户界面代理类,它们实现Motif界面样式 com. sun. javax. swing. plaf. windows 用户界面代理类,它们实现Windows 界面样式 用户界面代理类,它们实现Windows界面样式 com. sun. javax. swing. plaf. gtk 用户界面代理类,它们实现GTK 界面样式 用户界面代理类,它们实现GTK界面样式 School of Computer Science and Technology

– swing包中的类层次结构 School of Computer Science and Technology

– swing组件的分类 • 按照功能划分 – 基本控件:实现人机交互的基本组件。包括JButton、Jcombo. Box、 JList、JMenu、JSlider、JText. Field、JTex. Atrea、JPassword. Field; – 顶层容器:包括JFrame、JApplet、JDialog、JWindow; – 中间容器:涵盖JPanel、JScroll. Pane、JSplit. Pane、JLayered. Pane、 JTool. Bar; – 特殊容器:在GUI上起特殊作用的中间层,如JInternal. Frame(内部窗 件)、JRoot. Pane。 • 按swing组件对信息的可编辑与否划分 – 不可编辑信息的显示:如JLabel、JProgress. Bar、Tool. Tip; – 可 编 辑 信 息 的 显 示 : 如 JText. Field、 JText. Area、 JPassword. Field、 JColor. Chooser、JFile. Chooser、JTable。 School of Computer Science and Technology

• 组件常用方法 – 组件的颜色 • 组件背景色的设置方法: public void set. Background(Color c) • 组件前景色的设置方法: public void set. Foreground(Color c) • 组件背景色的获取方法: public Color get. Background() • 组件前景色的获取方法: public Color get. Foreground() • 上述方法中都涉及Color类,Color类是java. awt包中的类,该类窗 件的对象称为颜色对象,Color类的构造方法如下: public Color(int red, int green, int blue) – 其中red、green、blue的取值在 0~255之间。 School of Computer Science and Technology

– 组件的字体 • 组件字体的设置方法: public void set. Font(Font f) • 组件字体的获取方法: public void get. Font() • 上述方法用到了java. awt包中的Font类,该类的构造方法如下: public Font(String name, int style, int size) – name:字体名字,如果系统不支持字体的名字,将取默认名字创建字 体对象; – style:字体样式,有效取值为Font. BOLD、Font. PLAIN、Font. ITALIC、 Font. ROMAN_BASELINE、Font. CENTER_BASELINE、Font. HANGING_BASELINE、 Font. TRUETYPE_FONE; – size:字体大小,单位是磅。 • 获取系统中可以提供的字体名字,可以通过Graphics. Environment对 象调用get. Available. Fone. Family. Names()方法将可用字体名称存放到字 符串数组中。 School of Computer Science and Technology


– 组件的大小与位置 • public void set. Size(int width, int height) 设置组件的大小,width指定组件的宽度,height指定组件的高度。 • public void set. Location(int x, int y) 设置组件在容器中的位置。 • public Dimension get. Size() 返回一个Dimension对象下组件的宽度和高度值。 • public Point get. Location() 返回一个含有成员变量x和y的Point对象的引用,x和y就是组件 在容器中的坐标。 • public void set. Bounds(int x, int y, int width, int height) 设置组件在容器中的位置和组件的大小。 public Rectangle get. Bounds() 返回一个含有成员变量x、y、width和height的Rectangle对象的 引用。 School of Computer Science and Technology


–续 School of Computer Science and Technology

–续 School of Computer Science and Technology

• JFrame窗体 – JFrame是 GUI中 的 一 个 框 架 容 器 。 javax. swing包 中 的 JFrame是 java. awt包中Frame类的子类。 – 构造方法: JFrame() 创建一个不含标题的新JFrame类窗体对象 JFrame(String s) 创建一个标题为s的新JFrame类窗体对象 – 一般方法: set. Size(int width, int height) 设置JFrame对象的宽和高 set. Background(Color c) 设置JFrame对象的背景色 set. Visible(boolean b) 设置JFrame对象的可见性 set. Resizable(boolean b) 设置JFrame对象是否可以改变大小 JMenu. Bar get. Menu. Bar() 获得窗口中的菜单栏组件 remove(Component c) 将窗口中指定的组件移除 set. Default. Close. Operation(int op) 单击窗体关闭按钮时的默认操作 set. Bounds(int x, int y, int width, int height) Science and Technology School of Computer 设置窗口的坐标和大小





–续 School of Computer Science and Technology

–续 School of Computer Science and Technology

• 按钮组件 – Abstract. Button类派生出两个组件: • JButton组件:swing按钮; • JToggle. Button组 件 : 单 选 按 钮 (JRadio. Button)和 复 选 框 (JCheck. Button)的父类。 – JButton类的构造方法: JButton() 构造无标题按钮对象 JButton(String s) 构造标题为s的按钮对象 JButton(Icon icon) 构造图标为icon的按钮对象 JButton(String icon) s, Icon 构造标题为s、图标为icon的按钮对象 – JButton类的一般方法: add. Action. Listener(Action. Listener e) 给按钮对象增加动作监听器,即注册 remove. Action. Listener(Action. Listener e) 移去按钮上的监听器 set. Text(String s) 设置文字 School of Computer Science and Technology

get. Text() 获取按钮标签文字 set. Icon(Icon icon) 设置按钮图标 get. Icon() 获取按钮图标 set. Enabled(boolean b) 设置当前按钮的可用性 in. Selected() 获取按钮状态 set. Horizontal. Text. Position(int text. Position) 设置按钮名相对水平位置(LEFT/CENTER/RIGHT) –set. Vertical. Text. Position(int 实例 text. Position) School of Computer Science and Technology 设置按钮名相对垂直位置(TOP/CENTER/BOTTOM)

–续 School of Computer Science and Technology

–续 School of Computer Science and Technology

– 复选框与单选按钮 • JCheck. Button – 构造方法 JCheck. Box() 创建一个无标签复选框对象 JCheck. Box(String s) 创建一个标签为s的复选框对象 JCheck. Box(String s, Icon icon) 创建一个标签为s、图标为icon的复选框对象 JCheck. Box(String s, boolean b) 创建一个标签为s、状态为b的复选框对象 – 一般方法 void add. Item. Listener(Item. Listener i) 注册Item. Listener监听器 public boolean is. Selected() 返回复选框状态 void set. Selected(boolean • public JRadio. Button b) 设置复选框状态 – Java提供Button. Group类为单选按钮类提供多选一功能,当创建了若干 个单选按钮后,需要将这些按钮添加到Button. Group的实例中去。 – JRadio. Button构造方法与一般方法与复选框类似,略。 School of Computer Science and Technology


–续 School of Computer Science and Technology

• 下拉列表组件 – 下拉列表(JCombo. Box) • 构造方法 JCombo. Box() 创建空下拉列表框 JCombo. Box(Vector vect) 使用向量表创建一个下拉列表对象 JCombo. Box(Combo. Box. Model model) 从已有的model获得选项创建下拉列表对象 JCombo. Box(Object[ ] items) 使用数组创建下拉列表对象 • 一般方法 void add. Action. Listener(Action. Listener e) 注册事件对象给Action. Listener void add. Item. Listener(Item. Listener e) 注册事件对象给Item. Listener void add. Item(Object obj) 添加选项 Object get. Item. At(int index) 获取指定位置的选项 int get. Item. Count() 获得当前选项的数目 public Object get. Selected. Item() 获得被选项 School of Computer Science and Technology

void set. Editable(boolean b) 设置是否可以编辑 int get. Selected. Index() 获取被选中项的索引 void remove. All. Items() 移除全部选项 void remove. Item. At(int index) 移除索引为index处的选项 void remove(Component comp) 移除指定的组件 • 实例 School of Computer Science and Technology

School of Computer Science and Technology
![– 列表(JList) • 构造方法 JList() 创建一个空列表对象 JList(Vector vect) 使用向量表创建一个列表对象 JList(Object[ ] items) 使用数组创建一个列表对象 • – 列表(JList) • 构造方法 JList() 创建一个空列表对象 JList(Vector vect) 使用向量表创建一个列表对象 JList(Object[ ] items) 使用数组创建一个列表对象 •](http://slidetodoc.com/presentation_image_h2/a9346e4fce85e8e57c44f85a74a195a7/image-57.jpg)
– 列表(JList) • 构造方法 JList() 创建一个空列表对象 JList(Vector vect) 使用向量表创建一个列表对象 JList(Object[ ] items) 使用数组创建一个列表对象 • 一般方法 int [ ] get. Selected. Indices() 获得被选取的多个项 int get. Selected. Index(int i) 获得选取的单个项 void set. Visible. Row. Count(int num) 设置可见的列表选项 void add. List. Selection. Listener(List. Selection. Listener e) 注册监听器 int get. Visible. Row. Count() 获得可见的列表选项值 • 实例 School of Computer Science and Technology

School of Computer Science and Technology

School of Computer Science and Technology



School of Computer Science and Technology

School of Computer Science and Technology

School of Computer Science and Technology

School of Computer Science and Technology

• 进度条组件 – JProgress. Bar类通过用颜色动态填充显示某任务完成的百分比。 – 构造方法 JProgress. Bar() 创建一个水平进度条,最小为 0,最大为 100 JProgress. Bar(int min, int max) 创建一个水平进度条,最小为min,最大为max JProgress. Bar(int orient, int min, int max) 创建一个进度条,最小为min,最大为max,orient 为HORIZONTAL则为水平条,VERTICAL则为垂直条 – 一般方法 add. Change. Listener(Change. Listener e) 进度条注册监听器 get. Maximum 获得进度条最大值 get. Minimum 获得进度条最小值 set. Maximum(int n) 设置进度条最大值 set. Minimum(int n) 设置进度条最小值 get. Value 获得进度条当前值 set. Value(int n) 设置进度条当前值 School of Computer Science and Technology


School of Computer Science and Technology

School of Computer Science and Technology




School of Computer Science and Technology

School of Computer Science and Technology


School of Computer Science and Technology


JSplit. Pane(int s, Component c 1, Component c 2) 创建一个具有指定方向和指定组件的新JSplit. Pane • 一般方法: set. Divider. Location(double position) 设置分隔条的位置 – JLayered. Pane分层窗格 • 可用来处理组件经常重叠的情况,JLayered. Pane可将容器分为五层。 • 构造方法 JLayered. Pane() 创建新的JLayered. Pane • 一般方法 set. Layer(Component c, int layer) 设置指定组件的layer属性,使之成为该层中最底部的 组件 get. Layer(Component c) 返回指定Component的layer属性 add(JComponent c. int layer) 添加组件并设置所在的层 School of Computer Science and Technology


– JTool. Bar. HORIZONTAL – JTool. Bar. VERTICAL • 构造方法: JTool. Bar() 创建新的 具栏;默认的方向为HORIZONTAL • 一般方法: get. Component. Index(Component c) 返回指定组件的索引 get. Component. At. Index(int i) 返回指定索引位置的组件 • 实例 School of Computer Science and Technology

School of Computer Science and Technology


– 一般方法 add(JInternal. Frame c, int layer) 添加内部窗体,指定内部窗体所在的层次 set. Layer(JInternal. Frame c, int layer) 设置内部窗体所在的层次 get. Layer(JInternal. Frame c) 获取内部窗体所在的层次 get. All. Frames() 返回桌面中当前显示的所有JInternal. Frames get. All. Frames. In. Layer(int layer) 返回桌面指定层中当前所显示的所有JInternal. Frames get. Selected. Frame() 返 回 此 JDesktop. Pane中 当 前 活 动 的 JInternal. Frame, 如 果 当前没有活动的JInternal. Frame ,则返回null 当前没有活动的JInternal. Frame,则返回 add. Internal. Frame. Listener(Internal. Frame. Listen er listener) 添加指定的侦听器,以从此内部窗体接收内部窗体事件 set. Maximizable(boolean b) 设 置 maximizable属 性 , 该 属 性 确 定 是 否 可 以 通 过 某 个 用户操作最大化此JInternal. Frame set. Closable(boolean b) 设置是否可以通过某个用户操作关闭此JInternal. Frame set. Iconifiable(boolean b) 设置iconable属性,对于能够使JInternal. Frame变成一 个图标的用户而言,该属性必须为true set. Resizable(boolean b) 设置是否可以通过某个用户操作调整JInternal. Frame的 大小 set. Title(String title) 设置JInternal. Frame 的标题 设置JInternal. Frame的标题 set. Visible() () set. Visible. Science School of Computer and Technology 设置内部窗体可见性



• 静态方法 show. Input. Dialog(Object msg) 显示请求用户输入的问题消息对话框 show. Input. Dialog(Component parent. C, Object msg, String str, int msgtype) 显 示 请 求 用 户 输 入 内 容 的 对 话 框 , 它 以 parent. C 为 父 级 , 该 对 话 框 的 标 题 为 str, 消 息 类 型 为 msg. Type show. Message. Dialog(Component Object msg) parent. C, 调出信息为msg消息对话框 show. Message. Dialog(Component parent. C, Object msg, String str, int msgtype) 调出对话框,它显示使用由msg. Type参数确定 的默认图标的message Show. Confirm. Dialog(Component Object msg) parent. C, 调出带有选项Yes、No和Cancel的对话框;标 题为Select an Option Show. Confirm. Dialog(Component parent. C, Object msg, String str, int opt. Type) 调出一个由 opt. Type 参数确定其中选项数的 对话框 School of Computer Science and Technology


» JOption. Pane. ERROR_MESSAGE » JOption. Pane. PLAIN_MESSAGE 注意:以上这些值可以确定对话框的外观。 – Show. Input. Dialog:用于提示用户进行数据输入,通过返回一个确认/ 否消息给系统以决定取舍。 – Show. Option. Dialog:用于给出可供用户选择的对话框。 – Show. Confirm. Dialog:用于想用户提问一个要求确认的信息,并可得到 YES/NO/CANCEL的响应,方法中的msgtype可取以下值: » JOption. Pane. YES_NO_OPTION » JOption. Pane. OK_CANCEL_OPTION » JOption. Pane. YES_NO_CANCEL_OPTION – 当对话框消失后,show. Confirm. Dialog方法会返回下列整数值之一: » JOption. Pane. YES_OPTION » JOption. Pane. NO_OPTION » JOption. Pane. OK_OPTION » JOption. Pane. CANCEL_OPTION » JOption. Pane. CLOSED_OPTION School of Computer Science and Technology

• 实例 School of Computer Science and Technology

School of Computer Science and Technology

School of Computer Science and Technology


School of Computer Science and Technology

School of Computer Science and Technology

多媒体程序设计 • Java屏幕坐标系 – 单位:像素 • Graphics主要方法 draw 3 DRect(int x, int y, int w, int h, boolean raised) 绘 制 以 (x, y)为 起 点 宽 高 为 w和 h的 3 D矩 形 , 当 raised为true时 3 D效果为凸 fill 3 DRect(int x, int y, int w, int h, boolean raised) 填 充 以 (x, y)为 起 点 宽 高 为 w和 h的 3 D矩 形 , 当 raised为true时 3 D效果为凸 draw. Line(int x 1, int y 1, int x 2, int y 2) 画点(x 1, y 1)和点(x 2, y 2)之间的直线 draw. Rect(int x, int y, int w, int h) 绘制以(x, y)为起点宽高为w和h的矩形 School of Computer Science and Technology

fill. Rect(int x, int y, int w, int h) 填充以(x, y)为起点宽高为w和h的矩形 clear. Rect(int x, int y, int w, int h) 以背景色填充以(x, y)为起点宽高为w和h的矩形 draw. Round. Rect(int x, int y, int w, int h, int arc. W, int arc. H) 绘制以(x, y)为起点宽高为w和h的圆角矩形,四个圆 角的宽度为arc. W,高度为arc. H fill. Round. Rect(int x, int y, int w, int h, int arc. W, int arc. H) 填充以(x, y)为起点宽高为w和h的圆角矩形,四个圆 角的宽度为arc. W,高度为arc. H draw. Oval(int x, int y, int w, int h) 以(x, y)为起点、按宽高为w和h的外接矩形绘制椭圆 fill. Oval(int x, int y, int w, int h) 以(x, y)为起点、按宽高为w和h的外接矩形填充椭圆 draw. Arc(int x, int y, int h, int s. Ang, int arc. Ang) 以 s. Ang为 起 始 角 , arc. Ang为 张 角 画 圆 弧 或 扇 形 , 逆 时针为正 fill. Arc(int x, int y, int h, int s. Ang, int arc. Ang) 以 s. Ang为 起 始 角 , arc. Ang为 张 角 填 充 圆 弧 或 扇 形 , 顺时针为负 draw. Polygon(int y[ ], School of Computer Science x[ and], Technology 按照x[]和y[]数组为定点绘制封闭的points边形


– 此外还有系统定义的13中颜色常量: » WHITE / RED / GREEN / BLUE / GRAY / YELLOW / CYAN / BLACK / MAGENTA / PINK / DARKGRAY / LIGHTGRAY / ORANGE • 文本字体设置 Font(String name, int style, int size) set. Font(Font font) 根据指定名称、样式和磅值大小,创建一个新Font get. Font() 获取字体对象 get. Name() 获取字体的逻辑名字 get. Style() 获取字体字型 get. Size() 获取字体大小 设置字体 – 直接导入图形文件进行处理 • 声明Image类型的变量 – 通过java. awt下Component类的create. Image()方法完成。 Image create. Image(Image. Producer img) 使用指定的图像生成器创建一幅图像 Image create. Image(int w, int h) 返回具有w宽h高的空图像 School of Computer Science and Technology

– java. awt. image包中提供类Memory. Image. Source适用于在内存中生成一 幅图像。 Memory. Image. Source(int w, int h, int pix[ ], int start, int scan) 在 内 存 中 生 成 一 幅 图 像 , 其 中 pix[]代 表 图 像 每 一 点 颜 色 值 , start代 表 起 始 位 置 , scan代 表图像扫描线宽度 • 使用get. Image()加载图像 get. Image(URL url) 获取url中的图像 get. Image(URL url, String name) 获取指定url下文件name中的图像 get. Image(URL url) 获取url中的图像 get. Image(String filename) 获取指定url下文件filename中的图像 • 使用draw. Image()绘制图像 draw. Image(Image img, int x, int y, Image. Observer obs) 绘制指定图像中当前可用的图像 draw. Image(Image img, int x, int y, int w, int h, Color bg, Image. Observer obs) 绘制指定图像中当前可用的图像,其中bg是在 图像非透明部分下绘制的背景色 School of Computer Science and Technology


– 可控制声音的播放 • 获取Audio. Clip对象: get. Audio. Clip(URL url) 从url获取Audio. Clip对象 get. Audio. Clip(URL url, String name) 从url下声音文件name获取Audio. Clip对象 • Audio. Clip提供的三个方法: – play():从头开始播放声音,只播放一次; – loop():循环播放 – stop():停止当前正在播放的声音 School of Computer Science and Technology

总结 School of Computer Science and Technology
- Slides: 102