Java用戶界面技術(shù)_第1頁
Java用戶界面技術(shù)_第2頁
Java用戶界面技術(shù)_第3頁
Java用戶界面技術(shù)_第4頁
Java用戶界面技術(shù)_第5頁
已閱讀5頁,還剩35頁未讀, 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

第10課用戶界面設(shè)計參見《Java語言與面向?qū)ο蟪绦蛟O(shè)計》的第7章,第8章8.3節(jié)AWT容器容器Container能夠用來存放別的組件。有兩種類型的容器:Window和Panel。Frame容器Window是能獨立存在的容器,它有一個子類Frame。Frame有一個構(gòu)造方法Frame(Stringtitle)你可以通過add()方法,在Frame中加入其他的組件。Frame被創(chuàng)建后,它是不可見的.參見FrameShower.java

FrameShower.javapackagegui;importjava.awt.*;publicclassFrameShower{publicstaticvoidmain(Stringargs[]){Framef=newFrame("hello");f.add(newButton("PressMe"));f.setSize(100,100);f.setVisible(true);}}Panel容器

Panel只能存在于其他的容器(Window或其子類)中.通過Panel的默認(rèn)構(gòu)造方法Panel()可以創(chuàng)建一個Panel。參見MyFrame.javaMyFrame.javapackagegui;importjava.awt.*;publicclassMyFrameextendsFrame{Panelpanel=newPanel();Buttonbutton=newButton("pressme");publicMyFrame(Stringtitle){super(title);panel.add(button);panel.setBackground(Color.yellow);add(panel);setBackground(Color.blue);setSize(500,500);setVisible(true);}publicstaticvoidmain(Stringargs[]){MyFramef=newMyFrame("hello");}}布局管理器一.取消布局管理器setLayout(null)二.默認(rèn)布局管理器Window,F(xiàn)rame和Dialog的默認(rèn)布局管理器是BorderLayoutPanel和Applet的默認(rèn)布局管理器是FlowLayout。取消布局管理器publicMyFrame(Stringtitle){super(title);

panel.setLayout(null);panel.setSize(200,200);panel.setLocation(50,50);button.setSize(80,50);button.setLocation(80,80);panel.add(button);panel.setBackground(Color.yellow);setLayout(null);add(panel);setBackground(Color.blue);setSize(500,500);setVisible(true);}yx布局管理器布局管理器分為5種:-FlowLayout流式布局管理器-BorderLayout邊界布局管理器-GridLayout網(wǎng)格布局管理器-CardLayout卡片布局管理器-GridBagLayout網(wǎng)格包布局管理器

