编写图形用户界面.ppt

上传人:牧羊曲112 文档编号:6373970 上传时间:2023-10-21 格式:PPT 页数:78 大小:818.50KB
返回 下载 相关 举报
编写图形用户界面.ppt_第1页
第1页 / 共78页
编写图形用户界面.ppt_第2页
第2页 / 共78页
编写图形用户界面.ppt_第3页
第3页 / 共78页
编写图形用户界面.ppt_第4页
第4页 / 共78页
编写图形用户界面.ppt_第5页
第5页 / 共78页
点击查看更多>>
资源描述

《编写图形用户界面.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

26、eListener监听器。addMouseListener();当事件源所在的类已经实现MouseListener接口时,参数可用this,否则需创建或继承一个MouseListener。实现MouseListener接口的所有方法,即MousePressed(MouseEvent e)MouseReleased(MouseEvent e)MouseEntered(MouseEvent e)MouseExited(MouseEvent e)MouseClicked(MouseEvent e),事件处理-鼠标事件,import.*;import java.awt.*;public class M

27、ouseExample extends Frame implements MouseListener Label l1=new Label(init);public MouseExample()this.setLayout(new FlowLayout();add(l1);addMouseListener(this);public void mousePressed(MouseEvent e)int x=(int)e.getX();int y=(int)e.getY();l1.setText(在(+x+,+y+)处鼠标按下!);public void mouseReleased(MouseEv

28、ent e)int x=(int)e.getX();int y=(int)e.getY();l1.setText(在(+x+,+y+)处鼠标释放!);,事件处理-鼠标事件,public void mouseEntered(MouseEvent e)l1.setText(鼠标进入!);public void mouseExited(MouseEvent e)l1.setText(鼠标离开!);public void mouseClicked(MouseEvent e)if(e.getClickCount()=2)l1.setText(鼠标双击!);else l1.setText(鼠标点击!);,

29、事件处理-鼠标事件,实现鼠标移动事件的操作过程是:第一步,注册MouseMotionListener监听器。addMouseMotionListener();当事件源所在的类已经实现MouseMotionListener接口时,参数可用this,否则需创建或继承一个MouseMotionListener。第二步,实现MouseMotionListener接口的所有方法,即MouseMoved(MouseEvent e)MouseDraged(MouseEvent e)。,事件处理-鼠标事件,import.*;import java.awt.*;public class MouseDraw ex

30、tends Frame implements MouseMotionListener int x=-1,y=-1,con=3;public MouseDraw()this.setSize(300,300);this.setVisible(true);this.addMouseMotionListener(this);this.addWindowListener(new WindowAdapter()public void windowClosing(WindowEvent event)System.exit(0););,事件处理-鼠标事件,public void paint(Graphics

31、g)g.fillOval(x,y,con,con);public void mouseDragged(MouseEvent e)x=(int)e.getX();y=(int)e.getY();repaint();public void mouseMoved(MouseEvent e)public void update(Graphics g)paint(g);public static void main(String args)MouseDraw frame=new MouseDraw();,事件处理-鼠标事件,在具有键盘焦点的组件中按下或释放键盘等操作时,都发生键盘事件。键盘事件对应的事件

32、类是KeyEvent类,该类的主要方法:,事件处理-键盘事件,键盘事件对应的事件监听器为:KeyListener或KeyAdapter。KeyListener的主要方法:,事件处理-键盘事件,实现键盘事件的操作过程是:注册KeyListener监听器,addKeyListener();当事件源所在的类已经实现KeyListener接口时,参数可用this,否则需创建或继承一个KeyListener。实现KeyListener接口的所有方法,即KeyPressed(KeyEvent e)KeyReleased(KeyEvent e)KeyTyped(MouseEvent e),事件处理-键盘事件

33、,import java.awt.*;import.*;public class keyListener extends Frame implements KeyListener TextField txt=new TextField();public Label l1=new Label();public keylistener()setLayout(new FlowLayout();txt.addKeyListener(this);add(txt);add(l1);this.setSize(400,300);this.setTitle(Key Listener);this.setVisib

34、le(true);,事件处理-键盘事件,int nKeycode;public void keyPressed(KeyEvent e)nKeycode=e.getKeyCode();l1.setText(键盘按下的键为:+e.getKeyText(nKeycode);public void keyReleased(KeyEvent e)nKeycode=e.getKeyCode();l1.setText(键盘释放的键为:+e.getKeyText(nKeycode);public void keyTyped(KeyEvent e),事件处理-键盘事件,public static void ma

35、in(String args)keylistener nowframe=new keylistener();nowframe.addWindowListener(new WindowAdapter()public void windowClosing(WindowEvent e)System.exit(0););,事件处理-键盘事件,1.框架窗口组件(Frame):Frame是独立于浏览器的可独立运行的主窗口,通常用于进行开发桌面应用程序。Frame拥有边界和标题栏设置,其大小可以进行设置调整。Frame里面需要加入组件,也可以加入菜单,或在上面绘图。Frame的创建:(1)Frame():创

36、建一个没有窗口标题的窗口框架;(2)Frame(String):创建一个指定窗口标题的窗口框架。,窗口及菜单设计,import.*;import java.awt.*;public class Frame1 extends FrameLabel l1;TextField txt1;Label l2;Button b1;Button b2;Label l3;public Frame1()setTitle(Frame1);/设置窗口的标题setLayout(new FlowLayout();/设置布局l1=new Label(1+1=);add(l1);txt1=new TextField(1);

37、add(txt1);b1=new Button(ok);add(b1);b2=new Button(cancel);add(b2);,窗口及菜单设计,l2=new Label(Your answer is:);add(l2);l3=new Label(init);add(l3);/以上为在Frame上放置多个组件setResizable(false);/设置窗口不可缩放setVisible(true);public static void main(String args)Frame1 nowFrame=new Frame1();nowFrame.addWindowListener(new W

38、indowAdapter()/窗口事件处理 public void windowClosing(WindowEvent e)System.exit(0););,窗口及菜单设计,2.菜单组件由菜单栏MenuBar、菜单(Menu)和菜单项三部分组成。一个菜单栏由若干个菜单组成,一个菜单又由若干个菜单项组成。菜单栏MenuBar:(1)MenuBar():创建菜单栏。(2)MenuBar的主要方法:,窗口及菜单设计,菜单(Menu)(1)Menu(String):以指定标签创建菜单。(2)Menu的主要方法,窗口及菜单设计,菜单项(MenuItem)(1)MenuItem(String):以指定标

39、签创建菜单项。(2)MenuItem的主要方法,窗口及菜单设计,用Java创建菜单的过程如下:(1)创建一个菜单栏。(2)调用Frame的setMenuBar()方法将菜单栏加入Frame中。(3)分别创建若干个Menu对象,并加入MenuBar中。(4)对于每个Menu对象,分别创建若干个MenuItem对象,并加入Menu中。,窗口及菜单设计,JMenuBar menuBar=new JMenuBar();JMenu fileMenu=new JMenu(File);JMenu helpMenu=new JMenu(Help);JMenuItem displayMenu=new JMenu

40、Item(Display);JMenuItem clearMenu=new JMenuItem(Clear);JMenuItem closeMenu=new JMenuItem(Close);this.setJMenuBar(menuBar);menuBar.add(fileMenu);menuBar.add(helpMenu);fileMenu.add(displayMenu);fileMenu.add(clearMenu);fileMenu.add(closeMenu);,窗口及菜单设计,displayMenu.addActionListener(this);clearMenu.addAc

41、tionListener(this);closeMenu.addActionListener(this);public void actionPerformed(ActionEvent e)if(e.getSource()=displayMenu)displayMessage();if(e.getSource()=clearMenu)clearMessage();if(e.getSource()=closeMenu)shutDown();,窗口及菜单设计,布局管理器:用于容器类中,可以确定容器中组件的位置和大小。布局管理器接口LayoutManager:它的实现类可以自动根据运行平台的不同来布

42、置各种组件。每一个容器组件都有一个默认的布局管理器,也可以通过setLayout方法来设置其他布局管理器。一旦确定了布局管理方式,容器组件就可以使用相应的add方法加入组件。,布局管理器,Panel的默认布局。它以流式的方式,自左向右,自上而下的放置容器中的组件,即组件按每行先后从自左向右放置,一行放不下时再换行。FlowLayout的默认对齐方式为居中对齐,我们也可以手工设置为左对齐或右对齐。,FlowLayout布局管理器,FlowLayout布局管理器的创建FlowLayout():创建默认的FlowLayout布局管理器。FlowLayout(int):以指定的对齐方式创建FlowLa

43、yout布局管理器。其中参数有:FlowLayout.LEFT(左对齐)、FlowLayout.RIGHT(右对齐)、FlowLayout.CENTER(居中对齐)。,FlowLayout布局管理器,FlowLayout布局管理器的创建FlowLayout(int,int,int):以指定的对齐方式及间距创建FlowLayout布局管理器。其中第一个参数为对齐方式,第二个参数为组件间行间距,第三个参数为列间距,FlowLayout布局管理器,FlowLayout的主要方法,FlowLayout布局管理器,import java.awt.*;public class FlowLayoutExam

44、ple extends Frame Label txt=new Label(null);Button bnorth=new Button(North);Button bsouth=new Button(South);Button beast=new Button(East);Button bwest=new Button(West);,FlowLayout布局管理器,public FlowLayoutExample()this.setLayout(new FlowLayout(FlowLayout.LEFT);add(bnorth);add(bsouth);add(beast);add(bwe

45、st);add(txt);this.setSize(300,200);this.setTitle(FlowLayout Example);this.setVisible(true);public static void main(String args)FlowLayoutExample frame=new FlowLayoutExample();,FlowLayout布局管理器,创建一个形似无框线的表格,每个单元格放一个组件。GridLayout布局管理器的创建GridLayout(int rows,int cols)创建一个指定行数的栅格控制,int rows,int cols是指定的行数

46、和列数。GridLayout(int rows,int cols,int hgap,int vgap)创建一个指定行列数和间隔距离的栅格控制。Gridlayout()创建一个默认栅格控制,默认为单行单列,间隔为零,GridLayout布局管理器,this.setLayout(new GridLayout(3,2);,GridLayout布局管理器,将容器划分为东、南、西、北、中5个区域,每个区域最多能放置一个组件,通过使用定义的静态变量识别方位North,South,East,West和Center。BorderLayout布局管理器的创建BorderLayout()创建一个默认边界控制器,构

47、件间隔为零。BorderLayout(int hgap,int vgap)创建一个指定间隔的边框控制器,int hgap水平间隔,int vgap垂直间隔。,BorderLayout布局管理器,this.setLayout(new BorderLayout();add(bnorth,BorderLayout.NORTH);add(bsouth,BorderLayout.SOUTH);add(beast,BorderLayout.EAST);add(bwest,BorderLayout.WEST);add(bcenter,BorderLayout.CENTER);,BorderLayout布局管

48、理器,能够实现多个组件容器放置在同一个区域内交替显示,相当于多张卡片CardLayout布局管理器的创建CardLayout()创建一个默认的卡片控制器。CardLayout(int hgap,int vgap)创建一个指定间隔的卡片控制器,int hgap,int vgap指定的水平和垂直间隔距离。,CardLayout布局管理器,CardLayout布局管理器,import java.awt.*;public class CardLayoutExample extends Frame implements ActionListenerButton first=new Button(firs

49、t);Button second=new Button(second);Button third=new Button(third);Panel cards=new Panel();CardLayout cl=new CardLayout();public CardLayoutExample()/给三个按钮添加监听first.addActionListener(this);second.addActionListener(this);third.addActionListener(this);/设置本类(Frame)的布局管理器为BorderLayoutsetLayout(new Border

50、Layout();,Panel p=new Panel();p.setLayout(new FlowLayout();p.add(first);p.add(second);p.add(third);add(p,BorderLayout.NORTH);/设置卡片面板的布局管理器为CardLayoutcards.setLayout(cl);/给卡片面板中添加三个标签cards.add(First card,new Label(the frist one);cards.add(second card,new Label(the second one);cards.add(third card,new

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

当前位置:首页 > 生活休闲 > 在线阅读


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号