Java程序設(shè)計(jì)項(xiàng)目案例化教程課件:Java圖形用戶界面_第1頁(yè)
Java程序設(shè)計(jì)項(xiàng)目案例化教程課件:Java圖形用戶界面_第2頁(yè)
Java程序設(shè)計(jì)項(xiàng)目案例化教程課件:Java圖形用戶界面_第3頁(yè)
Java程序設(shè)計(jì)項(xiàng)目案例化教程課件:Java圖形用戶界面_第4頁(yè)
Java程序設(shè)計(jì)項(xiàng)目案例化教程課件:Java圖形用戶界面_第5頁(yè)
已閱讀5頁(yè),還剩51頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

Java面向?qū)ο蟪绦蛟O(shè)計(jì)Java圖形用戶界面教學(xué)內(nèi)容掌握AWT抽象窗口工具的一些和簡(jiǎn)單使用掌握窗體的多種布局設(shè)計(jì)的簡(jiǎn)單使用掌握SwingGUI工具包的使用掌握?qǐng)D形用戶界面的事件處理的簡(jiǎn)單使用理解事件適配器的原理以及簡(jiǎn)單的使用AWTAWT(AbstractWindowToolkit)抽象窗口工具包,是API為Java程序提供的創(chuàng)建圖形用戶界面GUI(GraphicsUserInterface圖形用戶界面)的工具集,該包包含了很多類和接口,用于JavaApplication的GUI編程。使用AWT的類和接口一般在java.awt包及其子包中,AWT包含兩個(gè)核心類Container(容器)和Component(組件)類。其類的層次結(jié)構(gòu)如圖6.1所示:容器和組件容器Container是用來(lái)組織和容納其他界面成份和元素的組件,Java提供了相應(yīng)的容器類,例如框架(JFrmae/Frame)、面板(JPanel/Panel)等類。組件Component是圖形用戶界面的基本單位,里面不再包含其他成份。組件是一個(gè)可以以圖形化的方式顯示在屏幕上并能與用戶進(jìn)行交互的對(duì)象,例如一個(gè)按鈕、一個(gè)標(biāo)簽等。組件不能獨(dú)立顯示出來(lái),必須將組件放在一定的容器中才可以顯示出來(lái)。有兩種常用的Container:Window:其對(duì)象表示自由停泊的頂級(jí)窗口Panel:其對(duì)象可作為容納其他Component對(duì)象,不能獨(dú)立存在,必須被添加到其他Container中(如Window或Applet)Frame類Frame類是window的子類,由Frame或其子類創(chuàng)建的對(duì)象為一個(gè)窗體。1、Frame類的常用構(gòu)造方法如表6.1所示:表6.1構(gòu)造方法主要功能()構(gòu)造一個(gè)最初不可見(jiàn)的Frame新實(shí)例(title)構(gòu)造一個(gè)新的、最初不可見(jiàn)的、具有指定標(biāo)題的Frame對(duì)象

1、

