Building Java GUIs Building GUIs with AWT AWT
Building Java GUIs 图形用户界面设计
• Building GUIs with AWT – AWT:抽象窗口 具集(Abstract Window Toolkit) • The AWT Event Model • The AWT Component Library
Building GUIs with AWT(Abstract Window Toolkit) • java. awt包 提供了基本的java程序的GUI设计 具。 – Component/Menu. Component – Container – Layout. Manager
Java. awt包
Component
Containers • 使用add()方法向Containers中加入 Component • Containers的两种主要类型:Window和 Panel
Frame java. lang. Object | +----java. awt. Component | +----java. awt. Container | +----java. awt. Window | +----java. awt. Frame
import java. awt. *; public class My. Frame extends Frame{ public static void main(String args[ ]){ My. Frame fr = new My. Frame("Hello Out There!"); fr. set. Size(200, 200); fr. set. Background(Color. red); fr. set. Visible(true); } public My. Frame (String str){ super(str); } }
• 运行结果 Frame. Example. java
Panel java. lang. Object | +----java. awt. Component | +----java. awt. Container | +----java. awt. Panel
import java. awt. *; public class Frame. With. Panel extends Frame{ public Frame. With. Panel(String str){ super(str); } public static void main(String args[]){ Frame. With. Panel fr = new Frame. With. Panel("Frame with Panel"); Panel pan=new Panel();
fr. set. Size(200, 200); fr. set. Background(Color. red); fr. set. Layout(null); pan. set. Size(100, 100); pan. set. Background(Color. yellow); fr. add(pan); fr. set. Visible(true); } }
• 运行结果: Frame. With. Panel. java
Layout. Manager(布局管理器) • • • Flow. Layout(Panel、Applet) Border. Layout(Window、Dialogs和Frame) Grid. Layout Card. Layout Grid. Bag. Layout
import java. awt. *; public class Ex. Gui{ private Frame f; private Button b 1; private Button b 2; public static void main(String args[]){ Ex. Gui that = new Ex. Gui(); that. go(); }
public void go(){ f = new Frame("GUI example"); f. set. Layout(new Flow. Layout()); b 1 = new Button("Press Me"); b 2 = new Button("Don't Press Me"); f. add(b 1); f. add(b 2); f. pack(); f. set. Visible(true); } }
• 运行结果 Layout. Example. java
Flow. Layout • Panel,Applet的缺省布局管理器。 set. Layout(new Flow. Layout(Flow. Layout. RIGHT, 20, 40)); set. Layout(new Flow. Layout(Flow. Layout. LEFT)); set. Layout(new Flow. Layout());
import java. awt. *; public class my. Buttons{ public static void main(String args[]) { Frame f = new Frame(); f. set. Layout(new Flow. Layout()); Button button 1 = new Button("Ok"); Button button 2 = new Button("Open"); Button button 3 = new Button("Close"); f. add(button 1); f. add(button 2); f. add(button 3);
f. set. Size(300, 100); f. set. Visible(true); } } 运行结果为: Flow. Example. java
Border. Layout • Window,Frame和Dialog的缺省布局管理器。 Border. Layout布局管理器包括 5个区域: North,South,East,West和Center。
import java. awt. *; public class button. Dir{ public static void main(String args[]){ Frame f = new Frame("Border. Layout"); f. set. Layout(new Border. Layout()); f. add("North", new Button("North")); f. add("South", new Button("South")); f. add("East", new Button("East")); f. add("West", new Button("West")); f. add("Center", new Button("Center")); f. set. Size(200, 200); f. set. Visible(true); } } Border. Example. java
Grid. Layout • 使容器中各个组件呈网格状布局。 import java. awt. *; public class Button. Grid { public static void main(String args[]) { Frame f = new Frame("Grid. Layout"); f. set. Layout(new Grid. Layout(3, 2)); f. add(new Button("1")); f. add(new Button("2"));
f. add(new Button("3")); f. add(new Button("4")); f. add(new Button("5")); f. add(new Button("6")); f. set. Size(200, 200); f. set. Visible(true); } } Grid. Example. java
import java. awt. *; public class Ex. Gui 3{ private Frame f; private Panel p; private Button bw, bc; private Button bfile, bhelp; public static void main(String args[]) { Ex. Gui 3 gui = new Ex. Gui 3(); gui. go(); }
public void go(){ f = new Frame("GUI example 3"); bw=new Button("West"); bc=new Button("Work space region"); f. add(bw, "West"); f. add(bc, "Center"); p = new Panel(); f. add(p, "North"); bfile= new Button("File"); bhelp= new Button("Help"); p. add(bfile); p. add(bhelp); f. pack(); f. set. Visible(true); } } Complex. Layout. Example. java
GUI Event Handling
事件处理模型 • Hierachical model(JDK 1. 0) 事件传递机制。 • Delegation model(JDK 1. 1, 1. 2) 授权处理机制。
import java. awt. *; import java. awt. event. *; public class Test. Button { public static void main(String args[]) { Frame f = new Frame("Test"); Button b = new Button("Press Me!"); b. add. Action. Listener(new Button. Handler()); f. set. Layout(new Flow. Layout()); f. add(b); f. set. Size(200, 100); f. set. Visible(true); } }
class Button. Handler implements Action. Listener { public void action. Performed(Action. Event e) { System. out. println("Action occurred"); } }
• java. util. Event. Object类是所有事件对象的基础类,所有的 事件类都是由它派生出来的。 public class Event. Object implements java. io. Serializable { protected transient Object source; public Event. Object(Object source); public Object get. Source(); public String to. String(); }
Java GUI的分类(1)
Java GUI的分类(2)
AWT事件及其相应的监听器接口 • Action. Event – 激活组件 – Action. Listener – action. Performed(Action. Event)
• Item. Event – 选择了某些项目 – Item. Listener – item. State. Changed(Item. Event)
• Mouse. Event – 鼠标移动 – Mouse. Motion. Listener – mouse. Dragged(Mouse. Event) mouse. Moved(Mouse. Event)
• Mouse. Event – 鼠标点击等 – Mouse. Listener – mouse. Pressed(Mouse. Event) mouse. Released(Mouse. Event) mouse. Entered(Mouse. Event) mouse. Exited(Mouse. Event) mouse. Clicked(Mouse. Event)
• Key. Event – 键盘输入 – Key. Listener – key. Pressed(Key. Event) key. Released(Key. Event) key. Typed(Key. Event)
• Focus. Event – 组件收到或失去焦点 – Focus. Listener – focus. Gained(Focus. Event) focus. Lost(focus. Event)
• Adjustement. Event – 移动了滚动条等组件 – Adjustment. Listener – adjustment. Value. Changed(Adjustment. Event)
• Component. Event – 对象移动缩放显示隐藏等 – Component. Listener – component. Moved(Component. Event) component. Hidden(Component. Event) component. Resized(Component. Event) component. Shown(Component. Event)
• Window. Event – 窗口收到窗口级事件 – Window. Listener – window. Closing(Window. Event) window. Opened(Window. Event) window. Iconified(Window. Event) window. Deiconified(Window. Event) window. Closed(Window. Event) window. Activated(Window. Event) window. Deactivated(Window. Event)
• Container. Event – 容器中增加删除了组件 – Container. Listener – component. Added(container. Event) component. Removed(container. Event)
• Text. Event – 文本字段或文本区发生改变 – Text. Listener – text. Value. Changed(Text. Event)
import java. awt. *; import java. awt. event. *; public class Two. Listen implements Mouse. Motion. Listener, Mouse. Listener, Window. Listener { private Frame f; private Text. Field tf; public static void main(String args[]) { Two. Listen two = new Two. Listen(); two. go(); }
public void go() { f = new Frame("Two listeners example"); f. add(new Label("Click and drag the mouse"), "North"); tf = new Text. Field(30); f. add(tf, "South"); f. add. Mouse. Motion. Listener(this); f. add. Mouse. Listener(this); f. add. Window. Listener(this); f. set. Size(300, 200); f. set. Visible(true); }
public void mouse. Dragged (Mouse. Event e) { String s = "Mouse dragging : X="+e. get. X()+"Y = "+e. get. Y(); tf. set. Text(s); } public void mouse. Moved(Mouse. Event e){} public void mouse. Clicked(Mouse. Event e){} public void mouse. Entered(Mouse. Event e){ String s = "The mouse entered"; tf. set. Text(s); }
public void mouse. Exited(Mouse. Event e){ String s = "The mouse has left the building"; tf. set. Text(s); } public void mouse. Pressed(Mouse. Event e){} public void mouse. Released(Mouse. Event e){} public void window. Closing(Window. Event e) { System. exit(1); }
public void window. Opened(Window. Event e) {} public void window. Iconified(Window. Event e) {} public void window. Deiconified(Window. Event e) {} public void window. Closed(Window. Event e) {} public void window. Activated(Window. Event e) {} public void window. Deactivated(Window. Event e) {} } Two. Listen. java 注意:实现一个接口必须要实现接口中包含 的所有方法。但是,实现的方法体可以为空
实例讲解 • Test. Button. java • Test. Inner. java
匿名类(Anonymous) • 实例讲解Test. Anonymous. java • 练习
GUI-Based Applications AWT 组件类库
Button • • • Button b = new Button("Quit"); Action. Event事件 Action. Listener接口 get. Action. Command() set. Action. Command()
Checkbox • • Item. Event Item. Listener get. State. Change() get. Item()
class Handler implements Item. Listener { public void item. State. Changed(Item. Event ev){ String state = "deselected"; if (ev. get. State. Change() = = Item. Event. SELECTED){ state = "selected" } System. out. println(ev. get. Item()+" "+state); } }
Checkbox. Group • set. Layout(new Grid. Layout(3, 1)); Checkbox. Group cbg = new Checkbox. Group(); add(new Checkbox("one", cbg, true)); add(new Checkbox("two", cbg, false)); add(new Checkbox("three", cbg, false));
File. Dialog • • • java. lang. Object +--java. awt. Component +--java. awt. Container +--java. awt. Window +--java. awt. Dialog +--java. awt. File. Dialog My. File. Dialog. java
Menu. Bar • 只能被添加到Frame对象中,作为整个菜 单树的根基。 Frame fr = new Frame("Menu. Bar"); Menu. Bar mb = new Menu. Bar(); fr. set. Menu. Bar(mb); fr. set. Size(150, 100); fr. set. Visible(true);
Menu • 下拉菜单。它可以被添加到Menu. Bar中或 其它Menu中。 Frame fr = new Frame("Menu. Bar"); Menu. Bar mb = new Menu. Bar(); fr. set. Menu. Bar(mb); Menu m 1 = new Menu("File"); Menu m 2 = new Menu("Edit"); Menu m 3 = new Menu("Help");
mb. add(m 1); mb. add(m 2); mb. set. Help. Menu(m 3); fr. set. Size(200, 200); fr. set. Visible(true);
Menu. Item • Menu. Item是菜单树中的“叶子节点”。 Menu. Item通常被添加到一个Menu中。对 于Menu. Item对象可以添加Action. Listener, 使其能够完成相应的操作。 Menu m 1 = new Menu("File"); Menu. Item mi 1 = new Menu. Item("Save"); Menu. Item mi 2 = new Menu. Item("Load"); Menu. Item mi 3 = new Menu. Item("Quit");
m 1. add(mi 1); m 1. add(mi 2); m 1. add. Separator(); m 1. add(mi 3);
组件外观控制 • 颜色 set. Foreground(); set. Background(); • Color类 – Color. red, Color. blue, Color. green int r = 255; int g = 255; int b = 0; Color c = new Color(r, g, b);
• 字体 set. Font(); 可以通过提供字体名,风格和大小创建一个字体 Font对象。 如:Font f = new Font(“Times. Roman”, Font. PLAIN, 14); 也可以通过如下方式得到当前平台下支持的所有 字体列表: Graphics. Environment ge = Graphics. Environment. get. Local. Graphics. Environmen t(); Font[] fonts = ge. get. All. Fonts();
• 标准的字体名 – Font. Dialog. Input – Font. Serif – Font. Sans. Serif – Font. Monospaced
• 标准的字体风格常量 – Font. BOLD – Font. ITALIC – Font. PLAIN – Font. BOLD + Font. ITALIC
Java Applets
Applet 举例 • Hello. World. java 源程序: import java. awt. Graphics; import java. applet. Applet; public class Hello. World extends Applet { String hw_text ; public void init () { hw_text = "Hello World"; }
public void paint(Graphics g) { g. draw. String (hw_text , 25) ; } } 编译源文件: javac Hello. World. java 产生字节代码:Hello. World. class
• 创建一个HTML文件:hello. html <HTML> <applet code=“Hello. World. class” width=300 height=300> </applet> </HTML>
执行Applet • 用浏览器打开嵌入Applet的HTML文件 • 或者用JDK自带的命令行 具 appletviewer打开HTML文件 – appletviewer hello. html
Java. applet. Applet类 java. lang. Object | +--java. awt. Component | +--java. awt. Container | +--java. awt. Panel | +--java. applet. Applet
Applet的显示 • paint()方法 public class Hello. World extends Applet { String hw_text ; public void init () { hw_text = "Hello World"; } public void paint(Graphics g) { g. draw. String (hw_text , 25) ; } }
Graphics类 • • • draw. String() draw. Line() draw. Polygon() draw. Rect() draw. Oval() draw. Image()
import java. applet. *; import java. awt. event. *; import java. awt. *; public class Paint. Model extends Applet { // The paint model: the last click Point private Point last. Click = null; public void init() { add. Mouse. Listener(new My. Model. Recorder()); } public void update(Graphics g) { paint(g); }
public void paint(Graphics g) { if ( last. Click != null ) { g. draw. String("Hello World!", last. Click. x, last. Click. y); } } private class My. Model. Recorder extends Mouse. Adapter { public void mouse. Pressed(Mouse. Event e) { last. Click = e. get. Point(); repaint(); } } }
与Applet的执行有关的标记 <applet [archive=archive. List] code=applet. File. class width=pixels height=pixels [codebase=codebase. URL] [alt=alternate. Text] [name=applet. Instance. Name] [align=alignment] [vspace=pixels] [hspace=pixels] > [<param name=applet. Attribute 1 value=value>] [<param name=applet. Attribute 2 value=value>]. . . [alternate. HTML] </applet>
• name = applet. Instance. Name Applet的名字。 • align = alignment Applet显示的对齐方式,如:left, right, top, texttop, middle, absmiddle, baseline, bottom, and absbottom。 • vspace = pixels hspace = pixels Applet与显示边框之间的垂直、水平间隔。 • <param name = applet. Attribute 1 value = value> 标记Applet执行时传递给它的参数,由name指定参数 名,value指定参数值。
与Applet有关的常用方法 • get. Document. Base() 返回该Applet所在的HTML文件的URL基地址。 • get. Code. Base() 返回该Applet字节码所在的URL基地址。 • get. Image(URL base, String target) 获取一个图像资源对象。 • get. Audio. Clip(URL base, String target) 获取一个声音资源对象。
import java. awt. *; import java. applet. Applet; public class Hw. Image extends Applet { Image duke; public void init() { duke = get. Image(get. Document. Base(), "graphics/surfer. Duke. gif"); } public void paint(Graphics g) { g. draw. Image(duke, 25, this); } }
• 播放声音的支持 play(URL sound. Directory, String sound. File); play(URL sound. URL);
import java. awt. Graphics; import java. awt. event. Mouse. Adapter; import java. awt. event. Mouse. Event; import java. applet. Applet; public class Hw. Audio extends Applet { public void init() { add. Mouse. Listener(new Mouse. Adapter() { public void mouse. Clicked(Mouse. Event event) { play(get. Code. Base(), "sounds/cuckoo. au"); } }); } public void paint(Graphics g) { g. draw. String("Audio Test", 25); } }
Audio. Clip类 • 在Applet中载入Audio. Clip对象 Audio. Clip sound; sound = get. Audio. Clip(get. Document. Base(), "bark. au"); • 利用Audio. Clip对象播放声音 sound. play(); sound. loop(); sound. stop(); 支持的声音文件类型有:. midi, . wav, . aiff, . au
public class Hw. Loop extends Applet { Audio. Clip sound; public void init() { sound = get. Audio. Clip(get. Code. Base(), "sounds/cuckoo. au"); } public void paint(Graphics g) { g. draw. String("Audio Test", 25); } public void start() { sound. loop(); } public void stop() { sound. stop(); } }
在HTML和Applet之间传递参数 <html> <applet code="Parameters. class" width=200 height=200> <param name=speed value="12"> <param name=distance value="500 m"> </applet> </html>
public class Parameters extends Applet { private String to. Display; private int speed; public void init() { String pv; pv = get. Parameter("speed"); if (pv == null){ speed = 10; } else { speed = Integer. parse. Int (pv); } to. Display = "Speed given: " + speed; }
public void paint(Graphics g) { g. draw. String(to. Display, 25); } }
- Slides: 130