javaawt编程.ppt

上传人:本田雅阁 文档编号:2892953 上传时间:2019-06-02 格式:PPT 页数:44 大小:315.52KB
返回 下载 相关 举报
javaawt编程.ppt_第1页
第1页 / 共44页
javaawt编程.ppt_第2页
第2页 / 共44页
javaawt编程.ppt_第3页
第3页 / 共44页
javaawt编程.ppt_第4页
第4页 / 共44页
javaawt编程.ppt_第5页
第5页 / 共44页
点击查看更多>>
资源描述

《javaawt编程.ppt》由会员分享,可在线阅读,更多相关《javaawt编程.ppt(44页珍藏版)》请在三一文库上搜索。

1、第十章 图形用户界面AWT,10.1 AWT简介 组件 容器 布局管理器 10.2 AWT事件处理模型 事件监听器 事件适配器 10.3 AWT举例,10.1 AWT简介,AWT(Abstract Window Toolkit):是Java程序提供的建立图形用户界面(Graphics User Interface,简称GUI)的工具集,通过GUI可以画线、矩形、圆形等基本图形,并且能创建按钮、标签、列表框等与用户进行交互的组件,使用户方便的建立自己的图形用户界面。AWT主要包括组件、容器、布局管理器、事件处理模型、图形图像工具和数据传送类等组成。,10.1 AWT简介,AWT的组件是重量级的。

2、 AWT包 java.awt java.awt.accessibility java.awt.color java.awt.datatransfer java.awt.dnd java.awt.event,java.awt.im java.awt.image java.awt.peer java.awt.print java.awt.font java.awt.geom,10.1 AWT简介,AWT包主要类的层次关系,10.1 AWT简介,AWT包主要类的层次关系,10.1 AWT简介,组件 Component,MenuComponent Component类方法: getComponentA

3、t(int x,int y), getFont(),getForeground(),getName(), getSize(),repaint(),paint(Graphics g), update(),setVisible(boolean b), setSize(Dimension d),setName(String name),10.1 AWT简介1 组件,Canvas,TextArea,Label,ScrollBar,CheckBox,List,TextField,Button,Choice,10.1 AWT简介1 组件,Netscape Communicator,Menu,Menubar

4、,Button,Label,Text field,10.1 AWT简介1 组件,基本组件 按钮Button: Button(), Button(String label) 标签Label:文本字符串,仅用于显示,所以不生成动作事件。Label(),Label(String),setText(),getText() 复选框和单选按钮Checkbox: Checkbox( String label), Checkbox( String label,boolean state), Checkbox( String label,boolean state,CheckboxGroup) 下拉式列表Cho

