收藏 分享(赏)

Java-AWT详解与用法.docx

上传人:weiwoduzun 文档编号:3612259 上传时间:2018-11-13 格式:DOCX 页数:30 大小:2.98MB
下载 相关 举报
Java-AWT详解与用法.docx_第1页
第1页 / 共30页
Java-AWT详解与用法.docx_第2页
第2页 / 共30页
Java-AWT详解与用法.docx_第3页
第3页 / 共30页
Java-AWT详解与用法.docx_第4页
第4页 / 共30页
Java-AWT详解与用法.docx_第5页
第5页 / 共30页
点击查看更多>>
资源描述

1、目录GUI 图形用户界面 .2AWT 的基础知识 .2AWT 的事件处理 .2事件处理机制 2事件分类 .3事件监听器 .4事件适配器 .5灵活设计事件监听器类 5用匿名内部类实现事件监听器 7事件处理的多重运用 8修改组件的默认事件处理方式 9常用的 AWT 组件 10Button 11Label.12TextComponent 文本框 13TextField.13TextArea.14Checkbox15Choice 下拉列表框 17List18Canvas19菜单 .19容器 Container 21Panel 类和 ScrollPane22布局管理器 .23BorderLayout 布

2、局管理器 .23FlowLayout.24GridLayout25CardLayout .25取消布局管理器 .26GUI 组件上的图形操作 26Graphics 类与图形绘制 26组件重绘 .27图像显示 .29双缓冲技术 .30GUI 图形用户界面AWT 的基础知识AWT 事件处理GUI 组件上的图形操作常用的 AWT 组件布局管理器AWT 的基础知识GUI(Graphical user interface,即图形用户界面) ,应用程序提供给用户操作的图形用户界面,包括菜单、窗口、按钮、工具栏、和其他各种屏幕元素。在 JDK 中提供了 AWT 和 Swing 两个包,用于 GUI 程序的设

3、计和开发。AWT 是早期版本,swing 是对 awt 的改进,提供了更加丰富的组件和功能,掌握了 awt 基本上就掌握了 swing示例:import java.awt.*;public class Test1 public static void main(String args) Frame f = new Frame(“第一个GUI程序“);f.setVisible(true); /设置窗口可见f.setLocation(300, 300); /设置窗口的左上角坐标f.setSize(300, 250); /设置窗口的大小/ f.setBounds(300, 300, 300, 250

4、);/设置窗口的坐标和大小/ f.setIconImage(image); /设置窗体左上角的图标/ f.setResizable(false); /设置窗口是否可以改变大小/ f.setMenuBar(mb); /设置窗口的菜单栏/ f.dispose(); /释放窗口及其组件的所有资源,关闭窗口,结束 AWT线程程序的 GUI 部分由 AWT 线程管理,只要程序中有线程存在,则程序是不会结束的。修改程序,在程序末尾添加 dispose 方法的调用。AWT 的事件处理事件处理机制通过 GUI 组件用户可以对应用程序进行各种操作,反之,应用程序可以通过各种组件来收集用户的操作信息,比如移动鼠标

5、,按下键盘等,GUI 只是提供给用户操作的图形界面,但 GUI 本身并不对用户的操作结果负责,比如单击按钮,程序并不知道在单击按钮时应该做出什么样的反应,如果想要单击按钮时程序响应某种功能,我们就必须编写相应的程序代码。上一节的程序中在我们点击关闭按钮时并不能关闭窗口和结束程序,只能通过结束进程的方式结束。Java 设计了一种专门的方式称为事件处理机制,来处理程序和用户之间的交互。在事件处理机制中需要了解 3 个重要的概念1、 事件:用户对组件的操作称为事件2、 事件源:发生事件的组件就是事件源3、 事件处理器:某个 java 类中负责处理事件的成员方法。 (我们要对事件进行处理肯定是编写程序

6、代码,当发生了某种事件,只要调用写好的程序代码即可)事件源、事件、事件处理器之间的工作关系:外部作用生成事件对象把事件对象传入事件处理器事件监听器注册组件(事件源)事件处理器事件对象2341对某个事件的处理的方法必须是 java 中的某个类的成员方法,这个类就称为事件监听器类,一个事件监听器对象必须先跟某个事件源进行关联,关联的过程称为监听器向事件源注册监听,这样当组件发生事件时,就知道该找哪个事件监听对象中的哪个方法了。注册完后,当组件接收到外部的作用,组件就会产生事件对象,并将事件对象传递给事件处理器来处理,在事件处理器中,只要打开事件对象看事件对象中的信息就知道发生了什么样的事情和事情的