Frame類的常用方法如表6.2所示:表6.2方法主要功能publicvoidsetBounds(intx,inty,intwidth,intheight)設(shè)置窗體的位置和大小。由x和y指定窗體左上角的相對(duì)于父窗體的位置,由width和height指定窗體的大小publicvoidsetSize(intwidth,intheight)設(shè)置窗體的大小publicvoidsetLocation(intx,inty)設(shè)置窗體的位置publicvoidsetTitle(title)設(shè)置窗體的標(biāo)題publicvoidsetBackground(c)設(shè)置窗體的背景顏色publicvoidsetResizable(booleanresizable)設(shè)置此窗體是否可由用戶調(diào)整大小,resizable-如果此窗體是可調(diào)整大小的,則為true;否則為falsepublicvoidsetVisible(booleanb)根據(jù)參數(shù)b的值顯示或隱藏此窗體【例6-1】:通過(guò)案例來(lái)掌握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); }}程序運(yùn)行的結(jié)果:該窗體左上角的位置離它的父窗體左邊、上邊的坐標(biāo)點(diǎn)是(300,300)?!纠?-2】:通過(guò)案例來(lái)掌握Frame的使用:顯示多個(gè)不同背景顏色的窗體。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);}}程序運(yùn)行的結(jié)果:Panel類Panel類是Container的子類,Panel對(duì)象可以看成可以容納Component的空間。1、Panel類的常用構(gòu)造方法如表6.3所示:表6.32、Panel類的常用方法(從父類繼承過(guò)來(lái)的方法)如表6.4所示:表6.4構(gòu)造方法主要功能()使用默認(rèn)的布局管理器創(chuàng)建新面板(layout)創(chuàng)建具有指定布局管理器的新面板方法主要功能publicvoidsetBounds(intx,inty,intwidth,intheight)設(shè)置Panel的位置和大小。由x和y指定窗體左上角的相對(duì)于父窗體的位置,由width和height指定窗體的大小publicvoidsetSize(intwidth,intheight)設(shè)置Panel的大小publicvoidsetLocation(intx,inty)設(shè)置Panel的位置publicvoidsetBackground(c)設(shè)置Panel的背景顏色publicvoidsetLayout(mgr)設(shè)置Panel的布局管理器【例6-3】:通過(guò)案例來(lái)掌握Panel的基礎(chǔ)用法。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);}}程序運(yùn)行的結(jié)果:【例6-4】:通過(guò)案例來(lái)掌握Panel的使用:顯示多個(gè)不同背景顏色的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); }}程序運(yùn)行的結(jié)果:【例6-5】:通過(guò)案例來(lái)掌握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); }}程序運(yùn)行結(jié)果:布局設(shè)計(jì)Container可以存放組件,組件的排列方式一般情況下有兩種,一種是人工排列,一種是利用Java提供的布局管理器對(duì)這些組件進(jìn)行排列,布局管理器直接設(shè)置這些組件的位置、大小和排列順序,不同的布局管理器采用不同的算法和策略。Java提供的布局管理器主要有5中:FlowLayout、BorderLayout、GridLayout、CardLayout、GridBagLayout布局。FlowLayout流布局,是Panel、Applet的默認(rèn)布局,該布局對(duì)組件從上到下,從左到右進(jìn)行布置,一行排滿后換行。不改變組件的大小,按組件的原有尺寸顯示組件,可設(shè)置不同的組件的間距、行距以及對(duì)齊方式。FlowLayout默認(rèn)的對(duì)齊方式是居中對(duì)齊。FlowLayout的構(gòu)造方法如表6.5所示:表6.5FlowLayout布局構(gòu)造方法主要功能()構(gòu)造一個(gè)新的FlowLayout,它是居中對(duì)齊的,默認(rèn)的水平和垂直間隙是5個(gè)單位(intalign)構(gòu)造一個(gè)新的FlowLayout,它具有指定的對(duì)齊方式,默認(rèn)的水平和垂直間隙是5個(gè)單位(intalign,inthgap,intvgap)創(chuàng)建一個(gè)新的流布局管理器,它具有指定的對(duì)齊方式以及指定的水平和垂直間隙【例6-6】:通過(guò)案例來(lái)掌握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); }}程序運(yùn)行的結(jié)果:如果改變Frame的大小,F(xiàn)rame中的組件的布局也會(huì)隨之改變BorderLayout是Window類及其子類的默認(rèn)布局管理器,它將容器分為5個(gè)部分,分別命名為EAST、WEST、SOUTH、NORTH和CENTER。每個(gè)部分只能放一個(gè)組件,如果希望在某個(gè)部分放置多個(gè)組件,可以先放一個(gè)容器對(duì)象,然后在容器里面放置多個(gè)組件和容器。BorderLayout類提供了的構(gòu)造方法如表6.6所示:表6.61、如不指定組件的加入部位,則默認(rèn)加入到CENTER區(qū),每個(gè)區(qū)域只能加入一個(gè)組件,如加入多個(gè),則先前加入的會(huì)被覆蓋。2、BorderLayout型布局容器尺寸縮放原則:北、南兩個(gè)區(qū)域在水平方向縮放。東、西兩個(gè)區(qū)域在垂直方向縮放。中部區(qū)域可在兩個(gè)方法上都縮放。BorderLayout布局構(gòu)造方法主要功能()構(gòu)造一個(gè)組件之間沒(méi)有間距的新邊框布局。(inthgap,intvgap)構(gòu)造一個(gè)具有指定組件間距的邊框布局。【例6-7】:通過(guò)案例來(lái)掌握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); }}程序運(yùn)行的結(jié)果:GridLayout布局GridLayout布局管理器是將空間劃分成規(guī)則的矩形網(wǎng)格,每個(gè)單元格區(qū)域大小相等。組件被添加到每個(gè)單元格中,先從左到右填滿一行后換行,再?gòu)纳系较?。GridLayout類的構(gòu)造方法如表5-2所示:表6.7構(gòu)造方法主要功能()創(chuàng)建具有默認(rèn)值的網(wǎng)格布局,即每個(gè)組件占據(jù)一行一列。(introws,intcols)創(chuàng)建具有指定行數(shù)和列數(shù)的網(wǎng)格布局。(introws,intcols,inthgap,intvgap)創(chuàng)建具有指定行數(shù)和列數(shù)的網(wǎng)格布局?!纠?-8】:通過(guò)案例來(lái)掌握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); }}程序運(yùn)行的結(jié)果:CardLayout布局CardLayout布局CardLayout類的常用方法: 表6.8方法主要功能publicCardLayout()創(chuàng)建一個(gè)間距大小為0的新卡片布局publicCardLayout(inth,intv)創(chuàng)建一個(gè)具有指定水平間距和垂直間距的新卡片布局。水平間距置于左右邊緣。垂直間距置于上下邊緣publicvoidfirst(Containerp)翻轉(zhuǎn)到容器的第一張卡片publicvoidlast(Containp)翻轉(zhuǎn)到容器的最后一張卡片publicvoidnext(Containp)翻轉(zhuǎn)到指定容器的下一張卡片publicvoidprevious(Containp)翻轉(zhuǎn)到指定容器的前一張卡片publicvoidshow(Containp,Stringname)翻轉(zhuǎn)到使用addLayoutComponent添加到此布局的具有指定name的組件【例6-7】:通過(guò)案例來(lái)掌握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(){//上一步的按鈕動(dòng)作publicvoidactionPerformed(ActionEvente){card.previous(pane);}});button_2.addActionListener(newActionListener(){//下一步的按鈕動(dòng)作publicvoidactionPerformed(ActionEvente){card.next(pane);}});b_1.addActionListener(newActionListener(){//直接翻轉(zhuǎn)到p_1publicvoidactionPerformed(ActionEvente){card.show(pane,"p1");}});b_2.addActionListener(newActionListener(){//直接翻轉(zhuǎn)到p_2publicvoidactionPerformed(ActionEvente){card.show(pane,"p2");}});b_3.addActionListener(newActionListener(){//直接翻轉(zhuǎn)到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();}}程序運(yùn)行的結(jié)果:wing

JavaSwing是JDK1.2后版本引入的第二代GUI類庫(kù),具有更好的可移植性,提供了更完整的組件,增加了許多功能和特性。Swing可以擴(kuò)展和簡(jiǎn)化跨平臺(tái)應(yīng)用程序的開(kāi)發(fā)。Swing系列由17個(gè)包組成,每個(gè)包都有其獨(dú)特的用途。正如您將在短期課程中學(xué)到的一樣,這些軟件包使您可以輕松整合各種具有高度復(fù)雜性和用戶友好性的應(yīng)用程序。Swing的特點(diǎn):1、純JAVA實(shí)現(xiàn)輕量級(jí),不依賴操作系統(tǒng),具有更好的跨平臺(tái)性,2、采用MVC設(shè)計(jì)思想3、采用要插入式外觀感覺(jué),在同一平臺(tái)可有不同的外觀展示4、建立在AWT基礎(chǔ)之上,不能完全舍棄AWTSwing基本組件組件分為容器類和非容器類,容器類是指那些能放入其它組件的組件,而非容器類是指JButtonLabel等。容器類分為頂層容器和非頂層容器。頂層容器是指有獨(dú)立窗口諸如Window這樣的,而Windwo最常用的子類是JFrame和JDialog頂層容器可以獨(dú)立存在,包括JFrame、JDialog、JApplet、JWindow(JDialog不可以獨(dú)立存在)。JFrame是大多數(shù)應(yīng)用程序的基本窗口,有邊框、標(biāo)題和按鈕,允許程序員把其他組件添加到它里面,把它們組織起來(lái),并把它們呈現(xiàn)給用戶。中間容器不能獨(dú)立存在,必須放在頂層容器內(nèi),且能夠容納其他控件,包括JPanel、JScrollPane、JToolBar、JSplitPane、JTabbedPane。用法都是New出對(duì)應(yīng)的面板,可以向其中添加組件,之后放到JFrame中即可,這里不再做一一截圖。表6.9主要中間容器及功能方法主要功能JPanel最普通的面板,沒(méi)有特殊功能,主要用來(lái)容納其它控件JScrollPane滾動(dòng)面板,即帶有長(zhǎng)寬滾動(dòng)條,主要用來(lái)容納大型控件JToolBar工具欄面板,包含圖標(biāo)按鈕??梢栽诔绦虻闹鞔翱谥飧?dòng)或是托拽JSplitPane分割式面板JTabbedPane選項(xiàng)卡面板【例6-10】:通過(guò)案例來(lái)掌握J(rèn)ComboBox類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("您最喜歡的圖書(shū)類型:"); 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={"文學(xué)","政治","經(jīng)濟(jì)","科技","醫(yī)學(xué)"}; 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; }}程序運(yùn)行的結(jié)果:事件處理圖形界面上的事件是指在某個(gè)組件上發(fā)生用戶操作。例如,用戶單擊了界面上的某個(gè)按鈕,就說(shuō)在這個(gè)按鈕上發(fā)生了事件,這個(gè)按鈕對(duì)象就是事件的擊發(fā)者。對(duì)事件作監(jiān)視的對(duì)象稱為監(jiān)視器,監(jiān)視器提供響應(yīng)事件的處理方法。為了讓監(jiān)視器與事件對(duì)象關(guān)聯(lián)起來(lái),需要對(duì)事件對(duì)象作監(jiān)視器注冊(cè),告訴系統(tǒng)事件對(duì)象的監(jiān)視器。其整個(gè)處理過(guò)程是這樣的,事件源可以注冊(cè)事件監(jiān)聽(tīng)器對(duì)象,并可以向事件監(jiān)聽(tīng)器對(duì)象發(fā)送事件對(duì)象。事件發(fā)生后,事件源將事件對(duì)象發(fā)給已經(jīng)注冊(cè)的所有事件監(jiān)聽(tīng)器。監(jiān)聽(tīng)器對(duì)象隨后會(huì)根據(jù)事件對(duì)象內(nèi)的相應(yīng)方法響應(yīng)這個(gè)事件。Java事件處理三要素:1、事件源(EventSource):即事件發(fā)生的場(chǎng)所,就是指各個(gè)組件,如按鈕等,點(diǎn)擊按鈕其實(shí)就是組件上發(fā)生的一個(gè)事件;2、事件(Event):事件封裝了組件上發(fā)生的事情,比如按鈕單擊、按鈕松開(kāi)等等;3、事件監(jiān)聽(tīng)器(EventListener):負(fù)責(zé)監(jiān)聽(tīng)事件源上發(fā)生的特定類型的事件,當(dāng)事件到來(lái)時(shí)還必須負(fù)責(zé)處理相應(yīng)的事件;事件處理機(jī)制在java語(yǔ)言中,為了便于系統(tǒng)管理事件,也為了便于程序作監(jiān)視器注冊(cè),系統(tǒng)將事件分類,稱為事件類型。系統(tǒng)為每個(gè)事件類型提供一個(gè)接口。要作為監(jiān)視器對(duì)象的類必須實(shí)現(xiàn)相應(yīng)的接口,提供接口規(guī)定的響應(yīng)事件的方法。以程序響應(yīng)按鈕事件為例,JButton類對(duì)象button可以是一個(gè)事件的激發(fā)者。當(dāng)用戶點(diǎn)擊界面中與button對(duì)應(yīng)的按鈕時(shí),button對(duì)象就會(huì)產(chǎn)生一個(gè)ActionEvent類型的事件。如果監(jiān)視器對(duì)象是obj,對(duì)象obj的類是Obj,則類Obj必須實(shí)現(xiàn)AWT中的ActionListener接口,實(shí)現(xiàn)監(jiān)視按鈕事件的actionPerformed方法。button對(duì)象必須用addActionListener方法注冊(cè)它的監(jiān)視器obj。程序運(yùn)行時(shí),當(dāng)用戶點(diǎn)擊button對(duì)象對(duì)應(yīng)的按鈕時(shí),系統(tǒng)就將一個(gè)ActionEvent對(duì)象從事件激發(fā)對(duì)象傳遞到監(jiān)視器。ActionEvent對(duì)象包含的信息包括事件發(fā)生在哪一個(gè)按鈕,以及有關(guān)該事件的其他信息。實(shí)際事件發(fā)生時(shí),通常會(huì)產(chǎn)生一系列的事件,例如,用戶點(diǎn)擊按鈕,會(huì)產(chǎn)生ChangeEvent事件提示光標(biāo)到了按鈕上,接著又是一個(gè)ChangeEvent事件表示鼠標(biāo)被按下,然后是ActionEvent事件表示鼠標(biāo)已松開(kāi),但光標(biāo)依舊在按鈕上,最后是ChangeEvent事件,表示光標(biāo)已離開(kāi)按鈕。但是應(yīng)用程序通常只處理按下按鈕的完整動(dòng)作的單個(gè)ActionEvent事件。每個(gè)事件類型都有一個(gè)相應(yīng)的監(jiān)視器接口,下面列出了每個(gè)接口的方法。實(shí)現(xiàn)監(jiān)視器接口的類必須實(shí)現(xiàn)所有定義在接口中的方法。外部類實(shí)現(xiàn)監(jiān)聽(tīng)器【例6-11】通過(guò)案例來(lái)掌握外部類實(shí)現(xiàn)監(jiān)聽(tīng)器importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;publicclassDemo6_11extendsJFrame{ JButtonbutton1,button2; JPanelpane1,pane2,p1,p2; CardLayoutcard1=newCardLayout(); Demo6_11(){ this.setTitle("外部類實(shí)現(xiàn)事件監(jiān)聽(tīng)"); this.setBounds(200,200,300,200); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); init(); }

