版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、1,目标重点难点,【课前思考】1 java语言是跨平台的编程语言,那么图形用户界面如何做到跨平台?2 AWT有哪些组件和容器?它们各自的使用方法是什么?3 AWT的事件处理模型是什么?原理又如何? 【学习目标】掌握用AWT来设计图形用户界面的方法,尤其是组件、容器、布局管理器等概念。学习AWT事件处理模型,掌握事件源、事件、事件处理者等概念,让程序能够响应用户的操作。最后了解AWT各个组件的用法及所采用的事件处理接口。 【学习指南】理解概念,多实践,勤思考,举一反三。 【难 重 点】 重点:事件处理模型。难点:内部类匿名类在AWT中的应用。,2,图形用户界面设计,3,使用AWT构造GUI 组件
2、 容器 布局管理 AWT 事件处理模型 AWT 组件,4,使用AWT构造,java.awt包 提供了基本的java程序的GUI设计工具。 Component Container LayoutManager,5,Java.lang.Object,AWTEvent,Font,Componet,Graphics,MenuComponent,各种布局管理器类,Container,Panel,Applet,Window,Frame,6,Component(组件),Java的图形用户界面的最基本组成部分是组件,组件是一个可以以图形化的方式显示在屏幕上并能与用户进行交互的对象,例如一个按钮,一个标签等。 组
3、件不能独立地显示出来,必须将组件放在一定的容器中才可以显示出来。 组件的一般功能 基本的绘画支持 外形控制 大小和位置控制 图像处理 组件的状态控制,7,Container(容器),容器(Container)实际上是Component的子类,因此容器本身也是一个组件,具有组件的所有性质,另外还具有容纳其它组件和容器的功能。 主要功能 组件的管理 布局管理,8,Graphics类,Graphics是所有用来在组件上进行图形绘制时所使用的图形环境上下文的父类. 它提供了对组件进行绘制的一般方法和接口 封装了用来进行图形绘制时必须的状态信息 要绘制的组件对象 当前颜色 当前字体 当前逻辑点操作的功能
4、(Xor或者paint) 当前XOR方式的替代颜色。,9,LayoutManager(布局管理器),为了使我们生成的图形用户界面具有良好的平台无关性,Java语言中,提供了布局管理器这个工具来管理组件在容器中的布局,而不使用直接设置组件位置和大小的方式。 每个容器都有一个布局管理器,当容器需要对某个组件进行定位或判断其大小尺寸时,就会调用其对应的布局管理器。,10,在程序中安排组件的位置和大小时,应该注意: 容器中的布局管理器负责各个组件的大小和位置,因此用户无法在这种情况下设置组件的这些属性。如果试图使用Java语言提供的setLocation(),setSize(),setBounds()
5、等方法,则都会被布局管理器覆盖。 如果用户确实需要亲自设置组件大小或位置,则应取消该容器的布局管理器,方法为: setLayout(null);,11,常用容器,Frame Panel Applet,12,Frame,java.lang.Object | +-java.awt.Component | +-java.awt.Container | +-java.awt.Window | +-java.awt.Frame,13,import java.awt.*; public class MyFrame extends Frame public static void main(String a
6、rgs ) MyFrame fr = new MyFrame(Hello Out There!); fr.setSize(200,200); fr.setBackground(Color.red); fr.setVisible(true); public MyFrame (String str) super(str); ,14,运行结果,15,Panel,java.lang.Object | +-java.awt.Component | +-java.awt.Container | +-java.awt.Panel,16,import java.awt.*; public class Fram
7、eWithPanel extends Frame public FrameWithPanel(String str) super(str); public static void main(String args) FrameWithPanel fr = new FrameWithPanel(Frame with Panel); Panel pan=new Panel();,17,fr.setSize(200,200); fr.setBackground(Color.red); fr.setLayout(null); pan.setSize(100,100); pan.setBackgroun
8、d(Color.yellow); fr.add(pan); fr.setVisible(true); ,18,运行结果:,19,LayoutManager,FlowLayout BorderLayout GridLayout CardLayout GridBagLayout,20,import java.awt.*; public class ExGui private Frame f; private Button b1; private Button b2; public static void main(String args) ExGui that = new ExGui(); tha
9、t.go(); ,21,public void go() f = new Frame(GUI example); f.setLayout(new FlowLayout(); b1 = new Button(Press Me); b2 = new Button(Dont Press Me); f.add(b1); f.add(b2); f.pack(); f.setVisible(true); ,22,运行结果,23,FlowLayout,Panel,Applet的缺省布局管理器。 setLayout(new FlowLayout(FlowLayout.RIGHT,20,40); setLayo
10、ut( (new FlowLayout FlowLayout.LEFT); setLayout(new FlowLayout(); 第1个参数设置对齐方向(alignment ),缺省情况是居中, 第2,3个参数设置横向和纵向的间隔(gap),缺省值是5,24,import java.awt.*; public class myButtons public static void main(String args) Frame f = new Frame(); f.setLayout(new FlowLayout(); Button button1 = new Button(Ok); Butt
11、on button2 = new Button(Open); Button button3 = new Button(Close); f.add(button1); f.add(button2); f.add(button3);,25,f.setSize(300,100); f.setVisible(true); 运行结果为:,26,BorderLayout,Window,Frame和Dialog的缺省布局管理器。BorderLayout布局管理器包括5个区域:North,South,East,West和Center。添加组件时,必须指明添加位置.否则无法显示. import java.awt
12、.*; public class buttonDir public static void main(String args) Frame f = new Frame(BorderLayout); f.setLayout(new BorderLayout();,27,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.setSiz
13、e(200,200); f.setVisible(true); ,28,运行结果,29,30,GridLayout,使容器中各个组件呈网格状布局。从上到下,从左到右一次排列. import java.awt.*; public class ButtonGrid public static void main(String args) Frame f = new Frame(GridLayout); f.setLayout(new GridLayout(3,2); f.add(new Button(1); f.add(new Button(2);,31,f.add(new Button(3);
14、f.add(new Button(4); f.add(new Button(5); f.add(new Button(6); f.setSize(200,200); f.setVisible(true); ,32,运行结果,33,CardLayout,CardLayout布局管理器能够帮助用户处理两个以至更多的成员共享同一显示空间。,34,import java.awt.*; import java.awt.event.*; public class ThreePages implements MousListener CardLayout layout=new CardLayout(); F
15、rame f=new Frame(“CardLayout”); Button page1Button; Label page2Label; TextArea page3Text; Button page3Top; Button page3Bottom; public static void main(String args) new ThreePages().go(); ,35,Public void go() f.setLayout(layout); f.add(page1Button=new Button(“Button page”) , “page1Button”); page1Butt
16、on.addMouseListener(this); f.add(page2Label=new Label(“Label page”) , “page2Label”); page2Label.addMouseLisener(this); /注册监听器 Panel panel=new Panel(); panel.setLayout(new BorderLayout(); panel.add(page3Text=new TextArea(“Composite page”), “Center”);,36,page3Text.addMouseListener(this); panel.add(pag
17、e3Top=new Button(“Top button”) , “North”); page3Top.addMouseListener(this); panel.add(page3Bottom=new Button(“Bottom button”) ,“South”); page3Bottom.addMouseListener(this); f.add(panel,“panel”); f.setSize(200,200); f.setVisible(true); ,37,public void mouseClicked(MouseEvent e) layout.next(f); public
18、 void mouseEntered(MouseEvent e) public void mouseExited(MouseEvent e) public void mousePressed(MouseEvent e) public void mouseReleased(MouseEvent e) ,38,模拟考题,Question 31) Which of the following statements are true? 1) The default layout manager for an Applet is FlowLayout 2) The default layout mana
19、ger for a Frame is FlowLayout 3) A layout manager must be assigned to an Applet before the setSize method is called 4) The FlowLayout manager attempts to honor the preferred size of any components,39,模拟考题,Answer to Question 31) 1) The default layout manager for an Applet is FlowLayout 4) The FlowLay
20、out manager attempts to honor the preferred size of any components,40,容器的嵌套,import java.awt.*; public class ExGui3 private Frame f; private Panel p; private Button bw,bc; private Button bfile,bhelp; public static void main(String args) ExGui3 gui = new ExGui3(); gui.go(); ,41,public void go() f = ne
21、w 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);,42,bfile= new Button(File); bhelp= new Button(Help); p.add(bfile); p.add(bhelp); f.pack(); f.setVisible(true); ,43,运行结果:,44,Frame Frame是一个顶级窗口。 Frame的缺省布局
22、管理器为BorderLayout。 Panel Panel无法单独显示,必须添加到某个容器中。 Panel的缺省布局管理器为FlowLayout。 当把Panel作为一个组件添加到某个容器中后,该Panel仍然可以有自己的布局管理器。因此,可以利用Panel使得BorderLayout中某个区域显示多个组件。,45,无布局管理器,setLayout(null),46,AWT 事件处理机制,47,什么是事件?,Event 事件,就是发生在用户界面上的用户交互行为所产生的一种效果。 Event Source 产生事件的对象。 Event handler(Listener) 接收事件对象并对其进行处
23、理的方法。,48,事件处理模型,Hierachical model(JDK 1.0) 事件传递机制。 Delegation model(JDK 1.1, 1.2), 授权(委托)处理机制。,49,Delegation Model(事件授权处理模型),将事件源对象和事件处理器(事件监听器)分开。,50,import java.awt.*; import java.awt.event.*; public class TestButton public static void main(String args) Frame f = new Frame(Test); Button b = new Bu
24、tton(Press Me!); b.addActionListener(new ButtonHandler(); f.setLayout(new FlowLayout(); f.add(b);,51,f.setSize(200,100); f.setVisible(true); class ButtonHandler implements ActionListener public void actionPerformed(ActionEvent e) System.out.println(Action occurred); ,52,53,使用JDK1.1授权处理模型进行事件处理的一般方法:
25、 对于某种类型的事件XXXEvent,要想接收并处理这类事件,必须定义相应的事件监听器类,该类需要实现针对特定事件的特定接口XXXListener; ActionEvent ActionListener 事件源中产生事件后,必须注册相应于该类事件的监听器,使用addXXXListener(XXXListener )方法来注册监听器。 事件发生后,产生表示特定事件的事件对象,事件对象被传递给已经注册的事件监听器,调用监听器中的特定方法处理事件,54,事件对象,java.util.EventObject类 EventObject类是所有事件对象的基础类,所有的事件类都是由它派生出来的。 publi
26、c class EventObject implements java.io.Serializable protected transient Object source; public EventObject(Object source); public Object getSource(); public String toString(); ,55,java.awt.AWTEvent,和AWT有关的所有事件类都由java.awt.AWTEvent类派生 ,它也是EventObject类的子类。AWT事件共有10类,可以归为两大类:低级事件和高级事件。,56,低级事件 ComponentE
27、vent(组件事件:组件尺寸的变化,移动) ContainerEvent(容器事件:组件增加,移动) WindowEvent(窗口事件:关闭窗口,窗口闭合,图标化) FocusEvent(焦点事件:焦点的获得和丢失) KeyEvent(键盘事件:键按下、释放) MouseEvent(鼠标事件:鼠标单击,移动),57,高级事件(语义事件) ActionEvent(动作事件:按钮按下,TextField中按Enter键) AdjustmentEvent(调节事件:在滚动条上移动滑块以调节数值) ItemEvent(项目事件:选择项目,不选择“项目改变”) TextEvent(文本事件,文本对象改变
28、),58,事件监听器,每类事件都有对应的事件监听器 监听器是接口,根据动作来定义方法。 interface KeyListener extends java.util.EventListener public void keyPressed(KeyEvent ev); public void keyTeleased(KeyEvent ev); public void keyTyped(KeyEvent ev); ,59,注册和注销监听器,注册监听器: public void add (listener); 注销监听器: public void remove (listener);,60,注册和
29、注销监听器,AWT的组件类中提供注册和注销监听器的方法,例如Button类:(查API) public class Button extends Component . public synchronized void addActionListener(ActionListener l); public synchronized void removeActionListener(ActionListener l); ,61,AWT事件及其相应的监听器接口,ActionEvent 激活组件 ActionListener ActionPerformed(ActionEvent),62,Item
30、Event 选择了某些项目 ItemListener ItemStateChanged(ItemEvent),63,MouseEvent 鼠标移动 MouseListener mouseDragged(MouseEvent) mouseMoved(MouseEvent),64,MouseEvent 鼠标点击等 MouseListener mousePressed(MouseEvent) mouseReleased(MouseEvent) mouseEntered(MouseEvent) mouseExited(MouseEvent) mouseClicked(MouseEvent),65,Ke
31、yEvent 键盘输入 KeyListener KeyPressed(KeyEvent) KeyReleased(KeyEvent) KeyTyped(KeyEvent),66,FocusEvent 组件收到或失去焦点 FocusListener focusGained(FocusEvent) focusLost(focusEvent),67,AdjustementEvent 移动了滚动条等组件 AdjustmentListener adjustmentValueChanged(AdjustmentEvent),68,ComponentEvent 对象移动缩放显示隐藏等 ComponentLi
32、stener componentMoved(ComponentEvent) componentHidden(ComponentEvent) componentResized(ComponentEvent) componentShown(ComponentEvent),69,WindowEvent 窗口收到窗口级事件 WindowListener windowClosing(WindowEvent) windowOpened(WindowEvent) windowIconified(WindowEvent) windowDeiconified(WindowEvent) windowClosed(
33、WindowEvent) windowActivated(WindowEvent) windowDeactivated(WindowEvent),70,ContainerEvent 容器中增加删除了组件 ContainerListener componentAdded(containerEvent) componentRemoved(containerEvent),71,TextEvent 文本字段或文本区发生改变 TextListener textValueChanged(TextEvent),72,例12.12 import java.awt.*; import java.awt.even
34、t.*; public class TwoListen implementsMouseMotionListener,MouseListener,WindowListener private Frame f; private TextField tf; public static void main(String args) TwoListen two = new TwoListen(); two.go(); ,73,public void go() f = new Frame(Two listeners example); f.add(new Label(Click and drag the
35、mouse),North); tf = new TextField(30); f.add(tf,South); f.addMouseMotionListener(this); f.addMouseListener(this); f.addWindowListener(this); f.setSize(300,200); f.setVisible(true); ,74,public void mouseDragged (MouseEvent e) String s = Mouse dragging : X=+e.getX()+Y = +e.getY(); tf.setText(s); publi
36、c void mouseMoved(MouseEvent e) public void mouseClicked(MouseEvent e) public void mouseEntered(MouseEvent e) String s = The mouse entered; tf.setText(s); ,75,public void mouseExited(MouseEvent e) String s = The mouse has left the building; tf.setText(s); public void mousePressed(MouseEvent e) publi
37、c void mouseReleased(MouseEvent e) public void windowClosing(WindowEvent e) System.exit(1); ,76,public void windowOpened(WindowEvent e) public void windowIconified(WindowEvent e) public void windowDeiconified(WindowEvent e) public void windowClosed(WindowEvent e) public void windowActivated(WindowEv
38、ent e) public void windowDeactivated(WindowEvent e) ,77,可以声明多个接口 implements MouseMotionListener, MouseListener,WindowListener 可以同时监听一个事件源上发生的多种事件: f.addMouseMotionListener(this); f.addMouseListener(this); f.addWindowListener(this); 可以通过事件对象获得详细资料 public void mouseDragged(MouseEvent e) String s=“Mous
39、e dragging :X=” +e.getX()+“Y=”+e.getY(); tf.setText(s); ,78,事件适配器(Event Adapters),一种简单的实现监听器的手段,缺点是只能单一继承. import java.awt.*; import java.awt.event.*; public class MouseClickHandler extends MouseAdaper public void mouseClicked(MouseEvent e) ,79,事件适配器(Event Adapters),Java.awt.event包中定义的适配器类有: Compone
40、ntAdapter(组件适配器) ContainerAdapter(容器适配器) FocusAdapter(焦点适配器) KeyAdapter(键盘适配器) MouseAdapter(鼠标适配器) MouseMotionAdapter(鼠标运动适配器) WindowAdapter(窗口适配器),80,模拟考题,Question 19) Which of the following are true? 1) A component may have only one event listener attached at a time 2) An event listener may be rem
41、oved from a component 3) The ActionListener interface has no corresponding Adapter class 4) The processing of an event listener requires a try/catch block,81,模拟考题,Answer to Question 19) 2) An event listener may be removed from a component 3) The ActionListener interface has no corresponding Adapter
42、class,82,AWT 组件库,83,按钮(Button),Button b = new Button(Quit); ActionEvent事件 ActionListener接口 getActionCommand() setActionCommand(),84,复选框 (Checkbox),setLayout(new GridLayout(3,1); add(new Checkbox(“one”,null,true); add(new Checkbox(“two”); add(new Checkbox(“three”);,85,复选框 (Checkbox),ItemEvent ItemLis
43、tener getStateChange()获取当前状态 getItem()获得被修改复选框的字符串对象,86,class Handler implements ItemListener public void itemStateChanged(ItemEvent ev) String state = deselected; if (ev.getStateChange() = = ItemEvent.SELECTED) state = selected System.out.println(ev.getItem()+ +state); ,87,复选框组(CheckboxGroup),setLa
44、yout(new GridLayout(3, 1); CheckboxGroup cbg = new CheckboxGroup(); add(new Checkbox(one, cbg, true); add(new Checkbox(two, cbg, false); add(new Checkbox(three, cbg, false);,88,下拉式菜单,Choice Colorchooser=new Choice(); Colorchooser.add(“Green”); Colorchooser.add(“Red”); Colorchooser.add(“Blue”); 用Item
45、Listener接口来进行监听,89,Canvas,如创建一个自定义组件,一个应用程序必须继承Canvas类才能获得有用的功能。如果想在画布上完成一些图形处理,则Canvas类中的paint()方法必须被重写。 Canvas组件监听各种鼠标,键盘事件。当在Canvas组件中输入字符时,必须先调用requestFocus()方法。,90,例12.14 import java.awt.*; import java.awt.event.*; import java.util.*; public class MyCanvas implements KeyListener, MouseListener
46、Canvas c; String s=“”; public static void main(String args) Frame f=new Frame(“Canvas”); MyCanvas mc=new Mycanvas(); mc.c=new Canvas(); f.add(“Center”,mc.c);,91,f.setSize(150,150); mc.c.addMouseListerner(mc); mc.c.addKeyListener(mc); f.setVisible(true); public void mouseClicked(MouseEvent ev) System
47、.out.println(“MouseClicked”); c.requestFocus(); public void keyTyped(KeyEvent ev) System.out.println(“KeyTyped”); s+=ev.getKeyChar(); c.getGraphics().drawString(s,0,20); ,92,public void keyPressed(KeyEvent ev) System.out.println(“KeyPressed”); public void keyReleased(KeyEvent ev) System.out.println(
48、“KeyReleased”); public void mousePressed(MouseEvent ev) System.out.println(“MousePressed”); public void MouseReleased(MouseEvent ev) System.out.println(“MouseReleased”); public void mouseEntered(MouseEvent ev) System.out.println(“MouseEntered”); public void mouseExited(MouseEvent ev) System.out.prin
49、tln(“MouseExited”); ,93,TextField,单行文本输入区 当回车键被按下时,会发生ActionEvent事件,可以通过ActionListener中的actionPerformed()方法对事件进行相应处理。 可以使用setEditable(boolean)方法设置为只读属性。,94,TextField,TextField tf1,tf2,tf3,tf4: tf1=new TextField(); tf2=new TextField(“”,20);/显示区域为20列 tf3=new TextField(“Hello!”);/按文本区域显示 tf4=new TextFi
50、eld(“Hello!”,30); /*初始文本为Hello!, 显示区域为30列,95,TextArea,TextArea可以显示多行多列的文本。使用setEditable(boolean)方法,可以将其设置为只读的。在TextArea中可以显示水平或垂直的滚动条。 要判断文本是否输入完毕,可以在TextArea旁边设置一个按钮,通过按钮点击产生的ActionEvent对输入的文本进行处理。,96,列表(List),List lst=new List(4,false); lst.add(“Venus”); lst.add(“Earth”); lst.add(“JavaSoft”); lst.
51、add(“Mars”); cnt.add(lst);,97,Frame,顶级窗口,可以显示标题 WindowEvent事件 Frame无法直接监听键盘输入事件。,98,Dialog,是Window类的子类。 对话框和一般窗口的区别在于它依赖于其它窗口。 对话框分为非模式(non-modal)和模式(modal)两种。,99,文件对话框(Filedialog),FileDialog d=new FileDialog(ParentFr,“FileDialog); d.setVisible(true); String filename=d.getFile();,100,菜单,无法直接将菜单添加到容器的某一位置,也无法使用布局管理器对其加以控制。菜单只能被添加到“菜单容器”中。,101,MenuBar,只能被添加到Frame对象中,作为整个菜单树的根基。 Frame fr = new Frame(MenuBar); MenuBar mb = new MenuBar(); fr.setMenuBar(mb); fr.setSize(150,100); fr.setVisible(true);,102,Menu,下拉菜单。它可以被添加到MenuBar中或其它Menu中。 Frame fr = new Fram
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 企业年度目标完成情况汇报函7篇
- 跨界融合行业绩效评价表
- 合同付款方式变更确认回复函5篇范文
- 关于新项目启动的会议通知(6篇范文)
- 数据安全技术文档与操作手册
- 商务经理业务增长KPI绩效考核表
- 关于营销推广效果总结的通知函件8篇范文
- IT部门项目进度考核表
- 教育顾问学生学习发展评估表
- 产品售后服务通知函(3篇范文)
- 2026年时事政治考题及参考答案
- 4.3 化学键(教案)2022-2023学年高一化学人教版(2019)必修第一册
- 2026年秋新教材统编版九年级上册历史全册教案(含教学反思)
- 施工现场污水沉淀池设计
- 2026人教版五年级数学上册第四单元第3课《平移(2)》课件
- 新版人教版四年级上册数学全册教案(完整版)教学设计含教学反思
- 医院物价管理制度 (三)
- 《网约车运营管理》教学课件1-2 网约车租赁还车业务
- 2026届浙江温州高三一模高考英语模拟试卷试题(含答案详解)
- 2025年北京市怀柔区六年级语文上册期中考试试卷及答案
- 美牙瓷贴面教学课件
评论
0/150
提交评论