Java用戶界面技術(shù)市公開課一等獎(jiǎng)省賽課獲獎(jiǎng)?wù)n件_第1頁
Java用戶界面技術(shù)市公開課一等獎(jiǎng)省賽課獲獎(jiǎng)?wù)n件_第2頁
Java用戶界面技術(shù)市公開課一等獎(jiǎng)省賽課獲獎(jiǎng)?wù)n件_第3頁
Java用戶界面技術(shù)市公開課一等獎(jiǎng)省賽課獲獎(jiǎng)?wù)n件_第4頁
Java用戶界面技術(shù)市公開課一等獎(jiǎng)省賽課獲獎(jiǎng)?wù)n件_第5頁
已閱讀5頁,還剩35頁未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡介

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

Java用戶界面技術(shù)第3頁FrameShower.javapackagegui;importjava.awt.*;publicclassFrameShower{publicstaticvoidmain(Stringargs[]){Framef=newFrame("hello");f.add(newButton("PressMe"));f.setSize(100,100);f.setVisible(true);}}Java用戶界面技術(shù)第4頁P(yáng)anel容器

Panel只能存在于其它容器(Window或其子類)中.經(jīng)過Panel默認(rèn)結(jié)構(gòu)方法Panel()能夠創(chuàng)建一個(gè)Panel。參見MyFrame.javaJava用戶界面技術(shù)第5頁MyFrame.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");}}Java用戶界面技術(shù)第6頁布局管理器一.取消布局管理器setLayout(null)二.默認(rèn)布局管理器Window,F(xiàn)rame和Dialog默認(rèn)布局管理器是BorderLayoutPanel和Applet默認(rèn)布局管理器是FlowLayout。Java用戶界面技術(shù)第7頁取消布局管理器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);}yxJava用戶界面技術(shù)第8頁布局管理器布局管理器分為5種:-FlowLayout流式布局管理器-BorderLayout邊界布局管理器-GridLayout網(wǎng)格布局管理器-CardLayout卡片布局管理器-GridBagLayout網(wǎng)格包布局管理器Java用戶界面技術(shù)第9頁

布局管理器練習(xí)運(yùn)行MyFlow.java運(yùn)行BorderLayoutTester.java運(yùn)行GridEx.java運(yùn)行CardLayoutTester.java運(yùn)行GridBagEx1.java改變?nèi)萜髦薪M件布局,觀看顯示效果Java用戶界面技術(shù)第10頁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);}}Java用戶界面技術(shù)第11頁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);}}Java用戶界面技術(shù)第12頁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);}}Java用戶界面技術(shù)第13頁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);}}Java用戶界面技術(shù)第14頁

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

參見ExGui3.javaJava用戶界面技術(shù)第15頁

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

參見ExGui4.javaJava用戶界面技術(shù)第16頁事件處理每一個(gè)能夠觸發(fā)事件組件被看成事件源.每一個(gè)事件都對(duì)應(yīng)專門監(jiān)聽者。監(jiān)聽者用來接收和處理這種事件。一個(gè)事件源能夠觸發(fā)各種事件,假如它注冊(cè)了某種事件對(duì)應(yīng)監(jiān)聽者,那么這種事件就會(huì)被接收和處理。這種模式被稱為"委托模型"。Java用戶界面技術(shù)第17頁事件處理軟件實(shí)現(xiàn)事件類(XXXEvent)事件監(jiān)聽接口(XXXListener)組件注冊(cè)監(jiān)聽接口方法(addXXXListener()方法)Java用戶界面技術(shù)第18頁事件處理1.用內(nèi)部類實(shí)現(xiàn)監(jiān)聽接口 參看EventTester1.java2.將容器類實(shí)現(xiàn)監(jiān)聽接口 參看EventTester2.java3.定義專門外部類實(shí)現(xiàn)監(jiān)聽接口參看EventTester3.java4.采取事件適配器參看EventTester4.java5.一個(gè)組件注冊(cè)多個(gè)監(jiān)聽者參看EventTester5.javaJava用戶界面技術(shù)第19頁用內(nèi)部類實(shí)現(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);}}Java用戶界面技術(shù)第20頁將容器類實(shí)現(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());}}Java用戶界面技術(shù)第21頁定義專門外部類實(shí)現(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());}}Java用戶界面技術(shù)第22頁采取事件適配器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)Java用戶界面技術(shù)第23頁一個(gè)組件注冊(cè)多個(gè)監(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");}}Java用戶界面技術(shù)第24頁事件類classMyListenerimplementsActionListener{intcount;publicMyListener(intcount){this.count=count;}publicvoidactionPerformed(ActionEventevt){

Buttonb=(Button)evt.getSource();

b.setLabel(newInteger(++count).toString());}}Java用戶界面技術(shù)第25頁事件監(jiān)聽接口Java用戶界面技術(shù)第26頁組件注冊(cè)監(jiān)聽接口組件能夠經(jīng)過addXXXListener方法(XXX表示某種事件)注冊(cè)監(jiān)聽者。子類組件繼承父類全部注冊(cè)監(jiān)聽者方法。