publicvoidinit(){ p1=newJPanel(); p1.add(newJLabel("第一個(gè)面板")); p1.setBackground(Color.red); p2=newJPanel(); p2.add(newJLabel("第二個(gè)面板")); 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,"綠色"); } }}程序運(yùn)行的結(jié)果類本身實(shí)現(xiàn)監(jiān)聽(tīng)器【例6-12】通過(guò)案例來(lái)掌握類本身實(shí)現(xiàn)監(jiān)聽(tīng)器importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;publicclassDemo6_12extendsJFrameimplementsActionListener{ JButtonbutton1,button2; JPanelpane1,pane2,p1,p2;CardLayoutcard1=newCardLayout();Demo6_12(){this.setTitle("自身類實(shí)現(xiàn)事件監(jiān)聽(tīng)");this.setBounds(200,200,300,200);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.setVisible(true);init();}publicvoidinit(){p1=newJPanel();p1.add(newJLabel("第一個(gè)面板"));p1.setBackground(Color.red);p2=newJPanel();p2.add(newJLabel("第二個(gè)面板"));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();}}程序運(yùn)行的結(jié)果:運(yùn)行結(jié)果與上個(gè)案例相同

內(nèi)部類實(shí)現(xiàn)監(jiān)聽(tīng)器【例6-13】通過(guò)案例來(lái)掌握內(nèi)部類實(shí)現(xiàn)監(jiān)聽(tīng)器importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;publicclassDemo6_13extendsJFrame{ JButtonbutton1,button2; JPanelpane1,pane2,p1,p2; CardLayoutcard1=newCardLayout(); Demo6_13(){ this.setTitle("內(nèi)部類實(shí)現(xiàn)事件監(jiān)聽(tīng)");this.setBounds(200,200,300,200);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.setVisible(true);init();}ArrayList類使用示例例6.6把10~20的階乘值用大整數(shù)(BigInteger)顯示在屏幕上。分析:程序中factorial(intx)是一自定義的靜態(tài)方法,該方法有一個(gè)整型參數(shù),返回值類型為ArrayList類對(duì)象。方法的功能是求0到x中所有整數(shù)的階乘值,將結(jié)果存放到ArrayList類對(duì)象中并返回結(jié)果。大整數(shù)不是基本數(shù)據(jù)類型,它們的乘法不能用運(yùn)算符*,而要使用大整數(shù)類的multiply()方法求乘積。publicvoidinit(){p1=newJPanel(); p1.add(newJLabel("第一個(gè)面板")); p1.setBackground(Color.red); p2=newJPanel(); p2.add(newJLabel("第二個(gè)面板")); 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,"綠色"); } }}程序運(yùn)行的結(jié)果:運(yùn)行結(jié)果與上個(gè)案例相同匿名內(nèi)部類實(shí)現(xiàn)監(jiān)聽(tīng)器【例6-14】通過(guò)案例來(lái)掌握匿名內(nèi)部類實(shí)現(xiàn)監(jiān)聽(tīng)器importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;publicclassDemo6_14extendsJFrame{ JButtonbutton1,button2; JPanelpane1,pane2,p1,p2; CardLayoutcard1=newCardLayout(); Demo6_14(){ this.setTitle("匿名內(nèi)部類實(shí)現(xiàn)事件監(jiān)聽(tīng)");this.setBounds(200,200,300,200);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.setVisible(true);init();}

publicvoidinit(){p1=newJPanel(); p1.add(newJLabel("第一個(gè)面板")); p1.setBackground(Color.red); p2=newJPanel(); p2.add(newJLabel("第二個(gè)面板")); 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(); }}程序運(yùn)行的結(jié)果:運(yùn)行結(jié)果與上個(gè)案例相同鍵盤(pán)事件