5、ice和列表List: Choice是文本字符串的弹出式菜单, Choice(),add();列表List可允许多选,支持滚动条,List(int,boolean),add() 单行文本组件TextField和多行文本组件TextArea:(setText(),getText(),SetEditable(),10.1 AWT简介1 组件,基本组件 TextField:setEchoChar() TextArea:append(),insert(),replace() 画布canvas:绘图功能,paint(),requestFocus() 滚动条ScrollBar:Scrollbar() ,S

6、crollbar(int orientation) 组件外观颜色和字体: Java.awt.Color: black, blue, cyan, darkGray, gray, green, lightGray, magenta, orange, pink, red, white, Yellow;Color(int r,int g,int b) Java.awt.Font:setFont() 菜单组件:MenuBar,Menu,MenuItem, CheckboxMenuItem,MenuShortcut,10.1 AWT简介2 容器,容器:是一种可以含有其他组件的组件,AWT中的容器都是从Co

7、ntainer抽象类派生而来的. 类型:Window,panel,ScrollPane 常用:Panel,Frame,Applet Panel和Applet:Applet从Panel类继承而来,而 Panel 从Container类继承而来,它不创建自己的窗口,因为它常用于将组件编组放入其它容器(Frame,Applet)中,它缺省的布局管理器为FlowLayout。 Frame:是一个功能齐全的、顶层的、可重定义尺寸的、带有菜单条的窗口。可以指定标题、图标和光标。它缺省的布局管理器为BorderLayout,且生成与窗口一样的事件:WindowOpened,WindowClosing,Win

8、dowClosed, Windows:不常使用, 它是一个顶层的窗口,不带边界和菜单条。,10.1 AWT简介3 布局管理,常用布局管理器 FlowLayout:组件从左到右、从上到下,一个挨一个地放在容器中。Panel和 Applet的默认容器。 GridLayout:网格布局管理器。每个网格单元放置一个组件或容器。 BorderLayout:按照东、西、南、北、中安排组件。是Window、Frame、Dialog的默认容器。 CardLayout:卡式布局管理器。 GridBagLayout:复杂的网格布局管理器。,10.1 AWT简介3 布局管理,BorderLayout类 功能:分成五

9、个区 创建: setLayout(new BorderLayout(); 将其他构件加入 add(“ East”, new Button(“东”); add(“South”, new Button(“南”); add(“West”, new Button(“西”); add(“North”, new Button(“北”); add(“Center”, new Button(“中”);,10.1 AWT简介3 布局管理,FlowLayout类 FlowLayout(); FlowLayout(FlowLayout.LEFT);(5像素) FlowLayout(FlowLayout.RIGHT,

10、10,20); GridLayout类 GridLayout mylayout = new GridLayout(3,3) setLayout();,10.1 AWT简介3 布局管理,GridBagLayout类和GridBagConstraints类 功能:借助于GridBagConstraints类,实现更灵活的外观管理 每个构件后都跟随一个GridBagLayout对象实体,来决定构件的外观. 创建 GridBagLayout myLayout=new GridBagLayout(); GridBagConstraints类的约束条件 gridwidth, gridheight, gri

11、dx, gridy, weightx, weighty, ipadx, ipady, insets , fill,anchor 设置(P287),10.1 AWT简介3 布局管理,例:(GridBagApplet.html),10.1 AWT简介3 布局管理,public void init() GridBagLayout layout=new GridBagLayout(); setLayout(layout); GridBagConstraints GBC = new GridBagConstraints(); Button button1=new Button(“button1“); B

12、utton button2=new Button(“button2“); Button button3=new Button(“button3“); Button button4=new Button(“button4“); Button button5=new Button(“button5“); Button button6=new Button(“button6“); Button button7=new Button(“button7“); Button button8=new Button(“button8“); Button button9=new Button(“button9“

13、);,10.1 AWT简介3 布局管理,GBC.fill=GridBagConstraints.BOTH; (按钮可以在水平和垂直两个方向扩展) layout.setConstraints(button1,GBC); add(button1); GBC.gridwidth=GridBagConstraints.RELATIVE; (BOTH依然起作用, 紧挨着最后一个按钮,) layout.setConstraints(button2,GBC); add(button2); GBC.gridwidth=GridBagConstraints.REMAINDER; (填充剩余部分) layout.

14、setConstraints(button3,GBC); add(button3);,10.1 AWT简介3 布局管理,GBC.gridwidth= GridBagConstraints.REMAINDER; (表示该按钮独占一行) layout.setConstraints(button4,GBC); add(button4);,10.1 AWT简介3 布局管理,GBC.gridwidth=2; (表示该按钮占两个单元) layout.setConstraints(button5,GBC); add(button5); GBC.gridwidth= GridBagConstraints.RE

15、MAINDER; layout.setConstraints(button6,GBC); add(button6);,10.1 AWT简介3 布局管理,GBC.gridwidth=1; GBC.gridheight=2; (高度为两个单元) layout.setConstraints(button7,GBC); add(button7);,10.1 AWT简介3 布局管理,GBC.gridwidth= GridBagConstraints.REMAINDER; GBC.gridheight=1; layout.setConstraints(button8,GBC); add(button8);

16、 layout.setConstraints (button9,GBC); add(button9);,10.1 AWT简介例,10.1 AWT简介例,import java.awt.*; public class Test Frame f; Button b; TextField tf; public void creat() f = new Frame(“testing“); b= new Button(“press“); f.add(b, “North“);,10.1 AWT简介例,tf= new TextField(); f.add(tf, “South“); f.setSize(20

17、0,200); f.setVisible(true); public static void main(String args) Test t = new Test(); t.creat(); ,10.2 AWT事件处理模型,Java1.1:基于事件的授权模型。 事件 事件源 事件处理器 任何类都能接收和管理事件,不管该类是否将Component当作超类。 事件是通过事件监听器(event listeners)来管理的. 源码顶部需加入:import java.awt.event.*,10.2 AWT事件处理模型事件类,事件类 (10类):,java.util.EventObject,java

18、.awt.AWTEvent,ComponentEvent,ItemEvent,TextEvent,ContainerEvent,FocusEvent,InputEvent,PaintEvent,WindowEvent,KeyEvent,MouseEvent,AdjustmentEvent,ActionEvent,10.2 AWT事件处理模型事件监听器,10.2 AWT事件处理模型事件监听器,10.2 AWT事件处理模型事件监听器例1,每类事件对应事件监听器,监听器是接口,所以要在类中实现接口。 import java.awt.*; import java.awt.event.*; public

19、 class Test1 implements ActionListener Frame f; Button b; TextField tf; public void creat() f = new Frame(“testing“); b= new Button(“press“); b.addActionListener(this); f.add(b, “North“);,10.2 AWT事件处理模型事件监听器例1,tf= new TextField(); f.add(tf, “South“); f.setSize(200,200); f.setVisible(true); public vo

20、id actionPerformed(ActionEvent e) tf.setText(“the Button is pressed“); public static void main(String args) Test1 t = new Test1(); t.creat(); ,10.2 AWT事件处理模型事件监听器例2,import java.awt.*; import java.awt.event.*; public class Test3 implements ActionListener,WindowListener Frame f; Button b; TextField tf

21、; public void creat() f = new Frame(“testing“); f.setLayout(new BorderLayout(); f.addWindowListener(this); b= new Button(“press“); b.addActionListener(this); f.add(b, “North“);,10.2 AWT事件处理模型事件监听器例2,tf= new TextField(); f.add(tf, “South“); f.setSize(200,200); f.setVisible(true); public void actionPe

22、rformed(ActionEvent e) tf.setText(“the Button is pressed“); public static void main(String args) Test3 t = new Test3(); t.creat(); ,10.2 AWT事件处理模型事件监听器例2,public void windowClosing(WindowEvent e) System.exit(0); public void windowActivated(WindowEvent e); public void windowClosed(WindowEvent e); publ

23、ic void windowDeactivated(WindowEvent e); public void windowDeiconified(WindowEvent e); public void windowIconified (WindowEvent e); public void windowOpened(WindowEvent e); ,10.2 AWT事件处理模型事件适配器,为了简单化,为一些监听器Listener接口提供了适配器Adapter类. ComponentAdapter ContainerAdapter FocusAdapter KeyAdapter MouseAdap

24、ter MouseMotionAdapter WindowAdapter,10.2 事件适配器举例内部类,import java.awt.*; import java.awt.event.*; public class Test4 implements ActionListener Frame f; Button b; TextField tf; public void creat() f = new Frame(“testing“); f.setLayout(new BorderLayout(); f.addWindowListener(new WinClosing(); b= new Bu

25、tton(“press“); b.addActionListener(this); f.add(b, “North“);,10.2 事件适配器举例内部类,tf= new TextField(); f.add(tf, “South“); f.setSize(200,200); f.setVisible(true); public static void main(String args) Test4 t = new Test4(); t.creat(); public void actionPerformed(ActionEvent e) tf.setText(“the Button is pr

26、essed“); ,10.2 事件适配器举例内部类,class WinClosing extends WindowAdapter public void windowClosing(WindowEvent e) System.exit(0); ,10.3 AWT举例菜单,import java.awt.*; public class TestMenu private Frame f; private MenuBar mb; private Menu m1,m2,mhelp; private MenuItem mi11,mi12,mi13,mi21,mih; CheckboxMenuItem m

27、i22; public TestMenu() f = new Frame(“Test Frame”); mb = new MenuBar(); m1 = new Menu(“文件”); m2 = new Menu(“编辑”);,查找,工具,10.3 AWT举例菜单,mhelp = new Menu(“帮助”); mi11 = new MenuItem(“新建”); mi12 = new MenuItem(“打开”); mi13 = new MenuItem(“保存”); mi21 = new MenuItem(“查找”); mi22 = new CheckboxMenuItem(“工具”);

28、mih = new MenuItem(“帮助文件”); m1.add(mi11); m1.add(mi12); m1.add(mi13);,10.3 AWT举例菜单,m1.addSeparator(); m2.add(mi21); m2.add(mi22); m1.add(m2); f.setMenuBar(mb); mb.add(m1); mhelp.add(mih); mb.setHelpMenu(mhelp); f.setSize(200,200); f.setVisible(true); public static void main(String args) TestMenu tm = new TestMenu(); ,本章小结,概念:组件、容器、布局管理器、事件、事件监听器、事件适配器 组件、容器、布局管理器、事件、事件监听器、事件适配器分别有哪些? 事件处理模型 事件监听器 事件适配器 内部类 AWT编程,习题,什么是组件、容器、布局管理器?分别有哪些? 什么是事件、事件监听器、事件适配器?分别有哪些? 简述AWT的事件处理过程。 用AWT完成加法运算。,习题,将文本行的输入加入到文本域中,追加,

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 其他


经营许可证编号:宁ICP备18001539号-1