![Java程序設(shè)計項目案例化教程課件:Java圖形用戶界面_第1頁](http://file4.renrendoc.com/view14/M02/20/0B/wKhkGWchdliAaILkAABS7jaO99o419.jpg)
![Java程序設(shè)計項目案例化教程課件:Java圖形用戶界面_第2頁](http://file4.renrendoc.com/view14/M02/20/0B/wKhkGWchdliAaILkAABS7jaO99o4192.jpg)
![Java程序設(shè)計項目案例化教程課件:Java圖形用戶界面_第3頁](http://file4.renrendoc.com/view14/M02/20/0B/wKhkGWchdliAaILkAABS7jaO99o4193.jpg)
![Java程序設(shè)計項目案例化教程課件:Java圖形用戶界面_第4頁](http://file4.renrendoc.com/view14/M02/20/0B/wKhkGWchdliAaILkAABS7jaO99o4194.jpg)
![Java程序設(shè)計項目案例化教程課件:Java圖形用戶界面_第5頁](http://file4.renrendoc.com/view14/M02/20/0B/wKhkGWchdliAaILkAABS7jaO99o4195.jpg)
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
Java面向?qū)ο蟪绦蛟O(shè)計Java圖形用戶界面教學內(nèi)容掌握AWT抽象窗口工具的一些和簡單使用掌握窗體的多種布局設(shè)計的簡單使用掌握SwingGUI工具包的使用掌握圖形用戶界面的事件處理的簡單使用理解事件適配器的原理以及簡單的使用AWTAWT(AbstractWindowToolkit)抽象窗口工具包,是API為Java程序提供的創(chuàng)建圖形用戶界面GUI(GraphicsUserInterface圖形用戶界面)的工具集,該包包含了很多類和接口,用于JavaApplication的GUI編程。使用AWT的類和接口一般在java.awt包及其子包中,AWT包含兩個核心類Container(容器)和Component(組件)類。其類的層次結(jié)構(gòu)如圖6.1所示:容器和組件容器Container是用來組織和容納其他界面成份和元素的組件,Java提供了相應的容器類,例如框架(JFrmae/Frame)、面板(JPanel/Panel)等類。組件Component是圖形用戶界面的基本單位,里面不再包含其他成份。組件是一個可以以圖形化的方式顯示在屏幕上并能與用戶進行交互的對象,例如一個按鈕、一個標簽等。組件不能獨立顯示出來,必須將組件放在一定的容器中才可以顯示出來。有兩種常用的Container:Window:其對象表示自由停泊的頂級窗口Panel:其對象可作為容納其他Component對象,不能獨立存在,必須被添加到其他Container中(如Window或Applet)Frame類Frame類是window的子類,由Frame或其子類創(chuàng)建的對象為一個窗體。1、Frame類的常用構(gòu)造方法如表6.1所示:表6.1構(gòu)造方法主要功能()構(gòu)造一個最初不可見的Frame新實例(title)構(gòu)造一個新的、最初不可見的、具有指定標題的Frame對象
1、
Frame類的常用方法如表6.2所示:表6.2方法主要功能publicvoidsetBounds(intx,inty,intwidth,intheight)設(shè)置窗體的位置和大小。由x和y指定窗體左上角的相對于父窗體的位置,由width和height指定窗體的大小publicvoidsetSize(intwidth,intheight)設(shè)置窗體的大小publicvoidsetLocation(intx,inty)設(shè)置窗體的位置publicvoidsetTitle(title)設(shè)置窗體的標題publicvoidsetBackground(c)設(shè)置窗體的背景顏色publicvoidsetResizable(booleanresizable)設(shè)置此窗體是否可由用戶調(diào)整大小,resizable-如果此窗體是可調(diào)整大小的,則為true;否則為falsepublicvoidsetVisible(booleanb)根據(jù)參數(shù)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); }}程序運行的結(jié)果:該窗體左上角的位置離它的父窗體左邊、上邊的坐標點是(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);}}程序運行的結(jié)果:Panel類Panel類是Container的子類,Panel對象可以看成可以容納Component的空間。1、Panel類的常用構(gòu)造方法如表6.3所示:表6.32、Panel類的常用方法(從父類繼承過來的方法)如表6.4所示:表6.4構(gòu)造方法主要功能()使用默認的布局管理器創(chuàng)建新面板(layout)創(chuàng)建具有指定布局管理器的新面板方法主要功能publicvoidsetBounds(intx,inty,intwidth,intheight)設(shè)置Panel的位置和大小。由x和y指定窗體左上角的相對于父窗體的位置,由width和height指定窗體的大小publicvoidsetSize(intwidth,intheight)設(shè)置Panel的大小publicvoidsetLocation(intx,inty)設(shè)置Panel的位置publicvoidsetBackground(c)設(shè)置Panel的背景顏色publicvoidsetLayout(mgr)設(shè)置Panel的布局管理器【例6-3】:通過案例來掌握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);}}程序運行的結(jié)果:【例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); }}程序運行的結(jié)果:【例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); }}程序運行結(jié)果:布局設(shè)計Container可以存放組件,組件的排列方式一般情況下有兩種,一種是人工排列,一種是利用Java提供的布局管理器對這些組件進行排列,布局管理器直接設(shè)置這些組件的位置、大小和排列順序,不同的布局管理器采用不同的算法和策略。Java提供的布局管理器主要有5中:FlowLayout、BorderLayout、GridLayout、CardLayout、GridBagLayout布局。FlowLayout流布局,是Panel、Applet的默認布局,該布局對組件從上到下,從左到右進行布置,一行排滿后換行。不改變組件的大小,按組件的原有尺寸顯示組件,可設(shè)置不同的組件的間距、行距以及對齊方式。FlowLayout默認的對齊方式是居中對齊。FlowLayout的構(gòu)造方法如表6.5所示:表6.5FlowLayout布局構(gòu)造方法主要功能()構(gòu)造一個新的FlowLayout,它是居中對齊的,默認的水平和垂直間隙是5個單位(intalign)構(gòu)造一個新的FlowLayout,它具有指定的對齊方式,默認的水平和垂直間隙是5個單位(intalign,inthgap,intvgap)創(chuàng)建一個新的流布局管理器,它具有指定的對齊方式以及指定的水平和垂直間隙【例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); }}程序運行的結(jié)果:如果改變Frame的大小,F(xiàn)rame中的組件的布局也會隨之改變BorderLayout是Window類及其子類的默認布局管理器,它將容器分為5個部分,分別命名為EAST、WEST、SOUTH、NORTH和CENTER。每個部分只能放一個組件,如果希望在某個部分放置多個組件,可以先放一個容器對象,然后在容器里面放置多個組件和容器。BorderLayout類提供了的構(gòu)造方法如表6.6所示:表6.61、如不指定組件的加入部位,則默認加入到CENTER區(qū),每個區(qū)域只能加入一個組件,如加入多個,則先前加入的會被覆蓋。2、BorderLayout型布局容器尺寸縮放原則:北、南兩個區(qū)域在水平方向縮放。東、西兩個區(qū)域在垂直方向縮放。中部區(qū)域可在兩個方法上都縮放。BorderLayout布局構(gòu)造方法主要功能()構(gòu)造一個組件之間沒有間距的新邊框布局。(inthgap,intvgap)構(gòu)造一個具有指定組件間距的邊框布局?!纠?-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); }}程序運行的結(jié)果:GridLayout布局GridLayout布局管理器是將空間劃分成規(guī)則的矩形網(wǎng)格,每個單元格區(qū)域大小相等。組件被添加到每個單元格中,先從左到右填滿一行后換行,再從上到下。GridLayout類的構(gòu)造方法如表5-2所示:表6.7構(gòu)造方法主要功能()創(chuàng)建具有默認值的網(wǎng)格布局,即每個組件占據(jù)一行一列。(introws,intcols)創(chuàng)建具有指定行數(shù)和列數(shù)的網(wǎng)格布局。(introws,intcols,inthgap,intvgap)創(chuàng)建具有指定行數(shù)和列數(shù)的網(wǎng)格布局?!纠?-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); }}程序運行的結(jié)果:CardLayout布局CardLayout布局CardLayout類的常用方法: 表6.8方法主要功能publicCardLayout()創(chuàng)建一個間距大小為0的新卡片布局publicCardLayout(inth,intv)創(chuàng)建一個具有指定水平間距和垂直間距的新卡片布局。水平間距置于左右邊緣。垂直間距置于上下邊緣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】:通過案例來掌握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(){//直接翻轉(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();}}程序運行的結(jié)果:wing
JavaSwing是JDK1.2后版本引入的第二代GUI類庫,具有更好的可移植性,提供了更完整的組件,增加了許多功能和特性。Swing可以擴展和簡化跨平臺應用程序的開發(fā)。Swing系列由17個包組成,每個包都有其獨特的用途。正如您將在短期課程中學到的一樣,這些軟件包使您可以輕松整合各種具有高度復雜性和用戶友好性的應用程序。Swing的特點:1、純JAVA實現(xiàn)輕量級,不依賴操作系統(tǒng),具有更好的跨平臺性,2、采用MVC設(shè)計思想3、采用要插入式外觀感覺,在同一平臺可有不同的外觀展示4、建立在AWT基礎(chǔ)之上,不能完全舍棄AWTSwing基本組件組件分為容器類和非容器類,容器類是指那些能放入其它組件的組件,而非容器類是指JButtonLabel等。容器類分為頂層容器和非頂層容器。頂層容器是指有獨立窗口諸如Window這樣的,而Windwo最常用的子類是JFrame和JDialog頂層容器可以獨立存在,包括JFrame、JDialog、JApplet、JWindow(JDialog不可以獨立存在)。JFrame是大多數(shù)應用程序的基本窗口,有邊框、標題和按鈕,允許程序員把其他組件添加到它里面,把它們組織起來,并把它們呈現(xiàn)給用戶。中間容器不能獨立存在,必須放在頂層容器內(nèi),且能夠容納其他控件,包括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={"文學","政治","經(jīng)濟","科技","醫(yī)學"}; 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; }}程序運行的結(jié)果:事件處理圖形界面上的事件是指在某個組件上發(fā)生用戶操作。例如,用戶單擊了界面上的某個按鈕,就說在這個按鈕上發(fā)生了事件,這個按鈕對象就是事件的擊發(fā)者。對事件作監(jiān)視的對象稱為監(jiān)視器,監(jiān)視器提供響應事件的處理方法。為了讓監(jiān)視器與事件對象關(guān)聯(lián)起來,需要對事件對象作監(jiān)視器注冊,告訴系統(tǒng)事件對象的監(jiān)視器。其整個處理過程是這樣的,事件源可以注冊事件監(jiān)聽器對象,并可以向事件監(jiān)聽器對象發(fā)送事件對象。事件發(fā)生后,事件源將事件對象發(fā)給已經(jīng)注冊的所有事件監(jiān)聽器。監(jiān)聽器對象隨后會根據(jù)事件對象內(nèi)的相應方法響應這個事件。Java事件處理三要素:1、事件源(EventSource):即事件發(fā)生的場所,就是指各個組件,如按鈕等,點擊按鈕其實就是組件上發(fā)生的一個事件;2、事件(Event):事件封裝了組件上發(fā)生的事情,比如按鈕單擊、按鈕松開等等;3、事件監(jiān)聽器(EventListener):負責監(jiān)聽事件源上發(fā)生的特定類型的事件,當事件到來時還必須負責處理相應的事件;事件處理機制在java語言中,為了便于系統(tǒng)管理事件,也為了便于程序作監(jiān)視器注冊,系統(tǒng)將事件分類,稱為事件類型。系統(tǒng)為每個事件類型提供一個接口。要作為監(jiān)視器對象的類必須實現(xiàn)相應的接口,提供接口規(guī)定的響應事件的方法。以程序響應按鈕事件為例,JButton類對象button可以是一個事件的激發(fā)者。當用戶點擊界面中與button對應的按鈕時,button對象就會產(chǎn)生一個ActionEvent類型的事件。如果監(jiān)視器對象是obj,對象obj的類是Obj,則類Obj必須實現(xiàn)AWT中的ActionListener接口,實現(xiàn)監(jiān)視按鈕事件的actionPerformed方法。button對象必須用addActionListener方法注冊它的監(jiān)視器obj。程序運行時,當用戶點擊button對象對應的按鈕時,系統(tǒng)就將一個ActionEvent對象從事件激發(fā)對象傳遞到監(jiān)視器。ActionEvent對象包含的信息包括事件發(fā)生在哪一個按鈕,以及有關(guān)該事件的其他信息。實際事件發(fā)生時,通常會產(chǎn)生一系列的事件,例如,用戶點擊按鈕,會產(chǎn)生ChangeEvent事件提示光標到了按鈕上,接著又是一個ChangeEvent事件表示鼠標被按下,然后是ActionEvent事件表示鼠標已松開,但光標依舊在按鈕上,最后是ChangeEvent事件,表示光標已離開按鈕。但是應用程序通常只處理按下按鈕的完整動作的單個ActionEvent事件。每個事件類型都有一個相應的監(jiān)視器接口,下面列出了每個接口的方法。實現(xiàn)監(jiān)視器接口的類必須實現(xiàn)所有定義在接口中的方法。外部類實現(xiàn)監(jiān)聽器【例6-11】通過案例來掌握外部類實現(xiàn)監(jiān)聽器importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;publicclassDemo6_11extendsJFrame{ JButtonbutton1,button2; JPanelpane1,pane2,p1,p2; CardLayoutcard1=newCardLayout(); Demo6_11(){ this.setTitle("外部類實現(xiàn)事件監(jiān)聽"); 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,"綠色"); } }}程序運行的結(jié)果類本身實現(xiàn)監(jiān)聽器【例6-12】通過案例來掌握類本身實現(xiàn)監(jiān)聽器importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;publicclassDemo6_12extendsJFrameimplementsActionListener{ JButtonbutton1,button2; JPanelpane1,pane2,p1,p2;CardLayoutcard1=newCardLayout();Demo6_12(){this.setTitle("自身類實現(xiàn)事件監(jiān)聽");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();}}程序運行的結(jié)果:運行結(jié)果與上個案例相同
內(nèi)部類實現(xiàn)監(jiān)聽器【例6-13】通過案例來掌握內(nèi)部類實現(xiàn)監(jiān)聽器importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;publicclassDemo6_13extendsJFrame{ JButtonbutton1,button2; JPanelpane1,pane2,p1,p2; CardLayoutcard1=newCardLayout(); Demo6_13(){ this.setTitle("內(nèi)部類實現(xiàn)事件監(jiān)聽");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)方法,該方法有一個整型參數(shù),返回值類型為ArrayList類對象。方法的功能是求0到x中所有整數(shù)的階乘值,將結(jié)果存放到ArrayList類對象中并返回結(jié)果。大整數(shù)不是基本數(shù)據(jù)類型,它們的乘法不能用運算符*,而要使用大整數(shù)類的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,"綠色"); } }}程序運行的結(jié)果:運行結(jié)果與上個案例相同匿名內(nèi)部類實現(xiàn)監(jiān)聽器【例6-14】通過案例來掌握匿名內(nèi)部類實現(xiàn)監(jiān)聽器importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;publicclassDemo6_14extendsJFrame{ JButtonbutton1,button2; JPanelpane1,pane2,p1,p2; CardLayoutcard1=newCardLayout(); Demo6_14(){ this.setTitle("匿名內(nèi)部類實現(xiàn)事件監(jiān)聽");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(); }}程序運行的結(jié)果:運行結(jié)果與上個案例相同鍵盤事件
鍵盤事件的事件源一般與該組件相關(guān),當一個組件處于激活狀態(tài)時,按下、釋放或敲擊鍵盤上的某個鍵時就會發(fā)生鍵盤事件。鍵盤事件的接口是KeyListener,注冊鍵盤事件監(jiān)視器的方法是addKeyListener(監(jiān)視器)。實現(xiàn)KeyListener接口有3個要實現(xiàn)的方法: 表6.10:KeyListener接口方法方法主要功能keyPressed(KeyEvente)按下某個鍵時調(diào)用此方法。keyReleased(KeyEvente)釋放某個鍵時調(diào)用此方法。keyTyped(KeyEvente)鍵入某個鍵時調(diào)用此方法?!?-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("鍵盤事件"); }}程序運行的結(jié)果:鼠標事件
鼠標事件的事件源往往與容器相關(guān),當鼠標進入容器、離開容器,或者在容器中單擊鼠標、拖動鼠標時都會發(fā)生鼠標事件。java語言為處理鼠標事件提供兩個接口:MouseListener,MouseMotionListener接口。我們主要講一下MouseListenerMouseListener接口能處理5種鼠標事件:按下鼠標,釋放鼠標,點擊鼠標、鼠標進入、鼠標退出。相應的方法有: 表6.11:MouseListener接口方法實現(xiàn)MouseListener接口就要實現(xiàn)以上的五種方法,而每一種都有參數(shù)MouseEvent,通過這個參數(shù)獲取鼠標的當前信息。MouseEvent的常用方法。 表6.12:MouseEvent常用方法:方法主要功能mousePressed(MouseEvente)在組件上按下鼠標按鈕時調(diào)用mouseReleased(MouseEvente)在組件上釋放鼠標按鈕時調(diào)用mouseEntered(MouseEvente)當鼠標進入組件時調(diào)用mouseExited(MouseEvente)當鼠標退出組件時調(diào)用mouseClicked(MouseEvente)在組件上單擊(按下并釋放)鼠標按鈕時調(diào)用方法主要功能intgetX()鼠標的X坐標intgetY()鼠標的Y坐標getClickCount()鼠標被點擊的次數(shù)getSource()獲取發(fā)生鼠標的事件源【例6-16】:通過案例來掌握鼠標事件小應用程序設(shè)置了一個文本區(qū),用于記錄一系列鼠標事件。當鼠標進入小應用程序窗口時,文本區(qū)顯示“鼠標進來”;當鼠標離開窗口時,文本區(qū)顯示“鼠標走開”;當鼠標被按下時,文本區(qū)顯示“鼠標按下”,當鼠標被雙擊時,文本區(qū)顯示“鼠標雙擊”;并顯示鼠標的坐標。程序還顯示一個紅色的圓,當點擊鼠標時,圓的半徑會不斷地變大。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("鼠標事件示意程序"); }}程序運行的結(jié)果:事件適配器事件適配器是監(jiān)聽器接口的空實現(xiàn);事件適配器實現(xiàn)了監(jiān)聽器接口,并為接口的每個方法都提供了空的實現(xiàn),方法體內(nèi)沒有具體的代碼。當需要創(chuàng)建監(jiān)聽器的時候,可以通過繼承適配器,而不是實現(xiàn)監(jiān)聽器接口。因為適配器已經(jīng)空實現(xiàn)了監(jiān)聽器接口中所有的方法,因此只要重寫自己想要寫的方法,從而簡化事件監(jiān)聽器的代碼的編寫。事件適配器類:將對應事件接口下所有的方法自動實現(xiàn)。下面以Window和Key事件舉例。WindowListener窗體事件接口,7個抽象方法,對應的適配器WindowAdapter,類,案例6-17采用WindowAdapter自動實現(xiàn)這7個抽象方法,重寫了3個方法?!纠?-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){//窗口正處在關(guān)閉過程中時調(diào)用System.out.println("關(guān)閉狀態(tài)");System.exit(0);}publicvoidwindowActivated(WindowEvente){//激活窗口時用System.out.println("激活狀態(tài)");}publicvoidwindowOpened(WindowEvente){//已打開窗口時調(diào)用System.out.println("打開狀態(tài)");}}程序運行的結(jié)果:KeyListener:鍵盤事件接口,對應的適配器是KeyAdapter,鍵盤適配器類?!纠?-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(); }}程序運行的結(jié)果:MouseListener鼠標事件接口,對應的適配器是MouseAdapter類,鼠標適配器類。【例6-19】:通過案例來掌握鼠標適配器類importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;publicclassDemo6_19{ privateFramef; privateButtonbt; Demo6_19(){//構(gòu)造方法 madeFrame(); } publicvoidmadeFrame(){ f=
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 現(xiàn)代商務場合下的著裝與舉止規(guī)范
- 居然之家國慶節(jié)活動方案
- 現(xiàn)代農(nóng)業(yè)旅游產(chǎn)業(yè)鏈構(gòu)建與農(nóng)業(yè)可持續(xù)發(fā)展
- 未來生態(tài)社區(qū)的規(guī)劃與水環(huán)境關(guān)系探討
- 災害預防教育在學校的推廣與應用
- 匯報邏輯清晰度職場的制勝法寶
- 6 飛向藍天的恐龍說課稿-2023-2024學年四年級下冊語文統(tǒng)編版
- 2023九年級物理上冊 第四章 探究電流4.3 導體對電流阻礙作用說課稿 (新版)教科版
- 2 送元二使安西(說課稿)- 2024-2025學年部編版語文六年級上冊
- 2024-2025學年高中數(shù)學 第一章 集合與常用邏輯用語 1.4.2 充要條件說課稿 新人教A版必修第一冊001
- 護士如何提高病情觀察的能力
- 醫(yī)?;鸨O(jiān)管培訓課件
- 產(chǎn)程中的人文關(guān)懷護理
- 開工第一課安全教育記錄表
- 2024年黑龍江農(nóng)業(yè)職業(yè)技術(shù)學院高職單招(英語/數(shù)學/語文)筆試歷年參考題庫含答案解析
- 部編版小學語文四年級下冊教師教學用書(教學參考)完整版
- 基于數(shù)據(jù)驅(qū)動的鋰離子電池剩余使用壽命預測方法研究
- 《內(nèi)臟疾病康復》課件
- 串通招投標法律問題研究
- 高原鐵路建設(shè)衛(wèi)生保障
- 家具廠各崗位責任制匯編
評論
0/150
提交評論