编写图形用户界面.ppt
《编写图形用户界面.ppt》由会员分享,可在线阅读,更多相关《编写图形用户界面.ppt(78页珍藏版)》请在三一办公上搜索。
1、第9章 编写JAVA图形界面,主要内容了解Java的GUI类研究AWT和Swing组件之间的区别创建GUI窗口使用布局管理器处理GUI事件使用按钮、标签、文本框、面板和菜单将AWT转化为Swing,了解Java的GUI类,GUI(Graphics User Interface)按钮、复选框、标签和其他简单组件文本域、滑动条以及其他复杂的组件下拉菜单和弹出菜单窗口、对话框和Applet窗口,AWT和Swing组件,AWT:抽象窗口工具集(Abstract Window Tools),Java的早期版本,组件种类有限,java.awt.*(Label)Swing:重写AWT,扩充的AWT,40多个
2、组件,是AWT的4倍,javax.swing.*(JLabel),1.AWT的概述:用于图形用户界面的开发。AWT中的主要软件包:,使用AWT类,2.AWT的分类:1.基本组件:基本组件是图形界面的最小单位,里面不再包含其他的成分。如:按钮Button、复选框Checkbox、组合框Choice、列表List、文本域 Textfield、多行文本域Textarea、静态文本Label、滚动条Scrollbar等。2.包容器(Container):包容器是一种特殊的组件,用来包含其他组件。如:面板Panel、窗口 Windows、对话框Dialog、文件对话框 Filedialog、框架Fram
3、e等。,使用AWT类,3.AWT组件的类层次:Component类的主要方法有:void enable():使组件可用。void disable():使组件不可用。void show():显示组件。void paint():绘制图形。void repaint():刷新。所有的UI组件都可继承或重载以上方法。Container类的主要方法:void add(Component c):将指定组件c加入到容器中。void SetLayout():设置布局管理器 所有的包容器组件都可继承或重载以上方法。,使用AWT类,Java的GUI设计既可用于Java Application,也可用于Java Ap
4、plet。Java的GUI设计包括以下方面:界面上放置哪些组件,这些组件以什么样的布局放置。如何进行事件处理。,使用AWT类,框架窗口组件(Frame):Frame是独立于浏览器的可独立运行的主窗口,通常用于进行开发桌面应用程序。Frame拥有边界和标题栏设置,其大小可以进行设置调整。Frame里面需要加入组件,也可以加入菜单,或在上面绘图。Frame的创建:(1)Frame():创建一个没有窗口标题的窗口框架;(2)Frame(String):创建一个指定窗口标题的窗口框架。,窗口,import java.awt.*;public class Frame1 extends Framepubl
5、ic Frame1()this.setSize(320,150);this.setTitle(“Frame Example);this.setVisible(true);public static void main(String args)Frame1 nowFrame=new Frame1();,窗口,标签组件(Label):标签组件显示的是静态文本,在通常情况下是不能编辑的,能起到提示的作用。Label的创建:Label组件有三种构造函数:(1)Label():创建空的标签;(2)Label(String):创建一个带初始字符串的标签;(3)Label(String,int):创建一个带
6、初始字符串及指定对齐方式的标签。其中对齐方式有几种形式:Left(Label.LEFT)、Right、Center。Label默认对齐方式为左对齐。,标签组件(Label),标签组件(Label):Label的主要方法:,标签组件(Label),Label示例LabelExample.javaimport java.awt.*;public class LabelExample extends Frame public LabelExample()this.setLayout(new GridLayout(5,1);/网格布局Label l1=new Label();/创建空的标签this.a
7、dd(l1);l1.setText(no1);/设置标签内容Label l2=new Label(no2);/创建带初始内容的标签this.add(l2);Label l3=new Label(Label.LEFT,Label.LEFT);/左对齐this.add(l3);,标签组件(Label),Label示例LabelExample.javaLabel l4=new Label(Label.RIGHT,Label.RIGHT);/居中this.add(l4);Label l5=new Label(Label.CENTER,Label.CENTER);/右对齐this.add(l5);thi
8、s.setSize(320,150);this.setTitle(Label Example);this.setVisible(true);public static void main(String args)LabelExample z=new LabelExample();,标签组件(Label),标签组件(Label),文本域一般用来让用户输入如姓名、信用卡号这样的信息,它是一个能够接收用户的键盘输入的小块区域。文本域的创建:TextField():创建空的文本域。TextField(int):创建具有指定长度的文本域。TextField(String):创建带有初始文本内容的文本域。
9、TextField(String,int):创建带有初始文本内容并具有指定长度的文本域。,文本域(TextField),文本域的主要方法:,文本域(TextField),TxtExample.javaimport java.awt.*;public class TxtExample extends Frame TextField tf1,tf2,tf3,tf4;public TxtExample()this.setLayout(new FlowLayout();tf1=new TextField();/空的文本域tf2=new TextField(20);/长度为20的空的文本域tf3=new
10、 TextField(你好);/带有初始文本内容的文本域tf4=new TextField(你好,30);/带有初始文本内容并具有指定长度的文本域this.add(tf1);this.add(tf2);this.add(tf3);this.add(tf4);this.setSize(320,150);this.setTitle(Label Example);this.setVisible(true);public static void main(String args)TxtExample txt=new TxtExample();,文本域(TextField),是一种交互能力强而交互方便的
11、控件,这个组件提供了“按下并动作”的基本用户界面。创建:(1)Button():建立一个没有标示字符串的新按钮类对象。(2)Button(String label):建立一个标示字符串为label的新按钮。按钮的主要方法:,按钮(Button),import java.awt.*;public class ButtonExample extends Framepublic ButtonExample()this.setLayout(new FlowLayout();Button button1=new Button();button1.setLabel(button 1);Button but
12、ton2=new Button(button 2);this.add(button1);this.add(button2);this.setSize(320,150);this.setTitle(Button Example);this.setVisible(true);public static void main(String args)ButtonExample b=new ButtonExample();,按钮(Button),事件是用户已经采取了某些操作的信号也是MouseEvent,WindowEvent,ActionEvent之类的实例,事件处理,事件源,事件监听器,事件处理三要
13、素事件源、事件类型、事件监听器为事件处理的三要素。1.事件源 事件源是一个事件的产生者,如按钮、窗口、文本域等等。2.事件类型 Java中所有的事件都封装成一个类,这些事件类被集中在包和包中,所有的事件类均继承了AWTEvent类,所有的事件类均继承了一个方法getSouce()方法,该方法返回发生事件的对象。3.事件监听器 不同的类型事件发生后,由事件监听器接收事件并调用相应的事件处理方法。所有的事件监听器实际上都是一个包中的接口,继承了接口。不同事件类型的监听器具有不同的方法。,事件处理,事件处理模型:事件被送往产生这个事件的组件,每一个组件注册一个或多个称为监听器的类,这些类包含事件处理
14、器,用来接收和处理这些事件。采用这种方法,事件处理器可以安排在与源组件分离的对象中。监听器就是实现了Listener接口的类。实现Listener接口的类可以被注册为一个监听器。事件是只向注册的监听器报告的对象。每个事件都有一个对应的监听器接口,,事件处理,在Button对象上用鼠标进行点击时,将发送一个ActionEvent事件。这个ActionEvent事件会被使用addActionListener()方法进行注册的所有ActionListener的actionPerformed()方法接收。ActionEvent类的getActionCommand()方法返回与动作相关联的命令名称。以按
15、钮的点击动作为例,将返回Button的标签。,事件处理,事件处理,包含GUI事件处理的应用程序必须由以下几个步骤:第一步,程序加入包。Import;第二步,给所需的事件源对象注册事件监听器。事件源对象.addXXXListener(XXXListener);如果所在的容器主类已经实现了相应的事件监听器接口,即class XXXX extends YYYYYY implements ZZZListener则addXXXListener()的参数为this,否则需新建一个XXXListener类或其继承类。第三步,实现相应的方法。如果某个监听器接口包含多个方法,则需要实现所有的方法。,事件处理,点
16、击按钮所发生的事件为动作事件,这是一种最常用的事件处理。动作事件对应的事件类是ActionEvent类,该类的主要方法见下图:,事件处理-按钮点击,ActionListener的主要方法:,实现动作事件的操作过程是:第一步,注册动作事件监听器addActionListener(ActionListener)。第二步,实现ActionListener接口的方法:actionPerformed(ActionEvent e)。,事件处理-按钮点击,实现过程:1修改ButtonExample,变成监听器public class ButtonExample extends Frame implement
17、s ActionListener2添加监听器,addActionListenerbutton1.addActionListener(this);button2.addActionListener(this);3添加事件接口方法,actionPerformed(ActionEvent e)public void actionPerformed(ActionEvent e)/TODO Auto-generated method stubif(e.getSource()=button1)System.out.println(button 1 has been pressed);if(e.getSou
18、rce()=button2)System.out.println(button 2 has been pressed);,事件处理-按钮点击,窗口关闭需要响应窗口事件的接口命令WindowListener1修改ButtonExample,变成窗口监听器public class ButtonExample extends Frame implements ActionListener,WindowListener2添加监听器,addActionListenerthis.addWindowListener(this);3添加事件接口方法public void windowActivated(Win
19、dowEvent e)public void windowClosed(WindowEvent e)public void windowClosing(WindowEvent e)System.exit(0);public void windowDeactivated(WindowEvent e)public void windowDeiconified(WindowEvent e)public void windowIconified(WindowEvent e)public void windowOpened(WindowEvent e),事件处理-窗口事件,扩充适配器类:可实现监听器接口
20、并重写具有空方法的所有接口方法通过使用扩充适配器类不再需要重写7个接口方法解决方案:创建2个单独的类,事件处理-窗口事件,实现方法1添加扩充适配器类(ButtonExample.java)class WindowCloser extends WindowAdapterButtonExample frametoclose;public WindowCloser(ButtonExample frame)frametoclose=frame;public void windowClosing(WindowEvent e)frametoclose.shutDown();2添加监听器()WindowCl
21、oser eHandle=new WindowCloser(this);this.addWindowListener(eHandle);,事件处理-窗口事件,import java.awt.*;import.*;public class AWTFrameWithAButton extends Frame implements ActionListenerpublic AWTFrameWithAButton()Button closeButton=new Button(Close);this.setLayout(new FlowLayout();this.add(closeButton);thi
22、s.setSize(300,150);this.setTitle(AWT Frame With A Button);this.setVisible(true);closeButton.addActionListener(this);WindowCloser eventHandler=new WindowCloser(this);this.addWindowListener(eventHandler);public void actionPerformed(ActionEvent e)shutDown();,事件处理-窗口事件,public void shutDown()this.dispose
23、();System.exit(0);public static void main(String args)/create instance of Frame AWTFrameWithAButton frameWithButton=new AWTFrameWithAButton();class WindowCloser extends WindowAdapterAWTFrameWithAButton frameToClose;public WindowCloser(AWTFrameWithAButton frame)frameToClose=frame;public void windowCl
24、osing(WindowEvent event)frameToClose.shutDown();,事件处理-窗口事件,创建内部类:是在另一个类中定义的类重要好处:可以扩充不同于外部类的类关键字:new编译结果:两个class文件,一个是ButtonExample.class,另一个是ButtonExample$1.class,事件处理-窗口事件,实现方法在ButtonExample的构造函数中加入this.addWindowListener(new WindowAdapter()public void windowClosing(WindowEvent event)shutDown(););,
25、事件处理-窗口事件,关系,事件处理-窗口事件,触发鼠标事件的事件源通常是一个容器,当鼠标进入、离开容器,或者在容器中单击鼠标、拖动鼠标等操作时,都发生鼠标事件。鼠标事件对应的事件类是MouseEvent类,该类的主要方法:,事件处理-鼠标事件,鼠标事件对应的事件监听器有两个:MouseListener(或者MouseAdapter)对应鼠标事件,MouseMontionListener(或者MouseMontionAdaper)对应鼠标移动事件。MouseListener类的主要方法,MouseMontionListener的主要方法,事件处理-鼠标事件,实现鼠标事件的操作过程是:注册Mous



- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 编写 图形 用户界面

链接地址:https://www.31ppt.com/p-6373970.html