7、一些细节,比如发生事件时鼠标的位置。事件分类事件用于描述发生了什么事情。我们根据这些事情给事件进行分类,事件对象就是事件类的实例对象。按产生事件的物理操作和 GUI 组件的表现效果进行分类:举例:MouseEventWindowEventActionEvent 事件的基本操作,例如按钮操作,不考虑是鼠标的单击还是快捷键时就可以使用 ActionEvent如果事件对应的监听器中的成员方法仅有一个,就可以称为是语义事件,是高级事件,如果事件对应的监听器中的成员方法有多种方法即事件发生的每一种情况都有对应的方法处理,就是低级事件事件监听器一个事件监听器对象负责处理一类事件,这一类事件的每一种情况的发

8、生分别由事件监听器对象中的一个方法来具体处理在事件源和事件监听器对象中进行约定的接口类就称为事件监听器接口事件监听器接口类的名称与事件类的名称是相对应的,例如,MouseEvent 事件类的监听器接口名为MouseListener。编写示例:实现关闭窗口的事件处理,讲解用不同层次的事件类型来表示同一个事件源对象。public class Test2 public static void main(String args) Frame f = new Frame(“第二个GUI程序“);f.setVisible(true); f.setBounds(300, 300, 300, 250);f.a

9、ddWindowListener(new MyWindowListener();/注册事件监听器Button b = new Button(“按钮“);f.add(b);System.out.println(“end“);/编写事件监听器类。点击关闭按钮时,调用我的事件监听器中的代码处理窗口的关闭class MyWindowListener implements WindowListenerpublic void windowOpened(WindowEvent e) public void windowClosing(WindowEvent e) / 正准备关闭时发生的事件Window w

10、= e.getWindow();/返回哪个窗口发生的事件w.setVisible(false);w.dispose();System.exit(0);/关闭程序/ e.getSource();/发生事件的来源/ e.getComponent();/发生事件的组件/ e.getWindow();/发生事件的窗口/上面三条调用语句就是通过不同的层次的事件类型来表示同一个事件源对象public void windowClosed(WindowEvent e) /关闭后响应的事件public void windowIconified(WindowEvent e) public void windowD

11、eiconified(WindowEvent e) public void windowActivated(WindowEvent e) public void windowDeactivated(WindowEvent e) 总结:处理发生在某个 GUI 组件上的 XXXEvent 事件的某种情况,其事件处理的通用编写流程:1、编写一个事件了 XXXListerner 接口的事件监听器2、 XXXListener 类中的用于处理该事件情况的方法中,编写处理代码3、调用组件的 addxxxListener 方法,将 XXXListener 创建的实例对象注册到 GUI 组件上。事件适配器为了简

12、化事件监听器接口实现类的编程,java 提供了大多数事件监听器接口的简单的实现类,称为事件适配器(Adapter)类。适配器类简单的实现了监听器接口中的所有的方法。用事件适配器来处理事件,可以简化事件监听器的编写,因为一个类只要实现了适配器的方法,就等于实现了监听器接口。如果要对某类监听器的某类情况进行处理只要在适配器的处理类中覆盖处理这种事件的方法就可以了。示例:使用事件适配器来关闭窗口public class Test3 public static void main(String args) Frame f = new Frame(“第三个GUI程序“);f.setVisible(tru

13、e); f.setBounds(300, 300, 300, 250);f.addWindowListener(new MyWindowListener();/注册事件监听器Button b = new Button(“按钮“);f.add(b);System.out.println(“end“);/编写事件适配器类。class MyWindowListener extends WindowAdapterpublic void windowClosing(WindowEvent e) / 正准备关闭时发生的事件Window w = e.getWindow();/返回哪个窗口发生的事件w.set

14、Visible(false);w.dispose();System.exit(0);/关闭程序初学者使用事件适配器时的常见问题:是方法没有调用还是方法中的程序代码的执行问题。如果是没有调用,就要注意是方法名写错了,还是没有注册事件监听器。事件适配器的不足:不能在继承其他类了灵活设计事件监听器类如果要在事件监听器类中访问非事件源的其他 GUI 组件,程序该如何编写。例如在上述的 Frame 中要单击button 按钮来关闭窗口,即事件源是 Button,但是在事件处理程序中要访问 frame 将框架窗口关闭。public class Test4 implements ActionListener

15、Frame f = new Frame(“第四个GUI程序“);public static void main(String args) Test4 t = new Test4();t.f.setVisible(true); t.f.setBounds(300, 300, 300, 250);Button b = new Button(“按钮“);t.f.add(b);b.addActionListener(t);System.out.println(“end“);public void actionPerformed(ActionEvent e) f.setVisible(false);f.

16、dispose();/ System.exit(0);/这种方法不能解决,所以给上面的框架类设置为事件监听器class MyActionLinster implements ActionListenerpublic void actionPerformed(ActionEvent e) / e.getSource();/获得的是注册事件监听器的按钮/ f.dispose();/找不到框架使用内部类实现public class Test5Frame f = new Frame(“第五个GUI程序“);public static void main(String args) Test5 t = n

17、ew Test5();t.f.setVisible(true); t.f.setBounds(300, 300, 300, 250);Button b = new Button(“按钮“);t.f.add(b);b.addActionListener(t.new MyActionListener();System.out.println(“end“);class MyActionListener implements ActionListenerpublic MyActionListener()public void actionPerformed(ActionEvent e) f.setVi

18、sible(false);f.dispose();用匿名内部类实现事件监听器匿名内部类具有以下特点:1, 匿名内部类本身没有构造方法,但是会调用父类的构造方法。2, 匿名内部类中尽管没有构造方法,但是在匿名类中可以提供一段实例初始化代码。Java 虚拟机会在调用父类构造方法之后执行这一段代码。3, 除了可以在外部类的方法中定义匿名类意外,还可以在声明一个成员变量时定义匿名类。4, 匿名类除了可以继承类以外还可以实现接口。5, 匿名类和局部类一样,可以访问外部类的所有成员。如果匿名类处于一个方法中,还能访问所在方法的 final 成员和参数。6, 局部内部类的名字在方法外是不可见的。因此与匿名类

19、一样,能够起到封装类型名字的作用。内部类的用途:1, 封装类型;2, 直接访问外部类的成员;3, 回调外部类的方法。public class Test6Frame f = new Frame(“第六个GUI程序“);public static void main(String args) final Test6 t = new Test6();t.f.setVisible(true); t.f.setBounds(300, 300, 300, 250);Button b = new Button(“按钮“);t.f.add(b);b.addActionListener(new ActionLi

20、stener()public void actionPerformed(ActionEvent e) t.f.setVisible(false);t.f.dispose(););System.out.println(“end“);事件处理的多重运用用户的一个操作在触发了一个低级操作的同时,可能也触发了语义事件(高级事件) ,例如在一个按钮上按下了鼠标既触发了鼠标事件也触发了按钮的事件,程序应该根据实际需要选择处理鼠标事件还是按钮事件也可以同时处理鼠标和按钮事件,如果在鼠标单击时希望能够处理按钮事件的同时,在鼠标按下时按钮显示一种标题,鼠标弹起时恢复原先标题。就需要在按钮上注册多个事件监听器,一

21、个处理鼠标事件一个处理按钮的动作事件。(一个 GUI 组件到底能够触发哪几种事件,可以通过 API 文档或集成开发环境查阅。 )一个组件上的一个动作可以产生多种不同类型的事件外部作用组件(事件源)事件对象1事件对象 2事件处理器 1事件处理器 2事件处理器同样,一个事件监听器对象可以注册到多个事件源上外部作用事件对象组件 1(事件源 1)组件 2(事件源 2)事件处理器例如有三个按钮的单击事件可以同时响应一个事件处理在一个事件源上可以注册对同一类事件进行处理的多个事件监听器对象外部作用事件对象组件(事件源) 事件处理器 2事件处理器 1修改组件的默认事件处理方式1、一般情况下,如果移动鼠标到一

22、个组件或一个窗口上,组件或窗口对此毫无反应,这是因为组件的默认处理方式屏蔽了对所有事件的响应。不管发生什么事情事件都不会在组件上发生,即组件不会产生任何事件对象。只有在一个组件上注册了某种事件的事件监听器对象后,组件才会产生相应的事件对象。2、当一个组件上发生了某种事件后,系统就会调用组件对象的 processEvent 方法来处理即默认调用相应的processXXXEvent 方法。然后再调用相应的监听器方法。如果想修改组件的处理方法就需要覆盖processEvent 方法或者 procesXXXEvent 方法。processEvent 是处理所有事件的总入口,procesXXXEvent

23、 是处理某种事件的入口。可以通过继承某组件的方式来重写 processEvent,3、调用 enableEvents(long enentsToEnable)方法,可以在即使没有注册事件监听器的情况下,组件也能够对某些类型的事件进行响应和产生相应的事件对象。为了方便描述每一个事件类型,java 给每个事件类型定义了单独的数字来表示,只需要将多个需要实现的事件相或即可。编程实例:在窗口上显示一个按钮,一旦鼠标移动到这个按钮上时,按钮就移动到其他位置上,这样,鼠标就永远无法点击到这个按钮。public class Test2 extends Frame public Test2()addWindo

24、wListener(new WindowAdapter() public void windowClosing(WindowEvent e)dispose();System.exit(0););public static void main(String args) Test2 mainFrame = new Test2();MyButton bt1 = new MyButton(“main“);MyButton bt2 = new MyButton(“friend“);bt1.friend = bt2;bt2.friend = bt1;mainFrame.add(bt1,“North“);m

25、ainFrame.add(bt2,“South“);mainFrame.setSize(400, 400);mainFrame.setTitle(“TestMyButton“);mainFrame.setVisible(true);/只能放在主窗口后面,否则下面的方法无法预料bt2.setVisible(false);class MyButton extends ButtonMyButton friend = null;public MyButton(String title)super(title);/本身按钮不会有响应,所以要调用该方法将响应事件添加进去this.enableEvents(

26、AWTEvent.MOUSE_MOTION_EVENT_MASK);protected void processMouseMotionEvent(MouseEvent t)setVisible(false);friend.setVisible(true);常用的 AWT 组件java 的图形用户界面的最基本的组成部分就是组件,组件是一个可以以图形化的方式显示在屏幕上,并能与用户进行交互的对象,例如一个按钮一个标签等。抽象类 Component 是所有组件的父类,规定了 gui 组件的基本特性,基本功能。MenuComponent 是所有菜单的父类.Button构造:public Button(

27、); public Button(String label);常用方法:Void setLabel(String label)设置按钮的标记String getLabel() 获得按钮的标记Void addActionListener(ActionListener) 将参数 1 对象指定为按钮的监听者Void removeActionListener(ActionListener) 将参数 1 对象从监听者中去掉按钮标签单行文本域多行文本域选择框下拉列表框列表空白区域滚动条容器面板带滚动条的面板Swing 组件的基类框架对话框 文件对话框LabelTextComponent 文本框TextFi

28、eldpublic class Test extends Frame implements ActionListenerTextField tf1 = new TextField(“请输入名字“ );TextField tf2 = new TextField();public static void main(String args) final Test mainFrame = new Test();mainFrame.setBounds(300,300,300, 300);mainFrame.setTitle(“测试程序“);mainFrame.add(mainFrame.tf1,“Nor

29、th“);mainFrame.tf1.addActionListener(mainFrame);mainFrame.tf2.setEchoChar(*);mainFrame.add(mainFrame.tf2,“South“);mainFrame.tf2.addActionListener(mainFrame);mainFrame.addWindowListener(new WindowAdapter() public void windowClosing(WindowEvent e)mainFrame.dispose();System.exit(0););mainFrame.setVisib

30、le(true);/* private static void addTextField(Test mainFrame) final TextField tf1 = new TextField(“请输入名字“);tf1.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) System.out.println(e.getActionCommand();tf1.setText(“););mainFrame.add(tf1,“North“);final TextField tf2 = ne

31、w TextField();tf2.setEchoChar(*);tf2.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) System.out.println(e.getActionCommand();tf2.setText(“););mainFrame.add(tf2,“South“);*/public void actionPerformed(ActionEvent e) if(e.getSource() = tf1)System.out.println(“tf1“+e.ge

32、tActionCommand();tf1.setText(“);if(e.getSource() = tf2)System.out.println(“tf2“+e.getActionCommand();tf2.setText(“);TextAreapublic class Test extends Frame implements KeyListenerTextArea ta = new TextArea(5, 30);public static void main(String args) final Test mainFrame = new Test();mainFrame.setBoun

33、ds(300,300,300, 150);mainFrame.setTitle(“测试程序“);mainFrame.add(mainFrame.ta);mainFrame.ta.addKeyListener(mainFrame);mainFrame.addWindowListener(new WindowAdapter() public void windowClosing(WindowEvent e)mainFrame.dispose();System.exit(0););mainFrame.setVisible(true);public void keyTyped(KeyEvent e)

34、public void keyReleased(KeyEvent e) public void keyPressed(KeyEvent e) if(e.getKeyChar() = n)TextArea ta = (TextArea)e.getSource();System.out.println(ta.getText();Checkbox用来建立单选按钮和多选按钮(复选框)创建多选按钮,只需要使用构造函数 Checkbox(String label,Boolean state)创建单选按钮,使用构造函数 Checkbox(String label,Boolean state,Checkbox

35、Group group)单选按钮和多选按钮的语义事件为 ItemEvent,对应的监听器接口为 ItemListener,该接口中只有一个itemStateChanged 方法编写实例:创建一个多选按钮和两个属于同一组的单选按钮,并对每个按钮的选中情况进行处理public class TestMenuBar extends FrameCheckbox cb1 = new Checkbox(“你喜欢我吗?“,true );CheckboxGroup cbg = new CheckboxGroup();Checkbox cb2 = new Checkbox(“喜欢“,true, cbg);Chec

36、kbox cb3 = new Checkbox(“不喜欢“,false,cbg);public TestMenuBar()FlowLayout fl = new FlowLayout();/流式布局this.setLayout(fl);add(cb1); add(cb2); add(cb3);addWindowListener(new WindowAdapter() public void windowClosing(WindowEvent e)dispose();System.exit(0););public static void main(String args) TestMenuBar

37、 mainFrame = new TestMenuBar();mainFrame.setSize(400, 400);mainFrame.setVisible(true);如果程序想了解用户对按钮操作的结果,就需要为这些按钮添加事件处理监听器。public class TestMenuBar extends FrameCheckbox cb1 = new Checkbox(“你喜欢我吗?“,true );CheckboxGroup cbg = new CheckboxGroup();Checkbox cb2 = new Checkbox(“喜欢“,true, cbg);Checkbox cb3

38、 = new Checkbox(“不喜欢“,false,cbg);class CbItemListener implements ItemListenerpublic void itemStateChanged(ItemEvent e) /由于是一个监听器监听多个事件源,所以需要对事件源进行分析判断Checkbox cb = (Checkbox)e.getItemSelectable();/返回事件的发生者String label = cb.getLabel();if(label.equals(“你喜欢我吗?“)if(cb.getState()=true)System.out.println(

39、“我很高兴 “);elseSystem.out.println(“我很伤心 “);else Checkbox c= cbg.getSelectedCheckbox();if(c != null)System.out.println(c.getLabel();public TestMenuBar()FlowLayout fl = new FlowLayout();/流式布局this.setLayout(fl);add(cb1);add(cb2);add(cb3);CbItemListener cbi = new CbItemListener();cb1.addItemListener(cbi);

40、cb2.addItemListener(cbi);cb3.addItemListener(cbi);addWindowListener(new WindowAdapter() public void windowClosing(WindowEvent e)dispose();System.exit(0););public static void main(String args) TestMenuBar mainFrame = new TestMenuBar();mainFrame.setSize(400, 400);mainFrame.setVisible(true);Choice 下拉列表

41、框Choice 类的语义事件为 ItemEvent,对应的监听器接口为 ItemListener,该接口中只有一个itemStateChanged 方法。编程实现下拉列表框。public class TestChoice extends FrameChoice ch = new Choice();class ChItemListener implements ItemListenerpublic void itemStateChanged(ItemEvent e) System.out.println(e.getItem();/返回选择的是哪个条目public TestChoice ()Flo

42、wLayout fl = new FlowLayout();/流式布局this.setLayout(fl);ch.add(“choice1“);ch.add(“choice2“);ch.add(“choice3“);add(ch);ch.addItemListener(new ChItemListener();addWindowListener(new WindowAdapter() public void windowClosing(WindowEvent e)dispose();System.exit(0););public static void main(String args) Te

43、stChoice mainFrame = new TestChoice ();mainFrame.setSize(400, 400);mainFrame.setVisible(true);Listpublic class Test extends Frame implements MouseListenerList list = new List();public static void main(String args) final Test mainFrame = new Test();mainFrame.setBounds(300,300,300, 150);mainFrame.setT

44、itle(“测试程序“);mainFrame.addWindowListener(new WindowAdapter() public void windowClosing(WindowEvent e)mainFrame.dispose();System.exit(0););String str = “java“,“c#“,“C+“,“adriod“;for(int i=0; i ll = new LinkedList();private int orgX; private int orgY;private int endX; private int endY;public void pain

45、t(Graphics g)/只能使用参数对象,不能自己产生对象for(MyLine my: ll)draw(g,my);public DrawLine()addWindowListener(new WindowAdapter() public void windowClosing(WindowEvent e)dispose();System.exit(0););/添加鼠标监听器对象this.addMouseListener(new MouseAdapter() public void mousePressed(MouseEvent e)orgX = e.getX(); orgY = e.get

46、Y();public void mouseReleased(MouseEvent e)endX = e.getX();endY = e.getY();Graphics g = getGraphics();ll.add(new MyLine(orgX,orgY,endX,endY);repaint(););public void draw(Graphics g,MyLine my)g.setColor(Color.RED);g.setFont(new Font(null,Font.ITALIC|Font.BOLD,30);/斜体和黑体g.drawString(“(“+my.getOrgX()+“

47、,“+my.getOrgY()+“)“, my.getOrgX(), my.getOrgY();g.drawString(“(“+my.getEndX()+“,“+my.getEndY()+“)“,my.getEndX(),my.getEndY();g.drawLine(my.getOrgX(), my.getOrgY(), my.getEndX(),my.getEndY();public static void main(String args) System.out.println(“Starting DrawLine“);DrawLine mainFrame = new DrawLine

48、();mainFrame.setSize(400, 400);mainFrame.setTitle(“DrawLine“);mainFrame.setVisible(true);class MyLine private int orgX;private int orgY;private int endX;private int endY;public MyLine(int orgX, int orgY, int endX, int endy) super();this.orgX = orgX;this.orgY = orgY; this.endX = endX;this.endY = endy;public int getOrgX() return orgX; public int getOrgY() return orgY; public int getEndX() return endX; public int getEndY() return endY; 图像显示Gui 组件上显示图像使用 Graphics.drawImage(Image img, int x, int y, ImageOb

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

当前位置:首页 > 网络科技 > Java

本站链接:文库   一言   我酷   合作


客服QQ:2549714901微博号:道客多多官方知乎号:道客多多

经营许可证编号: 粤ICP备2021046453号世界地图

道客多多©版权所有2020-2025营业执照举报