版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
Java面向对象程序设计Java图形用户界面教学内容掌握AWT抽象窗口工具的一些和简单使用掌握窗体的多种布局设计的简单使用掌握SwingGUI工具包的使用掌握图形用户界面的事件处理的简单使用理解事件适配器的原理以及简单的使用AWTAWT(AbstractWindowToolkit)抽象窗口工具包,是API为Java程序提供的创建图形用户界面GUI(GraphicsUserInterface图形用户界面)的工具集,该包包含了很多类和接口,用于JavaApplication的GUI编程。使用AWT的类和接口一般在java.awt包及其子包中,AWT包含两个核心类Container(容器)和Component(组件)类。其类的层次结构如图6.1所示:容器和组件容器Container是用来组织和容纳其他界面成份和元素的组件,Java提供了相应的容器类,例如框架(JFrmae/Frame)、面板(JPanel/Panel)等类。组件Component是图形用户界面的基本单位,里面不再包含其他成份。组件是一个可以以图形化的方式显示在屏幕上并能与用户进行交互的对象,例如一个按钮、一个标签等。组件不能独立显示出来,必须将组件放在一定的容器中才可以显示出来。有两种常用的Container:Window:其对象表示自由停泊的顶级窗口Panel:其对象可作为容纳其他Component对象,不能独立存在,必须被添加到其他Container中(如Window或Applet)Frame类Frame类是window的子类,由Frame或其子类创建的对象为一个窗体。1、Frame类的常用构造方法如表6.1所示:表6.1构造方法主要功能()构造一个最初不可见的Frame新实例(title)构造一个新的、最初不可见的、具有指定标题的Frame对象
1、
Frame类的常用方法如表6.2所示:表6.2方法主要功能publicvoidsetBounds(intx,inty,intwidth,intheight)设置窗体的位置和大小。由x和y指定窗体左上角的相对于父窗体的位置,由width和height指定窗体的大小publicvoidsetSize(intwidth,intheight)设置窗体的大小publicvoidsetLocation(intx,inty)设置窗体的位置publicvoidsetTitle(title)设置窗体的标题publicvoidsetBackground(c)设置窗体的背景颜色publicvoidsetResizable(booleanresizable)设置此窗体是否可由用户调整大小,resizable-如果此窗体是可调整大小的,则为true;否则为falsepublicvoidsetVisible(booleanb)根据参数b的值显示或隐藏此窗体【例6-1】:通过案例来掌握Frame的使用:基本窗口的显示importjava.awt.Color;importjava.awt.Frame;publicclassDemo6_01{ publicstaticvoidmain(Stringargs[]){ Framef=newFrame("MyFirstTestFrame"); f.setLocation(300,300); f.setSize(170,100); f.setBackground(Color.blue); f.setResizable(false); f.setVisible(true); }}程序运行的结果:该窗体左上角的位置离它的父窗体左边、上边的坐标点是(300,300)。【例6-2】:通过案例来掌握Frame的使用:显示多个不同背景颜色的窗体。importjava.awt.Color;importjava.awt.Frame;classMultiFrameextendsFrame{ staticintid=0; MultiFrame(intx,inty,intw,inth,Colorcolor){ super("MultiFrame"+(++id));setBackground(color);setLayout(null);setBounds(x,y,w,h);setVisible(true);}}publicclassDemo6_02{ publicstaticvoidmain(Stringargs[]){ MultiFrameframe1=newMultiFrame(100,100,200,200,Color.MAGENTA); MultiFrameframe2=newMultiFrame(300,100,200,200,Color.GREEN); MultiFrameframe3=newMultiFrame(100,300,200,200,Color.YELLOW); MultiFrameframe4=newMultiFrame(300,300,200,200,Color.BLUE);}}程序运行的结果:Panel类Panel类是Container的子类,Panel对象可以看成可以容纳Component的空间。1、Panel类的常用构造方法如表6.3所示:表6.32、Panel类的常用方法(从父类继承过来的方法)如表6.4所示:表6.4构造方法主要功能()使用默认的布局管理器创建新面板(layout)创建具有指定布局管理器的新面板方法主要功能publicvoidsetBounds(intx,inty,intwidth,intheight)设置Panel的位置和大小。由x和y指定窗体左上角的相对于父窗体的位置,由width和height指定窗体的大小publicvoidsetSize(intwidth,intheight)设置Panel的大小publicvoidsetLocation(intx,inty)设置Panel的位置publicvoidsetBackground(c)设置Panel的背景颜色publicvoidsetLayout(mgr)设置Panel的布局管理器【例6-3】:通过案例来掌握Panel的基础用法。importjava.awt.Color;importjava.awt.Frame;importjava.awt.Panel;publicclassDemo6_03{ publicstaticvoidmain(Stringargs[]){ Framef=newFrame("JavaFramewithPanel"); Panelp=newPanel(null);f.setLayout(null);f.setBounds(300,300,500,500);f.setBackground(newColor(0,0,102));p.setBounds(50,50,400,400); p.setBackground(newColor(204,204,255)); f.add(p);f.setVisible(true);}}程序运行的结果:【例6-4】:通过案例来掌握Panel的使用:显示多个不同背景颜色的Panel。importjava.awt.Color;importjava.awt.Frame;importjava.awt.Panel;classMultiPanelextendsFrame{ privatePanelp1,p2,p3,p4; MultiPanel(Strings,intx,inty,intw,inth){ super(s);setLayout(null);p1=newPanel(null);p2=newPanel(null);p3=newPanel(null);p4=newPanel(null);p1.setBounds(0,0,w/2,h/2);p2.setBounds(0,h/2,w/2,h/2);p3.setBounds(w/2,0,w/2,h/2);p4.setBounds(w/2,h/2,w/2,h/2);p1.setBackground(Color.BLACK);p2.setBackground(Color.BLUE);p3.setBackground(Color.YELLOW);p4.setBackground(Color.RED);add(p1);add(p2);add(p3);add(p4);setBounds(x,y,w,h);setVisible(true);}}publicclassDemo6_04{ publicstaticvoidmain(Stringargs[]){ newMultiPanel("FrameWithMultiPanel",200,200,300,200); }}程序运行的结果:【例6-5】:通过案例来掌握Frame和Panel的使用:Panel显示在Frame中间且占Frame大小的一半。importjava.awt.Color;importjava.awt.Frame;importjava.awt.Panel;classFrameWithPanelextendsFrame{ privatePanelp; FrameWithPanel(intx,inty,intw,inth,Colorc){ super("FrameWithPanel"); setLayout(null); setBounds(x,y,w,h); setBackground(c); p=newPanel(null); p.setBounds(w/4,h/4,w/2,h/2); p.setBackground(Color.RED); add(p); setVisible(true); }}publicclassDemo6_05{ publicstaticvoidmain(Stringargs[]){ newFrameWithPanel(200,200,300,200,Color.BLUE); }}程序运行结果:布局设计Container可以存放组件,组件的排列方式一般情况下有两种,一种是人工排列,一种是利用Java提供的布局管理器对这些组件进行排列,布局管理器直接设置这些组件的位置、大小和排列顺序,不同的布局管理器采用不同的算法和策略。Java提供的布局管理器主要有5中:FlowLayout、BorderLayout、GridLayout、CardLayout、GridBagLayout布局。FlowLayout流布局,是Panel、Applet的默认布局,该布局对组件从上到下,从左到右进行布置,一行排满后换行。不改变组件的大小,按组件的原有尺寸显示组件,可设置不同的组件的间距、行距以及对齐方式。FlowLayout默认的对齐方式是居中对齐。FlowLayout的构造方法如表6.5所示:表6.5FlowLayout布局构造方法主要功能()构造一个新的FlowLayout,它是居中对齐的,默认的水平和垂直间隙是5个单位(intalign)构造一个新的FlowLayout,它具有指定的对齐方式,默认的水平和垂直间隙是5个单位(intalign,inthgap,intvgap)创建一个新的流布局管理器,它具有指定的对齐方式以及指定的水平和垂直间隙【例6-6】:通过案例来掌握FlowLayout布局管理器的使用:importjava.awt.Button;importjava.awt.FlowLayout;importjava.awt.Frame;publicclassDemo6_06{ publicstaticvoidmain(Stringargs[]){ Framef=newFrame("FlowLayout"); ButtonOpen=newButton("Open"); ButtonContinue=newButton("Continue"); ButtonClose=newButton("Close"); ButtonExit=newButton("Exit"); f.setLayout(newFlowLayout(FlowLayout.LEFT)); f.add(Open); f.add(Continue); f.add(Close); f.add(Exit); f.setSize(100,100); f.setVisible(true); }}程序运行的结果:如果改变Frame的大小,Frame中的组件的布局也会随之改变BorderLayout是Window类及其子类的默认布局管理器,它将容器分为5个部分,分别命名为EAST、WEST、SOUTH、NORTH和CENTER。每个部分只能放一个组件,如果希望在某个部分放置多个组件,可以先放一个容器对象,然后在容器里面放置多个组件和容器。BorderLayout类提供了的构造方法如表6.6所示:表6.61、如不指定组件的加入部位,则默认加入到CENTER区,每个区域只能加入一个组件,如加入多个,则先前加入的会被覆盖。2、BorderLayout型布局容器尺寸缩放原则:北、南两个区域在水平方向缩放。东、西两个区域在垂直方向缩放。中部区域可在两个方法上都缩放。BorderLayout布局构造方法主要功能()构造一个组件之间没有间距的新边框布局。(inthgap,intvgap)构造一个具有指定组件间距的边框布局。【例6-7】:通过案例来掌握BorderLayout布局管理器的使用:importjava.awt.BorderLayout;importjava.awt.Button;importjava.awt.Frame;publicclassDemo6_07{ publicstaticvoidmain(Stringargs[]){ FrameborderLayoutFrame; borderLayoutFrame=newFrame("BorderLayout"); ButtonEAST=newButton("EAST"); ButtonSOUTH=newButton("SOUTH"); ButtonWEST=newButton("WEST"); ButtonNORTH=newButton("NORTH"); ButtonCENTER=newButton("CENTER"); borderLayoutFrame.add(EAST,BorderLayout.EAST); borderLayoutFrame.add(SOUTH,BorderLayout.SOUTH); borderLayoutFrame.add(WEST,BorderLayout.WEST); borderLayoutFrame.add(NORTH,BorderLayout.NORTH); borderLayoutFrame.add(CENTER,BorderLayout.CENTER); borderLayoutFrame.setSize(200,200); borderLayoutFrame.setVisible(true); }}程序运行的结果:GridLayout布局GridLayout布局管理器是将空间划分成规则的矩形网格,每个单元格区域大小相等。组件被添加到每个单元格中,先从左到右填满一行后换行,再从上到下。GridLayout类的构造方法如表5-2所示:表6.7构造方法主要功能()创建具有默认值的网格布局,即每个组件占据一行一列。(introws,intcols)创建具有指定行数和列数的网格布局。(introws,intcols,inthgap,intvgap)创建具有指定行数和列数的网格布局。【例6-8】:通过案例来掌握GridLayout布局管理器的使用:importjava.awt.*;publicclassDemo6_08{ publicstaticvoidmain(Stringargs[]){ Frameframe=newFrame("GridLayout"); Buttonbtn1=newButton("btn1"); Buttonbtn2=newButton("btn2"); Buttonbtn3=newButton("btn3"); Buttonbtn4=newButton("btn4"); Buttonbtn5=newButton("btn5"); Buttonbtn6=newButton("btn6"); frame.setLayout(newGridLayout(2,3)); frame.add(btn1); frame.add(btn2); frame.add(btn3); frame.add(btn4); frame.add(btn5); frame.add(btn6); frame.pack(); frame.setSize(300,150); frame.setVisible(true); }}程序运行的结果:CardLayout布局CardLayout布局CardLayout类的常用方法: 表6.8方法主要功能publicCardLayout()创建一个间距大小为0的新卡片布局publicCardLayout(inth,intv)创建一个具有指定水平间距和垂直间距的新卡片布局。水平间距置于左右边缘。垂直间距置于上下边缘publicvoidfirst(Containerp)翻转到容器的第一张卡片publicvoidlast(Containp)翻转到容器的最后一张卡片publicvoidnext(Containp)翻转到指定容器的下一张卡片publicvoidprevious(Containp)翻转到指定容器的前一张卡片publicvoidshow(Containp,Stringname)翻转到使用addLayoutComponent添加到此布局的具有指定name的组件【例6-7】:通过案例来掌握CardLayout布局importjava.awt.BorderLayout;importjava.awt.CardLayout;importjava.awt.Color;importjava.awt.Insets;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjavax.swing.JButton;importjavax.swing.JFrame;importjavax.swing.JLabel;importjavax.swing.JPanel;publicclassDemo6_09extendsJFrame{privateJPanelpane=null;privateJPanelp=null;privateCardLayoutcard=null;privateJButtonbutton_1=null;privateJButtonbutton_2=null;privateJButtonb_1=null,b_2=null,b_3=null;privateJPanelp_1=null,p_2=null,p_3=null;publicDemo6_09(){super("CardLayoutTest");card=newCardLayout(5,5);pane=newJPanel(card);p=newJPanel();button_1=newJButton("<上一步");button_2=newJButton("下一步>");b_1=newJButton("1");b_2=newJButton("2");b_3=newJButton("3");b_1.setMargin(newInsets(2,2,2,2));b_2.setMargin(newInsets(2,2,2,2));b_3.setMargin(newInsets(2,2,2,2));p.add(button_1);p.add(b_1);p.add(b_2);p.add(b_3);p.add(button_2);p_1=newJPanel();p_2=newJPanel();p_3=newJPanel();p_1.setBackground(Color.GREEN);p_2.setBackground(Color.BLUE);p_3.setBackground(Color.RED);p_1.add(newJLabel("JPanel_1"));p_2.add(newJLabel("JPanel_2"));p_3.add(newJLabel("JPanel_3"));pane.add(p_1,"p1");pane.add(p_2,"p2");pane.add(p_3,"p3");button_1.addActionListener(newActionListener(){//上一步的按钮动作publicvoidactionPerformed(ActionEvente){card.previous(pane);}});button_2.addActionListener(newActionListener(){//下一步的按钮动作publicvoidactionPerformed(ActionEvente){card.next(pane);}});b_1.addActionListener(newActionListener(){//直接翻转到p_1publicvoidactionPerformed(ActionEvente){card.show(pane,"p1");}});b_2.addActionListener(newActionListener(){//直接翻转到p_2publicvoidactionPerformed(ActionEvente){card.show(pane,"p2");}});b_3.addActionListener(newActionListener(){//直接翻转到p_3publicvoidactionPerformed(ActionEvente){card.show(pane,"p3");}});this.getContentPane().add(pane);this.getContentPane().add(p,BorderLayout.SOUTH);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.setSize(300,200);this.setVisible(true);}publicstaticvoidmain(String[]args){newDemo6_09();}}程序运行的结果:wing
JavaSwing是JDK1.2后版本引入的第二代GUI类库,具有更好的可移植性,提供了更完整的组件,增加了许多功能和特性。Swing可以扩展和简化跨平台应用程序的开发。Swing系列由17个包组成,每个包都有其独特的用途。正如您将在短期课程中学到的一样,这些软件包使您可以轻松整合各种具有高度复杂性和用户友好性的应用程序。Swing的特点:1、纯JAVA实现轻量级,不依赖操作系统,具有更好的跨平台性,2、采用MVC设计思想3、采用要插入式外观感觉,在同一平台可有不同的外观展示4、建立在AWT基础之上,不能完全舍弃AWTSwing基本组件组件分为容器类和非容器类,容器类是指那些能放入其它组件的组件,而非容器类是指JButtonLabel等。容器类分为顶层容器和非顶层容器。顶层容器是指有独立窗口诸如Window这样的,而Windwo最常用的子类是JFrame和JDialog顶层容器可以独立存在,包括JFrame、JDialog、JApplet、JWindow(JDialog不可以独立存在)。JFrame是大多数应用程序的基本窗口,有边框、标题和按钮,允许程序员把其他组件添加到它里面,把它们组织起来,并把它们呈现给用户。中间容器不能独立存在,必须放在顶层容器内,且能够容纳其他控件,包括JPanel、JScrollPane、JToolBar、JSplitPane、JTabbedPane。用法都是New出对应的面板,可以向其中添加组件,之后放到JFrame中即可,这里不再做一一截图。表6.9主要中间容器及功能方法主要功能JPanel最普通的面板,没有特殊功能,主要用来容纳其它控件JScrollPane滚动面板,即带有长宽滚动条,主要用来容纳大型控件JToolBar工具栏面板,包含图标按钮。可以在程序的主窗口之外浮动或是托拽JSplitPane分割式面板JTabbedPane选项卡面板【例6-10】:通过案例来掌握JComboBox类importjava.awt.Container;importjava.awt.Dimension;importjava.awt.FlowLayout;importjavax.swing.AbstractListModel;importjavax.swing.ComboBoxModel;importjavax.swing.JComboBox;importjavax.swing.JFrame;importjavax.swing.JLabel;importjavax.swing.WindowConstants;publicclassDemo6_10{ publicstaticvoidmain(String[]args){ newJComboBoxDemo(); }}classJComboBoxDemoextendsJFrame{ JComboBoxjc=newJComboBox(newComboBoxDemo()); JLabeljl=newJLabel("您最喜欢的图书类型:"); publicJComboBoxDemo(){ setSize(newDimension(250,350)); setVisible(true); setTitle("下拉列表框案列"); setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); Containercp=getContentPane(); cp.setLayout(newFlowLayout()); cp.add(jl); cp.add(jc); } }classComboBoxDemoextendsAbstractListModelimplementsComboBoxModel{ Stringselecteditem=null; String[]test={"文学","政治","经济","科技","医学"}; publicObjectgetElementAt(intindex){ returntest[index]; } publicintgetSize(){ returntest.length; } publicvoidsetSelectedItem(Objectitem){ selecteditem=(String)item; } publicObjectgetSelectedItem(){ returnselecteditem; } publicintgetIndex(){ for(inti=0;i<test.length;i++){ if(test[i].equals(getSelectedItem())) returni; break; } return0; }}程序运行的结果:事件处理图形界面上的事件是指在某个组件上发生用户操作。例如,用户单击了界面上的某个按钮,就说在这个按钮上发生了事件,这个按钮对象就是事件的击发者。对事件作监视的对象称为监视器,监视器提供响应事件的处理方法。为了让监视器与事件对象关联起来,需要对事件对象作监视器注册,告诉系统事件对象的监视器。其整个处理过程是这样的,事件源可以注册事件监听器对象,并可以向事件监听器对象发送事件对象。事件发生后,事件源将事件对象发给已经注册的所有事件监听器。监听器对象随后会根据事件对象内的相应方法响应这个事件。Java事件处理三要素:1、事件源(EventSource):即事件发生的场所,就是指各个组件,如按钮等,点击按钮其实就是组件上发生的一个事件;2、事件(Event):事件封装了组件上发生的事情,比如按钮单击、按钮松开等等;3、事件监听器(EventListener):负责监听事件源上发生的特定类型的事件,当事件到来时还必须负责处理相应的事件;事件处理机制在java语言中,为了便于系统管理事件,也为了便于程序作监视器注册,系统将事件分类,称为事件类型。系统为每个事件类型提供一个接口。要作为监视器对象的类必须实现相应的接口,提供接口规定的响应事件的方法。以程序响应按钮事件为例,JButton类对象button可以是一个事件的激发者。当用户点击界面中与button对应的按钮时,button对象就会产生一个ActionEvent类型的事件。如果监视器对象是obj,对象obj的类是Obj,则类Obj必须实现AWT中的ActionListener接口,实现监视按钮事件的actionPerformed方法。button对象必须用addActionListener方法注册它的监视器obj。程序运行时,当用户点击button对象对应的按钮时,系统就将一个ActionEvent对象从事件激发对象传递到监视器。ActionEvent对象包含的信息包括事件发生在哪一个按钮,以及有关该事件的其他信息。实际事件发生时,通常会产生一系列的事件,例如,用户点击按钮,会产生ChangeEvent事件提示光标到了按钮上,接着又是一个ChangeEvent事件表示鼠标被按下,然后是ActionEvent事件表示鼠标已松开,但光标依旧在按钮上,最后是ChangeEvent事件,表示光标已离开按钮。但是应用程序通常只处理按下按钮的完整动作的单个ActionEvent事件。每个事件类型都有一个相应的监视器接口,下面列出了每个接口的方法。实现监视器接口的类必须实现所有定义在接口中的方法。外部类实现监听器【例6-11】通过案例来掌握外部类实现监听器importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;publicclassDemo6_11extendsJFrame{ JButtonbutton1,button2; JPanelpane1,pane2,p1,p2; CardLayoutcard1=newCardLayout(); Demo6_11(){ this.setTitle("外部类实现事件监听"); this.setBounds(200,200,300,200); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); init(); }
publicvoidinit(){ p1=newJPanel(); p1.add(newJLabel("第一个面板")); p1.setBackground(Color.red); p2=newJPanel(); p2.add(newJLabel("第二个面板")); p2.setBackground(Color.green); pane1=newJPanel(card1); pane1.add("红色",p1); pane1.add("绿色",p2); button1=newJButton("红色"); button2=newJButton("绿色"); pane2=newJPanel(); pane2.add(button1); pane2.add(button2); this.add(pane1,BorderLayout.CENTER); this.add(pane2,BorderLayout.SOUTH); button1.addActionListener(newColorEvent(card1,pane1)); button2.addActionListener(newColorEvent(card1,pane1)); } publicstaticvoidmain(String[]args){ newDemo6_11(); }}classColorEventimplementsActionListener{ CardLayoutcard1=newCardLayout(); JPanelpane1=newJPanel(); ColorEvent(CardLayoutcard1,JPanelpane1){ this.card1=card1; this.pane1=pane1; } publicvoidactionPerformed(ActionEvente){ if(e.getActionCommand().equals("红色")){ card1.show(pane1,"红色"); }elseif(e.getActionCommand().equals("绿色")){ card1.show(pane1,"绿色"); } }}程序运行的结果类本身实现监听器【例6-12】通过案例来掌握类本身实现监听器importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;publicclassDemo6_12extendsJFrameimplementsActionListener{ JButtonbutton1,button2; JPanelpane1,pane2,p1,p2;CardLayoutcard1=newCardLayout();Demo6_12(){this.setTitle("自身类实现事件监听");this.setBounds(200,200,300,200);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.setVisible(true);init();}publicvoidinit(){p1=newJPanel();p1.add(newJLabel("第一个面板"));p1.setBackground(Color.red);p2=newJPanel();p2.add(newJLabel("第二个面板"));p2.setBackground(Color.green);pane1=newJPanel(card1);pane1.add("红色",p1);pane1.add("绿色",p2);button1=newJButton("红色");button2=newJButton("绿色");pane2=newJPanel();pane2.add(button1);pane2.add(button2);this.add(pane1,BorderLayout.CENTER);this.add(pane2,BorderLayout.SOUTH);button1.addActionListener(newColorEvent(card1,pane1));button2.addActionListener(newColorEvent(card1,pane1));}publicvoidactionPerformed(ActionEvente){if(e.getActionCommand().equals("红色")){card1.show(pane1,"红色");}elseif(e.getActionCommand().equals("绿色")){ card1.show(pane1,"绿色"); }}publicstaticvoidmain(String[]args){ newDemo6_12();}}程序运行的结果:运行结果与上个案例相同
内部类实现监听器【例6-13】通过案例来掌握内部类实现监听器importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;publicclassDemo6_13extendsJFrame{ JButtonbutton1,button2; JPanelpane1,pane2,p1,p2; CardLayoutcard1=newCardLayout(); Demo6_13(){ this.setTitle("内部类实现事件监听");this.setBounds(200,200,300,200);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.setVisible(true);init();}ArrayList类使用示例例6.6把10~20的阶乘值用大整数(BigInteger)显示在屏幕上。分析:程序中factorial(intx)是一自定义的静态方法,该方法有一个整型参数,返回值类型为ArrayList类对象。方法的功能是求0到x中所有整数的阶乘值,将结果存放到ArrayList类对象中并返回结果。大整数不是基本数据类型,它们的乘法不能用运算符*,而要使用大整数类的multiply()方法求乘积。publicvoidinit(){p1=newJPanel(); p1.add(newJLabel("第一个面板")); p1.setBackground(Color.red); p2=newJPanel(); p2.add(newJLabel("第二个面板")); p2.setBackground(Color.green); pane1=newJPanel(card1); pane1.add("红色",p1); pane1.add("绿色",p2); button1=newJButton("红色"); button2=newJButton("绿色"); pane2=newJPanel(); pane2.add(button1); pane2.add(button2); this.add(pane1,BorderLayout.CENTER); this.add(pane2,BorderLayout.SOUTH); button1.addActionListener(newColorEventDemo(card1,pane1)); button2.addActionListener(newColorEventDemo(card1,pane1)); } }classColorEventDemoimplementsActionListener{ CardLayoutcard1=newCardLayout(); JPanelpane1=newJPanel(); ColorEventDemo(CardLayoutcard1,JPanelpane1){ this.card1=card1; this.pane1=pane1; } publicstaticvoidmain(String[]args){ newDemo6_13(); } publicvoidactionPerformed(ActionEvente){ if(e.getActionCommand().equals("红色")){ card1.show(pane1,"红色"); } elseif(e.getActionCommand().equals("绿色")){ card1.show(pane1,"绿色"); } }}程序运行的结果:运行结果与上个案例相同匿名内部类实现监听器【例6-14】通过案例来掌握匿名内部类实现监听器importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;publicclassDemo6_14extendsJFrame{ JButtonbutton1,button2; JPanelpane1,pane2,p1,p2; CardLayoutcard1=newCardLayout(); Demo6_14(){ this.setTitle("匿名内部类实现事件监听");this.setBounds(200,200,300,200);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.setVisible(true);init();}
publicvoidinit(){p1=newJPanel(); p1.add(newJLabel("第一个面板")); p1.setBackground(Color.red); p2=newJPanel(); p2.add(newJLabel("第二个面板")); p2.setBackground(Color.green); pane1=newJPanel(card1); pane1.add("红色",p1); pane1.add("绿色",p2); button1=newJButton("红色"); button2=newJButton("绿色"); pane2=newJPanel(); pane2.add(button1); pane2.add(button2); this.add(pane1,BorderLayout.CENTER); this.add(pane2,BorderLayout.SOUTH); button1.addActionListener(newActionListener(){ publicvoidactionPerformed(ActionEventarg0){ card1.show(pane1,"红色"); } }); button2.addActionListener(newActionListener(){ publicvoidactionPerformed(ActionEventarg0){ card1.show(pane1,"绿色"); } }); } publicstaticvoidmain(String[]args){ newDemo6_14(); }}程序运行的结果:运行结果与上个案例相同键盘事件
键盘事件的事件源一般与该组件相关,当一个组件处于激活状态时,按下、释放或敲击键盘上的某个键时就会发生键盘事件。键盘事件的接口是KeyListener,注册键盘事件监视器的方法是addKeyListener(监视器)。实现KeyListener接口有3个要实现的方法: 表6.10:KeyListener接口方法方法主要功能keyPressed(KeyEvente)按下某个键时调用此方法。keyReleased(KeyEvente)释放某个键时调用此方法。keyTyped(KeyEvente)键入某个键时调用此方法。【6-15】:通过案例来掌握键盘事件importjava.awt.*;importjava.awt.event.*;importjavax.swing.JFrame;publicclassDemo6_15extendsJFrameimplementsKeyListener{ TextAreatext1=newTextArea(5,20); TextAreatext2=newTextArea(5,20); publicDemo6_15(Stringtitle){ super(title); setLocation(200,300); setLayout(newGridLayout(2,1)); setSize(300,400); text1.addKeyListener(this); add(text1); add(text2); this.setVisible(true); } publicvoidkeyPressed(KeyEvente){ } publicvoidkeyTyped(KeyEvente){ text2.setText(text1.getText()); } publicvoidkeyReleased(KeyEvente){} publicstaticvoidmain(String[]args){ Demo6_15demo15=newDemo6_15("键盘事件"); }}程序运行的结果:鼠标事件
鼠标事件的事件源往往与容器相关,当鼠标进入容器、离开容器,或者在容器中单击鼠标、拖动鼠标时都会发生鼠标事件。java语言为处理鼠标事件提供两个接口:MouseListener,MouseMotionListener接口。我们主要讲一下MouseListenerMouseListener接口能处理5种鼠标事件:按下鼠标,释放鼠标,点击鼠标、鼠标进入、鼠标退出。相应的方法有: 表6.11:MouseListener接口方法实现MouseListener接口就要实现以上的五种方法,而每一种都有参数MouseEvent,通过这个参数获取鼠标的当前信息。MouseEvent的常用方法。 表6.12:MouseEvent常用方法:方法主要功能mousePressed(MouseEvente)在组件上按下鼠标按钮时调用mouseReleased(MouseEvente)在组件上释放鼠标按钮时调用mouseEntered(MouseEvente)当鼠标进入组件时调用mouseExited(MouseEvente)当鼠标退出组件时调用mouseClicked(MouseEvente)在组件上单击(按下并释放)鼠标按钮时调用方法主要功能intgetX()鼠标的X坐标intgetY()鼠标的Y坐标getClickCount()鼠标被点击的次数getSource()获取发生鼠标的事件源【例6-16】:通过案例来掌握鼠标事件小应用程序设置了一个文本区,用于记录一系列鼠标事件。当鼠标进入小应用程序窗口时,文本区显示“鼠标进来”;当鼠标离开窗口时,文本区显示“鼠标走开”;当鼠标被按下时,文本区显示“鼠标按下”,当鼠标被双击时,文本区显示“鼠标双击”;并显示鼠标的坐标。程序还显示一个红色的圆,当点击鼠标时,圆的半径会不断地变大。importjavax.swing.*;importjava.awt.*;importjava.awt.event.*;classMyFrameextendsJFrameimplementsMouseListener{ JTextAreatext; intx,y,r=10; intmouseFlg=0; staticStringmouseStates[]={"鼠标单击","鼠标进来","鼠标走开","鼠标双击","鼠标键按下","鼠标松开"}; MyFrame(Strings){ super(s); Containercon=this.getContentPane(); this.setSize(300,400); this.setLayout(newGridLayout(2,1)); this.setLocation(200,100); text=newJTextArea(15,20); text.setBackground(Color.green); con.add(text); addMouseListener(this); this.setVisible(true); this.pack(); } publicvoidpaint(){ text.append(mouseStates[mouseFlg]+"了,位置是:"+x+","+y+"\n"); }
publicvoidmousePressed(MouseEvente){ x=e.getX(); y=e.getY(); mouseFlg=4; paint(); } publicvoidmouseRelease(MouseEvente){} publicvoidmouseEntered(MouseEvente){ x=e.getX(); y=e.getY(); mouseFlg=1; paint(); } publicvoidmouseExited(MouseEvente){ x=e.getX(); y=e.getY(); mouseFlg=2; paint(); } publicvoidmouseClicked(MouseEvente){ if(e.getClickCount()==2){ x=e.getX(); y=e.getY(); mouseFlg=3; paint(); }else{ x=e.getX(); y=e.getY(); mouseFlg=0; paint(); }} publicvoidmouseReleased(MouseEvente){ x=e.getX(); y=e.getY(); mouseFlg=5; paint(); }}publicclassDemo6_16{ publicstaticvoidmain(String[]args){ MyFramemyFrame=newMyFrame("鼠标事件示意程序"); }}程序运行的结果:事件适配器事件适配器是监听器接口的空实现;事件适配器实现了监听器接口,并为接口的每个方法都提供了空的实现,方法体内没有具体的代码。当需要创建监听器的时候,可以通过继承适配器,而不是实现监听器接口。因为适配器已经空实现了监听器接口中所有的方法,因此只要重写自己想要写的方法,从而简化事件监听器的代码的编写。事件适配器类:将对应事件接口下所有的方法自动实现。下面以Window和Key事件举例。WindowListener窗体事件接口,7个抽象方法,对应的适配器WindowAdapter,类,案例6-17采用WindowAdapter自动实现这7个抽象方法,重写了3个方法。【例6-17】:通过案例来掌握窗体适配器类的使用importjava.awt.FlowLayout;importjava.awt.event.WindowAdapter;importjava.awt.event.WindowEvent;importjavax.swing.JButton;importjavax.swing.JFrame;publicclassDemo6_17extendsJFrame{publicDemo6_17(){ setLayout(newFlowLayout(FlowLayout.CENTER)); add(newJButton("我是一个按钮")); addWindowListener(newMyWindowListener()); } publicstaticvoidmain(String[]agrs){ JFrameframe=newDemo6_17(); frame.setSize(400,400); frame.setLocation(400,300); frame.setVisible(true); }}classMyWindowListenerextendsWindowAdapter{publicvoidwindowClosing(WindowEvente){//窗口正处在关闭过程中时调用System.out.println("关闭状态");System.exit(0);}publicvoidwindowActivated(WindowEvente){//激活窗口时用System.out.println("激活状态");}publicvoidwindowOpened(WindowEvente){//已打开窗口时调用System.out.println("打开状态");}}程序运行的结果:KeyListener:键盘事件接口,对应的适配器是KeyAdapter,键盘适配器类。【例6-18】:通过案例来掌握键盘适配器类importjava.awt.*;importjava.awt.event.*;publicclassDemo6_18{ Framefr=newFrame("KeyEvent..."); publicDemo6_18(){ fr.setLocation(300,300); fr.setVisible(true); fr.addWindowListener(newWindowAdapter(){ publicvoidwindowClosing(WindowEvente){ System.exit(0); fr.setVisible(false); } }); fr.addKeyListener(newKeyAdapter(){ publicvoidkeyPressed(KeyEvente){ intkeycode=e.getKeyCode(); if(keycode==KeyEvent.VK_ENTER){ System.out.println("您按下的回车键"); } } }); } publicstaticvoidmain(String[]args){newDemo6_18(); }}程序运行的结果:MouseListener鼠标事件接口,对应的适配器是MouseAdapter类,鼠标适配器类。【例6-19】:通过案例来掌握鼠标适配器类importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;publicclassDemo6_19{ privateFramef; privateButtonbt; Demo6_19(){//构造方法 madeFrame(); } publicvoidmadeFrame(){ f=
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 二年级科学寒假衔接测量方法判断题综合应用卷专题集训版
- 2026柔性可穿戴设备用各向异性EMI导电硅橡胶的动态力学响应与屏蔽效能衰减规律深度研究
- 2026事业单位工勤技能-广东-广东热力运行工四级(中级工)历年参考题库含答案详解
- 2026事业单位工勤技能-山西-山西电工五级(初级工)历年参考题库含答案详解
- 2026事业单位工勤技能-山东-山东管道工四级(中级工)历年参考题库含答案详解
- 2026事业单位工勤技能-安徽-安徽铸造工二级(技师)历年参考题库含答案详解
- 2026事业单位工勤技能-安徽-安徽保健按摩师三级(高级工)历年参考题库含答案详解
- 2026事业单位工勤技能-宁夏-宁夏医技工四级(中级工)历年参考题库含答案详解
- 2026事业单位工勤技能-天津-天津家禽饲养员四级(中级工)历年参考题库含答案详解
- 2026事业单位工勤技能-四川-四川有线广播电视机务员五级(初级工)历年参考题库含答案详解
- 第6课 全球航路的开辟 授课课件(共26张)
- RTO焚烧炉日常巡检操作规程
- 2026秋新教材人教版小学数学四年级上册教学计划及进度表
- 2026腰椎间盘突出症患者术后护理
- 2026年四川高考化学试卷答案详解及复习备考指导
- 2026年秋季学期中小学1530安全教育记录
- 2026年中级银行从业资格《银行管理》考试真题(后附解析)
- 历年中考英语高频词汇汇编(真题800词版)
- 资阳空港私募基金管理有限责任公司市场化招聘(10人)笔试参考题库及答案详解
- 餐厅收银员操作规范
- (2026版)《中华人民共和国民族团结进步促进法》解读
评论
0/150
提交评论