Java用戶界面技術(shù)第27頁事件處理練習(xí):為計(jì)算器加上事件處理,使它能進(jìn)行簡單計(jì)算ExGui4.java-->Calculater.java

Java用戶界面技術(shù)第28頁AWT繪圖在Component類中提供了三個(gè)和繪圖相關(guān)方法:paint(Graphicsg):繪制組件外觀。update(Graphicsg):調(diào)用paint()方法,刷新組件外觀。repaint():調(diào)用update()方法,刷新組件外觀。Graphics類提供了繪制各種圖形方法drawLine(intx1,inty1,intx2,inty2):畫一條直線drawString(Stringstring,intleft,intbottom):寫一個(gè)字符串drawImage(Imageimage,intleft,inttop,ImageObserverobserver):畫一個(gè)圖片drawRect(intleft,inttop,intwidth,intheight):畫一個(gè)矩形drawOval(intx,inty,intwidth,intheight):畫一個(gè)橢圓fillRect(intleft,inttop,intwidth,intheight):填充一個(gè)矩形fillOval(intx,inty,intwidth,intheight)//填充一個(gè)橢圓Java用戶界面技術(shù)第29頁AWT繪圖repaint()調(diào)用update(),update()調(diào)用paint()Java用戶界面技術(shù)第30頁一個(gè)繪圖例子(SampleDrawer.java)按下[ChangeColor]按鈕Java用戶界面技術(shù)第31頁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}}Java用戶界面技術(shù)第32頁隨機(jī)畫橢圓OvalDrawer.javaOvalDrawer類paint()方法負(fù)責(zé)畫一個(gè)橢圓,OvalDrawer類還實(shí)現(xiàn)了Runnable接口,在run()方法中,每隔400毫秒就會(huì)隨機(jī)設(shè)置橢圓起始坐標(biāo)(x,y)、橢圓寬width和高h(yuǎn)eight,然后調(diào)用OvalDrawerrepaint()方法刷新界面。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);}}}Java用戶界面技術(shù)第33頁Swing組件在java.awt包中,提供了各種詳細(xì)組件,如窗體Frame、面板Panel、按鈕Button、文本框TextField和文本區(qū)域TextArea等。AWT組件優(yōu)點(diǎn)是簡單、穩(wěn)定,兼容于任何一個(gè)JDK版本,缺點(diǎn)是依賴于當(dāng)?shù)夭僮飨到y(tǒng)GUI,缺乏平臺(tái)獨(dú)立性。為了使用Java創(chuàng)建圖形界面也能夠跨平臺(tái),即在不一樣操作系統(tǒng)中保持相同外觀,從JDK1.2版本開始引入了Swing組件,這些Swing組件位于javax.swing包中,成為JDK基礎(chǔ)類庫一部分。Swing組件是用純Java語言編寫而成,不依賴于當(dāng)?shù)夭僮飨到y(tǒng)GUI,Swing組件能夠跨平臺(tái)運(yùn)行。獨(dú)立于當(dāng)?shù)仄脚_(tái)Swing組件被稱為輕量級(jí)組件,而依賴于當(dāng)?shù)仄脚_(tái)AWT組件被稱為重量級(jí)組件。Java用戶界面技術(shù)第34頁JComponent多數(shù)Swing組件父類為javax.swing.JComponentJava用戶界面技術(shù)第35頁JFrameJFrame與Frame最大區(qū)分在于前者不能直接經(jīng)過add()方法加入組件,也不能直接經(jīng)過setLayout()方法設(shè)置布局。

//以下代碼非法JFramejFrame=newJFrame("Hello");jFrame.setLayout(newGridLayout(2,1));jFrame.add(jLabel);jFrame.add(jButton);Java用戶界面技術(shù)第36頁JFrame每個(gè)JFrame都有一個(gè)與之關(guān)聯(lián)內(nèi)容面板(contentPane),只能針對(duì)這個(gè)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);Java用戶界面技術(shù)第37頁JFrameJFramesetDefaultCloseOperation(intoperation)方法用來決定怎樣響應(yīng)用戶關(guān)閉窗體操作,參數(shù)operation有以下可選值:JFrame.DO_NOTHING_ON_CLOSE:什么也不做。JFrame.HIDE_ON_CLOSE:隱藏窗體,這是JFra

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(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)論