布局管理器練習(xí)運行MyFlow.java運行BorderLayoutTester.java運行GridEx.java運行CardLayoutTester.java運行GridBagEx1.java改變?nèi)萜髦薪M件的布局,觀看顯示效果FlowLayoutpublicclassMyFlow{privateFramef;privateButtonbutton1,button2,button3;publicstaticvoidmain(Stringargs[]){MyFlowmflow=newMyFlow();mflow.go();}publicvoidgo(){f=newFrame("FlowLayout");f.setLayout(newFlowLayout());button1=newButton("Ok");button2=newButton("Open");button3=newButton("Close");f.add(button1);f.add(button2);f.add(button3);f.setSize(600,600);f.setVisible(true);}}BorderLayoutpublicclassBorderLayoutTester{privateFramef;privateButtonbn,bs,bw,be,bc;publicstaticvoidmain(Stringargs[]){BorderLayoutTesterguiWindow2=newBorderLayoutTester();guiWindow2.go();}publicvoidgo(){f=newFrame("BorderLayout");//f.setLayout(newBorderLayout());bn=newButton("B1");bs=newButton("B2");be=newButton("B3");bw=newButton("B4");bc=newButton("B5");f.add(bn,BorderLayout.NORTH);f.add(bs,BorderLayout.SOUTH);f.add(be,BorderLayout.EAST);f.add(bw,BorderLayout.WEST);f.add(bc,BorderLayout.CENTER);f.add(newButton("hello"));f.pack();f.setVisible(true);}}GridLayoutpublicclassGridEx{privateFramef;privateButtonb1,b2,b3,b4,b5,b6;publicstaticvoidmain(Stringargs[]){GridExgrid=newGridEx();grid.go();}publicvoidgo(){f=newFrame("Gridexample");f.setLayout(newGridLayout(3,2));b1=newButton("1");b2=newButton("2");b3=newButton("3");b4=newButton("4");b5=newButton("5");b6=newButton("6");f.add(b1);f.add(b2);f.add(b3);f.add(b4);f.add(b5);f.add(b6);f.setSize(500,500);f.setVisible(true);}}CardLayoutpublicclassCardLayoutTester{publicstaticvoidmain(Stringargs[]){Panelp1,p2,p3;Framef=newFrame("CardTest");CardLayoutmyCard=newCardLayout();f.setLayout(myCard);p1=newPanel();p2=newPanel();p3=newPanel();f.setBackground(Color.white);p1.setBackground(Color.black);p2.setBackground(Color.blue);p3.setBackground(Color.red);f.add(p1,"First");f.add(p2,"Second");f.add(p3,"Third");myCard.show(f,"Second");f.setSize(200,200);f.setVisible(true);}}

創(chuàng)建面板及復(fù)雜布局

參見ExGui3.java

創(chuàng)建面板及復(fù)雜布局

參見ExGui4.java事件處理每一個可以觸發(fā)事件的組件被當(dāng)作事件源.每一種事件都對應(yīng)專門的監(jiān)聽者。監(jiān)聽者用來接收和處理這種事件。一個事件源可以觸發(fā)多種事件,如果它注冊了某種事件對應(yīng)的監(jiān)聽者,那么這種事件就會被接收和處理。這種模式被稱為"委托模型"。事件處理的軟件實現(xiàn)事件類(XXXEvent)事件監(jiān)聽接口(XXXListener)組件的注冊監(jiān)聽接口方法(addXXXListener()方法)事件處理1.用內(nèi)部類實現(xiàn)監(jiān)聽接口 參看EventTester1.java2.將容器類實現(xiàn)監(jiān)聽接口 參看EventTester2.java3.定義專門的外部類實現(xiàn)監(jiān)聽接口參看EventTester3.java4.采用事件適配器參看EventTester4.java5.一個組件注冊多個監(jiān)聽者參看EventTester5.java用內(nèi)部類實現(xiàn)監(jiān)聽接口publicclassEventTester1extendsFrame{staticintcount=1;publicEventTester1(Stringtitle){super(title);}publicstaticvoidmain(Stringargs[]){EventTester1f=newEventTester1("hello");f.setLayout(newFlowLayout());

finalButtonb=newButton("1");

b.addActionListener(newActionListener(){//declareanInnerclasspublicvoidactionPerformed(ActionEventevt){

b.setLabel(newInteger(++count).toString());}});f.add(b);f.setSize(100,100);f.setBackground(Color.blue);f.setVisible(true);}}將容器類實現(xiàn)監(jiān)聽接口publicclassEventTester2extendsFrameimplementsActionListener{intcount=1;Buttonb;publicEventTester2(Stringtitle){super(title);setLayout(newFlowLayout());b=newButton("1");

b.addActionListener(this);//SampleitselfisanActionListeneradd(b);setSize(100,100);setBackground(Color.blue);setVisible(true);}publicstaticvoidmain(Stringargs[]){EventTester2f=newEventTester2("hello");}

publicvoidactionPerformed(ActionEventevt){b.setLabel(newInteger(++count).toString());}}定義專門的外部類實現(xiàn)監(jiān)聽接口publicclassEventTester3extendsFrame{Buttonb;Buttonb1;publicEventTester3(Stringtitle){super(title);setLayout(newFlowLayout());b=newButton("1");

b.addActionListener(newMyListener(1));add(b);b1=newButton("notregistred");add(b1);setSize(100,100);setBackground(Color.blue);setVisible(true);}publicstaticvoidmain(Stringargs[]){EventTester3f=newEventTester3("hello");}}classMyListenerimplementsActionListener{intcount;publicMyListener(intcount){this.count=count;}publicvoidactionPerformed(ActionEventevt){

Buttonb=(Button)evt.getSource();b.setLabel(newInteger(++count).toString());}}采用事件適配器publicclassEventTester4extendsFrame{Buttonb;publicEventTester4(Stringtitle){super(title);setLayout(newFlowLayout());b=newButton("1");

b.addMouseListener(newMyMouseListener(1));add(b);setSize(100,100);setBackground(Color.blue);setVisible(true);}publicstaticvoidmain(Stringargs[]){EventTester4f=newEventTester4("hello");}}classMyMouseListenerextendsMouseAdapter{intcount;publicMyMouseListener(intcount){this.count=count;}publicvoidmousePressed(MouseEventevt){Buttonb=(Button)evt.getSource();//geteventsourceb.setLabel(newInteger(++count).toString());}}mousePressed(MouseEvent)mouseReleased(MouseEvent)mouseEntered(MouseEvent)mouseExited(MouseEvent)mouseClicked(MouseEvent)一個組件注冊多個監(jiān)聽者publicclassEventTester5extendsFrame{Buttonb;publicEventTester5(Stringtitle){super(title);setLayout(newFlowLayout());b=newButton("Mouse1");

b.addMouseListener(newMyMouseListener1(1));//registerMyMouseListenerb.addActionListener(newMyActionListener(1));//registerMyActionListeneradd(b);setSize(300,300);setBackground(Color.blue);setVisible(true);}publicstaticvoidmain(Stringargs[]){EventTester5f=newEventTester5("hello");}}事件類classMyListenerimplementsActionListener{intcount;publicMyListener(intcount){this.count=count;}publicvoidactionPerformed(ActionEventevt){

Buttonb=(Button)evt.getSource();

b.setLabel(newInteger(++count).toString());}}事件監(jiān)聽接口組件注冊監(jiān)聽接口組件可以通過addXXXListener方法(XXX表示某種事件)注冊監(jiān)聽者。子類組件繼承父類的所有注冊監(jiān)聽者的方法。

事件處理練習(xí):為計算器加上事件處理,使它能進(jìn)行簡單的計算ExGui4.java-->Calculater.java

AWT繪圖在Component類中提供了三個和繪圖有關(guān)的方法:paint(Graphicsg):繪制組件的外觀。update(Graphicsg):調(diào)用paint()方法,刷新組件的外觀。repaint():調(diào)用update()方法,刷新組件的外觀。Graphics類提供了繪制各種圖形的方法drawLine(intx1,inty1,intx2,inty2):畫一條直線drawString(Stringstring,intleft,intbottom):寫一個字符串drawImage(Imageimage,intleft,inttop,ImageObserverobserver):畫一個圖片drawRect(intleft,inttop,intwidth,intheight):畫一個矩形drawOval(intx,inty,intwidth,intheight):畫一個橢圓fillRect(intleft,inttop,intwidth,intheight):填充一個矩形fillOval(intx,inty,intwidth,intheight)//填充一個橢圓AWT繪圖repaint()調(diào)用update(),update()調(diào)用paint()一個繪圖例子(SampleDrawer.java)按下[ChangeColor]按鈕SampleDrawer.javapublicclassSampleDrawerextendsFrameimplementsActionListener{Colorcolor=Color.red;Buttonb;publicSampleDrawer(Stringtitle){super(title);setLayout(newFlowLayout());b=newButton("ChangeColor");

b.addActionListener(this);add(b);setSize(300,300);setVisible(true);}publicvoidpaint(Graphicsg){

g.setColor(color);g.fillRect(100,100,100,100);g.setColor(Color.black);g.fillRect(0,100,100,100);g.fillRect(200,100,100,100);g.fillRect(0,200,300,100);}

publicstaticvoidmain(Stringargs[]){SampleDrawerf=newSampleDrawer("hello");}

publicvoidactionPerformed(ActionEventevt){if(color==Color.red)color=Color.green;elsecolor=Color.red;repaint();//callrepaint()method}}隨機(jī)畫橢圓OvalDrawer.javaOvalDrawer類的paint()方法負(fù)責(zé)畫一個橢圓,OvalDrawer類還實現(xiàn)了Runnable接口,在run()方法中,每隔400毫秒就會隨機(jī)的設(shè)置橢圓的起始坐標(biāo)(x,y)、橢圓的寬width和高h(yuǎn)eight,然后調(diào)用OvalDrawer的repaint()方法刷新界面。publicvoidrun(){while(true){x=(int)(Math.random()*300);y=(int)(Math.random()*300);width=(int)(Math.random()*100);height=(int)(Math.random()*100);color=colors[(int)(Math.random()*(colors.length-1))];repaint();try{Thread.sleep(400);}catch(InterruptedExceptione){thrownewRuntimeException(e);}}}Swing組件在java.awt包中,提供了各種具體的組件,如窗體Frame、面板Panel、按鈕Button、文本框TextField和文本區(qū)域TextArea等。AWT組件的優(yōu)點是簡單、穩(wěn)定,兼容于任何一個JDK版本,缺點是依賴于本地操作系統(tǒng)的GUI,缺乏平臺獨立性。為了使用Java創(chuàng)建的圖形界面也能夠跨平臺,即在不同操作系統(tǒng)中保持相同的外觀,從JDK1.2版本開始引入了Swing組件,這些Swing組件位于javax.swing包中,成為JDK基礎(chǔ)類庫的一部分。Swing組件是用純Java語言編寫而成的,不依賴于本地操作系統(tǒng)的GUI,Swing組件可以跨平臺運行。獨立于本地平臺的Swing組件被稱為輕量級組件,而依賴于本地平臺的AWT組件被稱為重量級組件。JComponent多數(shù)Swing組件的父類為javax.swing.JComponentJFrameJFrame與Frame的最大區(qū)別在于前者不能直接通過add()方法加入組件,也不能直接通過setLayout()方法設(shè)置布局。

//以下代碼非法JFramejFrame=newJFrame("Hello");jFrame.setLayout(newGridLayout(2,1));jFrame.add(jLabel);jFrame.add(jButton);JFrame每個JFrame都有一個與之關(guān)聯(lián)的內(nèi)容面板(contentPane),只能針對這個contentPane設(shè)置布局,以及加入組件:JFramejFrame=newJFrame("Hello");//獲得與JFrame關(guān)聯(lián)的contentPane,contentPane默認(rèn)的布局管理器為BorderLayoutContainercontentPane=jFrame.getContentPane();contentPane.setLayout(newGridLayout(2,1));contentPane.add(jLabel);contentPane.add(jButton);JFrameJFrame的setDefaultCloseOperation(intoperation)方法用來決定如何響應(yīng)用戶關(guān)閉窗體的操作,參數(shù)operation有以下可選值:JFrame.DO_NOTHING_ON_CLOSE:什么也不做。JFrame.HIDE_ON_CLOSE:隱藏窗體,這是JFrame的默認(rèn)選項。JFra

溫馨提示

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

評論

0/150

提交評論