鍵盤(pán)事件的事件源一般與該組件相關(guān),當(dāng)一個(gè)組件處于激活狀態(tài)時(shí),按下、釋放或敲擊鍵盤(pán)上的某個(gè)鍵時(shí)就會(huì)發(fā)生鍵盤(pán)事件。鍵盤(pán)事件的接口是KeyListener,注冊(cè)鍵盤(pán)事件監(jiān)視器的方法是addKeyListener(監(jiān)視器)。實(shí)現(xiàn)KeyListener接口有3個(gè)要實(shí)現(xiàn)的方法: 表6.10:KeyListener接口方法方法主要功能keyPressed(KeyEvente)按下某個(gè)鍵時(shí)調(diào)用此方法。keyReleased(KeyEvente)釋放某個(gè)鍵時(shí)調(diào)用此方法。keyTyped(KeyEvente)鍵入某個(gè)鍵時(shí)調(diào)用此方法?!?-15】:通過(guò)案例來(lái)掌握鍵盤(pán)事件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("鍵盤(pán)事件"); }}程序運(yùn)行的結(jié)果:鼠標(biāo)事件

鼠標(biāo)事件的事件源往往與容器相關(guān),當(dāng)鼠標(biāo)進(jìn)入容器、離開(kāi)容器,或者在容器中單擊鼠標(biāo)、拖動(dòng)鼠標(biāo)時(shí)都會(huì)發(fā)生鼠標(biāo)事件。java語(yǔ)言為處理鼠標(biāo)事件提供兩個(gè)接口:MouseListener,MouseMotionListener接口。我們主要講一下MouseListenerMouseListener接口能處理5種鼠標(biāo)事件:按下鼠標(biāo),釋放鼠標(biāo),點(diǎn)擊鼠標(biāo)、鼠標(biāo)進(jìn)入、鼠標(biāo)退出。相應(yīng)的方法有: 表6.11:MouseListener接口方法實(shí)現(xiàn)MouseListener接口就要實(shí)現(xiàn)以上的五種方法,而每一種都有參數(shù)MouseEvent,通過(guò)這個(gè)參數(shù)獲取鼠標(biāo)的當(dāng)前信息。MouseEvent的常用方法。 表6.12:MouseEvent常用方法:方法主要功能mousePressed(MouseEvente)在組件上按下鼠標(biāo)按鈕時(shí)調(diào)用mouseReleased(MouseEvente)在組件上釋放鼠標(biāo)按鈕時(shí)調(diào)用mouseEntered(MouseEvente)當(dāng)鼠標(biāo)進(jìn)入組件時(shí)調(diào)用mouseExited(MouseEvente)當(dāng)鼠標(biāo)退出組件時(shí)調(diào)用mouseClicked(MouseEvente)在組件上單擊(按下并釋放)鼠標(biāo)按鈕時(shí)調(diào)用方法主要功能intgetX()鼠標(biāo)的X坐標(biāo)intgetY()鼠標(biāo)的Y坐標(biāo)getClickCount()鼠標(biāo)被點(diǎn)擊的次數(shù)getSource()獲取發(fā)生鼠標(biāo)的事件源【例6-16】:通過(guò)案例來(lái)掌握鼠標(biāo)事件小應(yīng)用程序設(shè)置了一個(gè)文本區(qū),用于記錄一系列鼠標(biāo)事件。當(dāng)鼠標(biāo)進(jìn)入小應(yīng)用程序窗口時(shí),文本區(qū)顯示“鼠標(biāo)進(jìn)來(lái)”;當(dāng)鼠標(biāo)離開(kāi)窗口時(shí),文本區(qū)顯示“鼠標(biāo)走開(kāi)”;當(dāng)鼠標(biāo)被按下時(shí),文本區(qū)顯示“鼠標(biāo)按下”,當(dāng)鼠標(biāo)被雙擊時(shí),文本區(qū)顯示“鼠標(biāo)雙擊”;并顯示鼠標(biāo)的坐標(biāo)。程序還顯示一個(gè)紅色的圓,當(dāng)點(diǎn)擊鼠標(biāo)時(shí),圓的半徑會(huì)不斷地變大。importjavax.swing.*;importjava.awt.*;importjava.awt.event.*;classMyFrameextendsJFrameimplementsMouseListener{ JTextAreatext; intx,y,r=10; intmouseFlg=0; staticStringmouseStates[]={"鼠標(biāo)單擊","鼠標(biāo)進(jìn)來(lái)","鼠標(biāo)走開(kāi)","鼠標(biāo)雙擊","鼠標(biāo)鍵按下","鼠標(biāo)松開(kāi)"}; 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("鼠標(biāo)事件示意程序"); }}程序運(yùn)行的結(jié)果:事件適配器事件適配器是監(jiān)聽(tīng)器接口的空實(shí)現(xiàn);事件適配器實(shí)現(xiàn)了監(jiān)聽(tīng)器接口,并為接口的每個(gè)方法都提供了空的實(shí)現(xiàn),方法體內(nèi)沒(méi)有具體的代碼。當(dāng)需要?jiǎng)?chuàng)建監(jiān)聽(tīng)器的時(shí)候,可以通過(guò)繼承適配器,而不是實(shí)現(xiàn)監(jiān)聽(tīng)器接口。因?yàn)檫m配器已經(jīng)空實(shí)現(xiàn)了監(jiān)聽(tīng)器接口中所有的方法,因此只要重寫(xiě)自己想要寫(xiě)的方法,從而簡(jiǎn)化事件監(jiān)聽(tīng)器的代碼的編寫(xiě)。事件適配器類:將對(duì)應(yīng)事件接口下所有的方法自動(dòng)實(shí)現(xiàn)。下面以Window和Key事件舉例。WindowListener窗體事件接口,7個(gè)抽象方法,對(duì)應(yīng)的適配器WindowAdapter,類,案例6-17采用WindowAdapter自動(dòng)實(shí)現(xiàn)這7個(gè)抽象方法,重寫(xiě)了3個(gè)方法?!纠?-17】:通過(guò)案例來(lái)掌握窗體適配器類的使用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("我是一個(gè)按鈕")); addWindowListener(newMyWindowListener()); } publicstaticvoidmain(String[]agrs){ JFrameframe=newDemo6_17(); frame.setSize(400,400); frame.setLocation(400,300); frame.setVisible(true); }}classMyWindowListenerextendsWindowAdapter{publicvoidwindowClosing(WindowEvente){//窗口正處在關(guān)閉過(guò)程中時(shí)調(diào)用System.out.println("關(guān)閉狀態(tài)");System.exit(0);}publicvoidwindowActivated(WindowEvente){//激活窗口時(shí)用System.out.println("激活狀態(tài)");}publicvoidwindowOpened(WindowEvente){//已打開(kāi)窗口時(shí)調(diào)用System.out.println("打開(kāi)狀態(tài)");}}程序運(yùn)行的結(jié)果:KeyListener:鍵盤(pán)事件接口,對(duì)應(yīng)的適配器是KeyAdapter,鍵盤(pán)適配器類?!纠?-18】:通過(guò)案例來(lái)掌握鍵盤(pán)適配器類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("您按下的回車(chē)鍵"); } } }); } publicstaticvoidmain(String[]args){newDemo6_18(); }}程序運(yùn)行的結(jié)果:MouseListener鼠標(biāo)事件接口,對(duì)應(yīng)的適配器是MouseAdapter類,鼠標(biāo)適配器類?!纠?-19】:通過(guò)案例來(lái)掌握鼠標(biāo)適配器類importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;publicclassDemo6_19{ privateFramef; privateButtonbt; Demo6_19(){//構(gòu)造方法 madeFrame(); } publicvoidmadeFrame(){ f=

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論