Java语言6响应用户输入.ppt

上传人:牧羊曲112 文档编号:6510135 上传时间:2023-11-07 格式:PPT 页数:46 大小:1.12MB
返回 下载 相关 举报
Java语言6响应用户输入.ppt_第1页
第1页 / 共46页
Java语言6响应用户输入.ppt_第2页
第2页 / 共46页
Java语言6响应用户输入.ppt_第3页
第3页 / 共46页
Java语言6响应用户输入.ppt_第4页
第4页 / 共46页
Java语言6响应用户输入.ppt_第5页
第5页 / 共46页
点击查看更多>>
资源描述

《Java语言6响应用户输入.ppt》由会员分享,可在线阅读,更多相关《Java语言6响应用户输入.ppt(46页珍藏版)》请在三一办公上搜索。

1、Java语言程序设计,资源与环境科学学院,第六章 响应用户输入,一个简单的界面程序,import java.awt.*;public class TestFrame public static void main(String args)Frame f=new Frame(My Frame Programming!);f.add(new Button(ok);f.setSize(300,300);f.setVisible(true);/f.dispose();/释放窗口,加上这句后窗口闪下消失,此时点击button或者点击关闭窗口都没有响应,只有最大化,最小化有响应。,AWT事件处理机制,三个

2、重要的概念事件:用户对组件的一个操作,称之为一个事件事件源:发生事件的组件就是事件源。事件处理器:某个Java类中的负责处理事件的成员方法。事件,事件源,事件处理器之间的关系,事件分类,按产生事件的物理操作和GUI组件的表现效果分类MouseEventWindowEventActionEvent 按事件的性质分类:低级事件语义事件(又叫高级事件),在中列出多种事件,如果事件的监听器中有多个方法,则为低级事件,如果只有一个方法就为语义事件。,事件监听器,一个事件监听器对像负责处理一类事件。一类事件的每一种发生情况,分别由事件监听器对象中的一个方法来具体处理。在事件源和事件监听器对象中进行约定的接

3、口类,被称为事件监听器接口。书P193 事件监听器接口类的名称与事件类的名称通常是相对应的。例如MouseEvent事件类的监听器接口名为MouseListener。,创建事件监听器类,import;/myWindowListener.java文件import java.awt.event.*;public class myWindowListener implements WindowListenerpublic void windowOpened(WindowEvent e)public void windowClosing(WindowEvent e)e.getWindow().setV

4、isible(false);(Window)e.getComponent().dispose();System.exit(0);public void windowActivated(WindowEvent e)public void windowClosed(WindowEvent e)public void windowDeactivated(WindowEvent e)public void windowDeiconified(WindowEvent e)public void windowIconified(WindowEvent e),添加事件监听器,import java.awt.

5、*;/打开上例中的TestFrame.java程序public class TestFrame public static void main(String args)Frame f=new Frame(My Frame Programming!);f.add(new Button(ok);f.setSize(300,300);f.setVisible(true);f.addWindowListener(new myWindowListener();,运行后可以顺利的关闭窗口,事件监听器编写流程,处理发生在某个GUI组件上的*Event事件的某种情况,其事件处理的通用编写流程。编写一个实现了*

6、Listener接口的事件监听器类;*Listener类中的用于处理该事件情况的方法中,编写处理代码;调用组件的add*Listener方法,将类*Listener创建的实例对象注册到GUI组件上。,事件适配器,JDK中也提供了大多数事件监听器接口的最简单的实现类,称之为事件适配器(Adapter)类。用事件适配器来处理事件,可以简化事件监听器编写。在AWT中就经常用到声明和实现多个接口。无论实现了几个接口,接口中已定义的方法必须一一实现,如果对某事件不感兴趣,可以不具体实现其方法,而用空的方法体来代替,但却必须所有方法都要写上。这样一来会有一些不便,为了解决这个问题,AWT使用了适配器(Ad

7、apter),Java语言为一些Listener接口提供了适配器类(Adapter)。可以通过继承事件所对应的Adapter类,重写所需要的方法,无关的方法则不用实现。,事件适配器,/编写一个采用适配器的YourWindowListener.java类import java.awt.event.WindowAdapter;import java.awt.event.*;public class YourWindowListener extends WindowAdapter public void windowClosing(WindowEvent e)e.getWindow().dispos

8、e();System.exit(0);,事件适配器,import java.awt.*;public class TestFrame public static void main(String args)Frame f=new Frame(My Frame Programming!);f.add(new Button(ok);f.setSize(300,300);f.setVisible(true);/f.addWindowListener(new myWindowListener();f.addWindowListener(new YourWindowListener();,事件适配器,事

9、件适配器类的不足之处:Java不允许一个类继承多个类,因此当一个类已经继承了别的类,就不能再使用事件适配器类,此时只能使用事件监听器接口。如果*Listener接口中只有一个方法,可以采用事件监听器,没有必要使用事件适配器。因此对于*Listener接口中只有一个方法,是没有事件适配器类的。,在事件监听器中访问非事件源的其他GUI组件,import java.awt.*;import java.awt.event.*;/为button按钮添加关闭窗口的事件public class TestFrame implements ActionListener public static void ma

10、in(String args)Frame f=new Frame(My Frame Programming!);f.add(new Button(ok);f.setSize(300,300);f.setVisible(true);f.addWindowListener(new YourWindowListener();public void actionPerformed(ActionEvent e)书P194 f.dispose()System.exit(0);,判断是否正确?此函数中 f 在哪里定义?Btn是否关联事件?,在事件监听器中访问非事件源的其他GUI组件,import java.

11、awt.*;import java.awt.event.*;/为button按钮添加关闭窗口的事件public class TestFrame implements ActionListener Frame f=new Frame(My Frame Programming!);public static void main(String args)TestFrame tf=new TestFrame();Button btn=new Button(ok);tf.f.add(btn);tf.f.setSize(300,300);tf.f.setVisible(true);tf.f.addWind

12、owListener(new YourWindowListener();btn.addActionListener(tf);public void actionPerformed(ActionEvent e)f.dispose()System.exit(0);,注意:该写法结构不太好,可以改写,内部类 实现事件处理,内部类(Inner Class)是被定义于另一个类中的类,使用内部类的主要原因是由于:一个内部类的对象可访问外部类的成员方法和变量,包括私有的成员;实现事件监听器时,采用内部类、匿名类编程非常容易实现其功能;编写事件驱动程序,内部类很方便。内部类所能够应用的地方往往是在AWT的事件

13、处理机制中。,在事件监听器中访问非事件源的其他GUI组件,import java.awt.*;import java.awt.event.*;/为button按钮添加关闭窗口的事件public class TestFrame Frame f=new Frame(My Frame Programming!);public static void main(String args)TestFrame tf=new TestFrame();tf.init();public void init()Button btn=new Button(ok);btn.addActionListener(new A

14、ctionListener()public void actionPerformed(ActionEvent e)f.dispose();System.exit(0););f.add(btn);f.setSize(300,300);f.setVisible(true);f.addWindowListener(new YourWindowListener();,事件处理,一个组件上的一个动作可以产生多种不同类型的事件。一个事件监听器对象可以注册到多个事件源上。在一个事件源上也可以注册对同一类事件进行处理的多个事件监听器对象。,GUI组件的图形操作,Graphics类与图形绘制,Component

15、.getGraphics方法,将返回Graphics类对象。Graphics.drawLine(int x1,int y1,int x2,int y2)方法。Graphics.drawString(String str,int x,int y)方法。注意:drawString方法的坐标参数中(x,y)为文本的左下角。编程举例:以鼠标在窗口中按下时的位置作为起始点,鼠标释放时的位置作为终止点,在鼠标释放时将直线画出,并在每条直线的起始和终止点位置上打印出它们的坐标值。,Graphics类与图形绘制,鼠标事件,任何组件都可以激发这些事件。类通过接口 MouseListener 来实现,也可以通过适

16、配器 MouseAdapter()类来实现。按下鼠标事件 mousePressed(MouseEvent e);释放鼠标事件 mouseReleased(MouseEvent e);其他方法参见 p200,Graphics类与图形绘制,import java.awt.*;import java.awt.event.*;public class DrawLine Frame f=new Frame(“绘图工具”);/指定绘图工具的画面public static void main(String args)new DrawLine().init();public void init()f.setS

17、ize(300,300);f.setVisible(true);f.addWindowListener(new WindowAdapter()public void windowClosing(WindowEvent e)e.getWindow().dispose();/添加窗口关闭事件 System.exit(0););,Graphics类与图形绘制,f.addMouseListener(new MouseAdapter()/添加鼠标事件 int orgX,orgY;public void mousePressed(MouseEvent e)orgX=e.getX();orgY=e.getY

18、();public void mouseReleased(MouseEvent e)Graphics g=f.getGraphics();g.setColor(Color.RED);g.drawLine(orgX,orgY,e.getX(),e.getY(););,g.drawString(orgX+,+orgY,orgX,orgY);g.drawString(e.getX()+,+e.getY(),e.getX(),e.getY();,g.setFont(new Font(null,Font.ITALIC|Font.BOLD,30);,组件的重绘,上面的实例当窗口最小化等操作后,图形消失了,

19、需要进行组件的重绘。paint(Graphics g)进行组件的重绘。paint()方法将图形的内容重新绘制。AWT线程对组件重绘的调用过程。Paint()方法是由AWT线程调用管理的,应用程序并不直接调用paint()方法,应用程序调用repaint()方法,由repaint()调用update()后再调用paint()方法。,组件的重绘,import java.awt.*;import java.awt.event.*;public class DrawLine extends Frame/Frame f=new Frame(“绘图工具”);/删除该语句 int orgX,orgY,end

20、X,endY;public static void main(String args)new DrawLine().init();public void init().this.addMouseListener(new MouseAdapter().public void mouseReleased(MouseEvent e)endX=e.getX();endY=e.getY();Graphics g=getGraphics();.);public void paint(Graphics g)g.drawLine(orgX,orgY,endX,endY);,只绘制了最后一条直线,所有线的重绘,

21、在上次写的 Myline类 中添加 drawMe()的方法,实现一条线的绘制工作。将鼠标绘制的每一条线都记录到Myline类中,添加一个矢量 Vector 记录所有Myline类集合。添加paint()方法,在该方法中调用 Myline类集合中的drawMe()的方法,绘制每一条线。,所有线的重绘,/编写一个线段类 MyLineimport java.awt.Point;import java.awt.*;public class myline Point pnt1;Point pnt2;/添加绘制直线的方法public void drawMe(Graphics g)g.drawLine(pn

22、t1.x,pnt1.y,pnt2.x,pnt2.y);,所有线的重绘,import java.awt.*;import java.awt.event.*;import java.util.*;public class Redrawline extends Frame Vector vLines=new Vector();public static void main(String args)new Redrawline().init();public void paint(Graphics g)g.setColor(Color.red);Enumeration e=vLines.element

23、s();while(e.hasMoreElements()myline oneline=(myline)e.nextElement();oneline.drawMe(g);,所有线的重绘,public void init()addWindowListener(new WindowAdapter()public void windowClosing(WindowEvent e)(Window)e.getSource().dispose();System.exit(0););addMouseListener(new MouseAdapter()int orgX,orgY;public void m

24、ousePressed(MouseEvent e)orgX=e.getX();orgY=e.getY();public void mouseReleased(MouseEvent e)Graphics g=e.getComponent().getGraphics();g.setColor(Color.red);g.drawLine(orgX,orgY,e.getX(),e.getY();vLines.add(new myline(orgX,orgY,e.getX(),e.getY(););this.setSize(300,300);setVisible(true);,图像显示,使用Graphi

25、cs.drawImage(Image img,int x,int y,ImageObserver observer)方法显示图像。import java.awt.*;import java.awt.event.*;public class DrawImage extends FrameImage img=null;public static void main(String args)new DrawImage().init();,X,Y左上角坐标;Observer用于监视图像创建进度的对象。,图像显示,public void init()img=this.getToolkit().getIm

26、age(c:test.gif);setSize(300,300);setVisible(true);this.addWindowListener(new WindowAdapter()public void windowClosing(WindowEvent e)dispose();System.exit(0););public void paint(Graphics g)getGraphics().drawImage(img,0,0,this);,菜单,一个完整的菜单系统由菜单条、菜单和菜单项组成,它们之间的关系如图:Java中与菜单相关的类主要有:MenuBar(菜单条)Menu(菜单)M

27、enultem(菜单项)。,菜单,import java.awt.*;import java.awt.event.*;public class TestMenuBar MenuBar menubar=new MenuBar();/创建菜单条对象Menu fileM=new Menu(File);/创建各菜单Menu editM=new Menu(Edit);/创建各菜单MenuItem fileMI1=new MenuItem(New);/创建各菜单项CheckboxMenuItem fileMI5=new CheckboxMenuItem(Quit,true);Menu filePrint=

28、new Menu(print);/创建子菜单 MenuItem printM1=new MenuItem(preview);MenuItem printM2=new MenuItem(setting);public static void main(String args)new TestMenuBar();,菜单,TestMenuBar()FlowLayout fl=new FlowLayout();Frame f=new Frame(TestMenuBar);f.setLayout(fl);menubar.add(fileM);/将菜单加入菜单条menubar.add(editM);fil

29、eM.add(fileMI1);/将菜单项加入file菜单中filePrint.add(printM1);/将菜单项加入print菜单中filePrint.add(printM2);fileM.add(filePrint);/将print菜单作为菜单项加入file菜单fileM.addSeparator();/将一条分割线加入菜单中fileM.add(fileMI5);/将菜单项加入菜单中f.setMenuBar(menubar);/把整个菜单系统显示在窗口中f.setBounds(0,0,250,200);f.setVisible(true);f.addWindowListener(new

30、WindowAdapter();,为菜单添加事件,编写一个MenuListener类,作为菜单事件响应类。import java.awt.event.ActionEvent;import java.awt.event.ActionListener;public class MenuListener implements ActionListener public void actionPerformed(ActionEvent e)if(e.getActionCommand().equals(preview)System.out.println(doing preview);else if(e

31、.getActionCommand().equals(setting)System.out.println(doing Setting);,为菜单添加事件,将事件类MenuListener与组件进行关联import java.awt.*;import java.awt.event.*;public class TestMenuBar TestMenuBar()filePrint.add(printM1);/将菜单项加入print菜单中filePrint.add(printM2);fileM.add(filePrint);/将print菜单作为菜单项加入file菜单中MenuListener m

32、1=new MenuListener();printM1.addActionListener(m1);printM2.addActionListener(m1);,布局管理器,布局管理器,一个容器中的各个组件之间的位置和大小关系就称之为布局。Java语言提供了布局管理器来管理组件在容器中的布局,而不是直接使用位置坐标来设置各个组件的位置和大小。AWT中的布局管理器类:BorderLayout、FlowLayout、GridLayout、CardLayout、GridBagLayout,BorderLayout,边界布局,在一个窗体的4个边上放置4个组件,第5个组件占用中间剩下的空间。例:书P1

33、79,FlowLayout,从左到右,从上到下依次排列。一行放不下就放到下一行。例:书P175,GridLayout,将容器划分成若干行列的网格,在容器上添加组件时,它们会按从左到右、从上到下的顺序在网格中排列。在GridLayout的构造方法中,需要指定在容器上划分的网格的行、列数。例:书P177,CardLayout,实现将多个组件放在同一容器区域内的交替显示,相当于多张卡片摞在一起,在任何时候都只有最上面的一个可见。,GridBagLayout,它是GridLayout的衍生体,与其说它强制在一个栅格位置装配组件,不如说它允许组件占有几个相邻的栅格位置空间,它采用一系列约束和权值使控件各

34、就各位。功能强大,但使用非常复杂,不建议使用。,学习和开发GUI程序建议,重在掌握GUI程序的一些基本原理和开发过程,通过学习AWT组件可以更容易掌握GUI程序的基本原理和开发过程,但在GUI程序开发中,应尽量使用Swing组件。查阅JDK文档中的Swing包,通读一下其中所包含的组件,以了解Swing提供了哪些GUI组件。在用到某个GUI组件时,才有必要仔细阅读这个组件的具体使用帮助。在JDK的demo程序目录中,或是Java指南(可从Sun网站下载,英文名 The Java Tutorial)能找到某些组件的应用范例。,本章总结,事件处理机制,事件监听器,事件适配器 GUI组件的图形操作,组件的重绘实现方法 图像的显示方式 常用的AWT组件,Canvas,Container,Panel与ScrollPanel等组件 菜单的创建以及为菜单添加事件 布局管理器的种类,以及各自的特点,思考题,什么是事件、事件源和事件处理器,并描述三者的工作关系。描述事件处理的编码实现过程。描述事件监听器类和事件适配器类的关系与区别。描述在窗口上画直线的程序编写过程和组件重绘的原理。如何创建菜单,如何添加菜单事件。布局管理器的种类和特点。,

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

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


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号