版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
Java程序設(shè)計教案(第五章)Java程序設(shè)計教案(第五章)38/38PAGE38Java程序設(shè)計教案(第五章)Java程序設(shè)計教案(第五章)教案紙第5章Java的圖形用戶界面(6學(xué)時)【主要講授內(nèi)容及時間分配】5.1圖形用戶界面概述(25分鐘)5.2AWT中常用類的層次結(jié)構(gòu)(20分鐘)5.3基本組件的使用(45分鐘)5.4布局管理器(45分鐘)5.5事件處理(90分鐘)5.6菜單、繪圖類的使用(45分鐘)【重點與難點】1、重點:(1)基本組件的使用,包括Label、Button、TextField、TextArea、List、Checkbox和CheckboxGroup的構(gòu)造方法和常用方法的使用。(2)布局管理器的使用,包括FlowLayout、GridLayout、BorderLayout。(3)事件處理。2、難點:事件處理?!窘虒W(xué)要求】1、可以構(gòu)造出圖形用戶界面;2、可以為界面上的組件編寫相應(yīng)的事件處理代碼;3、可以構(gòu)造帶有菜單的應(yīng)用程序;4、可以寫小游戲?!緦嵤┓椒ā空n堂講授,PPT配合5.3基本組件的使用1Label類publicclassLabelDemoextendsFrame{ publicLabelDemo() { Labellb1,lb2,lb3; lb1=newLabel("LeftLabel"); lb2=newLabel("CenterLabel",Label.CENTER); lb3=newLabel("RightLabel",Label.RIGHT); lb3.setText("改變我的名字"); lb3.setAlignment(Label.CENTER); //lb3.setVisible(false); setLayout(newFlowLayout()); add(lb1); add(lb2); add(lb3); }publicstaticvoidmain(Stringargs[]){ LabelDemoLd=newLabelDemo(); Ld.setVisible(true); Ld.pack();}}2Button類publicclassMyButtons{ publicstaticvoidmain(Stringargs[]){ Framef=newFrame(); f.setLayout(newFlowLayout()); Buttonbutton1=newButton("Ok"); Buttonbutton2=newButton("Open"); Buttonbutton3=newButton("Close"); f.add(button1); f.add(button2); f.add(button3); f.setSize(300,100); f.setVisible(true); }}3CheckBox類publicclassCheckboxDemo2extendsFrame{publicCheckboxDemo2() { setLayout(newFlowLayout()); StringUniversity[]={"Tsinghua","Pecking","Fudan","Nanki","Tianjin"}; Checkboxc[]=newCheckbox[5]; Labellabel=newLabel("Thereare5University!"); Labellabel2=newLabel("Thereare5University!");Labellabel3=newLabel("Thereare5University!"); add(newLabel("PleasechoicetheUniversity:")); for(inti=0;i<5;i++) { c[i]=newCheckbox(University[i]); add(c[i]); } add(label); add(label2); add(label3); this.pack(); show(); } publicstaticvoidmain(Stringargs[]){ CheckboxDemo2cbd=newCheckboxDemo2(); cbd.setSize(400,500); }}4CheckBoxGroup類publicclassCheckboxGroupDemo2extendsFrame{ publicCheckboxGroupDemo2() {super("CheckboxGroupDemo2"); setLayout(newFlowLayout()); StringUniversity[]={"Tsinghua","Pecking","Fudan","Nanki","Tianjin"}; Checkboxc[]=newCheckbox[5]; Labellabel=newLabel("Thereare5University!"); CheckboxGroupd=newCheckboxGroup(); add(newLabel("PleasechoicetheUniversity:")); for(inti=0;i<5;i++) { c[i]=newCheckbox(University[i],d,true); add(c[i]); } //add(d); add(label); show(); } publicstaticvoidmain(Stringargs[]){ CheckboxGroupDemo2cbg=newCheckboxGroupDemo2(); cbg.setSize(600,700);}}5List類publicclassListTestextendsFrame{ publicListTest() { super("ListTest"); Listlt=newList(6,true); setLayout(newFlowLayout()); lt.addItem("you"); lt.addItem("你"); lt.addItem("I"); lt.addItem("我"); lt.addItem("he"); lt.addItem("他"); add(lt); }publicstaticvoidmain(Stringargs[]){ ListTestlt=newListTest(); lt.setVisible(true); lt.setSize(500,400);}}6TextField類publicclassTextFieldTestextendsFrame{ publicTextFieldTest() { super("TestTextField"); TextFieldtf=newTextField("&&&&&",20); TextAreata=newTextArea("thisisanewtext",6,20); setLayout(newFlowLayout());tf.setEchoChar('*');//tf.setText("9999999"); add(tf); add(ta); show(); } publicstaticvoidmain(Stringargs[]) { TextFieldTesttft=newTextFieldTest(); //tft.setVisible(false); tft.setSize(500,400); tft.pack(); }}事件處理事件類事件源事件監(jiān)聽接口處理事件的方法ActionEventButtonList(雙擊)TextFieldMenuItemActionListenerpublicvoidactionPerformed(ActionEvente)ItemEventCheckboxList(單擊)ChoiceCheckboxMenuItemItemListenerpublicvoiditemStateChanged(\o"classinjava.awt.event"ItemEvente)WindowEventFrameDialogWindowListener=1\*GB3①publicvoidwindowOpened(\o"classinjava.awt.event"WindowEvente)=2\*GB3②publicvoidwindowClosing(\o"classinjava.awt.event"WindowEvente)=3\*GB3③publicvoidwindowClosed(\o"classinjava.awt.event"WindowEvente)=4\*GB3④publicvoidwindowIconified(\o"classinjava.awt.event"WindowEvente)=5\*GB3⑤publicvoidwindowDeiconified(\o"classinjava.awt.event"WindowEvente)=6\*GB3⑥publicvoidwindowActivated(\o"classinjava.awt.event"WindowEvente)=7\*GB3⑦publicvoidwindowDeactivated(\o"classinjava.awt.event"WindowEvente)MouseEventFrameDialogPanelWindowCanvasMouseListenerpublicvoidmouseClicked(\o"classinjava.awt.event"MouseEvent
e)publicvoidmousePressed(\o"classinjava.awt.event"MouseEvent
e)publicvoidmouseReleased(\o"classinjava.awt.event"MouseEvent
e)publicvoidmouseEntered(\o"classinjava.awt.event"MouseEvent
e)publicvoidmouseExited(\o"classinjava.awt.event"MouseEvent
e)MouseEventFrameDialogPanelWindowCanvasMouseMotionListenerpublicvoidmouseDragged(\o"classinjava.awt.event"MouseEvent
e)publicvoidmouseMoved(\o"classinjava.awt.event"MouseEvent
e)事件源是Button例1點擊按鈕關(guān)閉程序。(法1)publicclassMyFirstFrame1extendsFrameimplementsActionListener{privateButtonquit=newButton("Quit");publicMyFirstFrame1(){ super("TestWindow"); add(quit); pack(); show(); quit.addActionListener(this); } publicvoidactionPerformed(ActionEvente){ dispose();// System.exit(0); } publicstaticvoidmain(Stringargs[]){ MyFirstFrame1mf=newMyFirstFrame1();} }例1點擊按鈕關(guān)閉程序。(法2)publicclassMyFirstFrame2extendsFrame{privateButtonquit=newButton("Quit");publicMyFirstFrame2(){ super("TestWindow"); add(quit); pack(); show(); quit.addActionListener(newButtonHander()); }publicstaticvoidmain(Stringargs[]) {newMyFirstFrame2();}}classButtonHandlerimplementsActionListener{publicvoidactionPerformed(ActionEvente){ System.exit(0); }}例2ClickMe(課本例5.4)importjava.awt.*;publicclassClickMeextendsFrameimplementsActionListener{privateButtonquit=newButton("Quit"); privateButtonclick=newButton("Clickhere");privateTextFieldtext=newTextField(10);privatebooleansecondClick=false;publicClickMe(){ super("ClickExample"); setLayout(newFlowLayout()); add(quit); add(click); click.addActionListener(this); quit.addActionListener(this); add(text); pack(); show(); } publicvoidactionPerformed(ActionEvente){ if(e.getSource()==quit) System.exit(0); elseif(e.getSource()==click){ if(secondClick) text.setText("notagain!"); else text.setText("Uh,ittickless"); secondClick=!secondClick; }}publicstaticvoidmain(Stringargs[]){ ClickMemyFrame=newClickMe(); } } 事件源是List課本例5.13publicclassTaskListextendsFrameimplementsActionListener{privateButtonadd=newButton("添加");privateButtondel=newButton("刪除");privateButtonup=newButton("增加優(yōu)先級");privateButtondown=newButton("降低優(yōu)先級");privateListlist=newList();privateTextFieldtaskInput=newTextField();privateLabelpriorityLabel=newLabel("改變優(yōu)先級");privateLabeltaskLabel=newLabel("工作事項:");privateclassWindowCloserextendsWindowAdapter{publicvoidwindowClosing(WindowEvente){System.exit(0); } }publicTaskList(){super("工作事項表");setup();add.addActionListener(this);del.addActionListener(this);up.addActionListener(this);down.addActionListener(this);addWindowListener(newWindowCloser()); list.addActionListener(this);}publicstaticvoidmain(Stringargs[]){TaskListtl=newTaskList();tl.pack();tl.show(); }privatevoidsetup(){Panelbuttons=newPanel();buttons.setLayout(newFlowLayout());buttons.add(add);buttons.add(del);Panelpriorities=newPanel();priorities.setLayout(newFlowLayout());priorities.add(up);priorities.add(priorityLabel);priorities.add(down);Panelinput=newPanel();input.setLayout(newBorderLayout());input.add("West",taskLabel);input.add("Center",taskInput);Paneltop=newPanel();top.setLayout(newGridLayout(2,1));top.add(input);top.add(priorities);setLayout(newBorderLayout());add("Center",list);add("South",buttons);add("North",top);} publicvoidactionPerformed(ActionEvente){if((e.getSource()==add)&&(!taskInput.getText().equals("")))handleAdd(taskInput.getText().trim());elseif((e.getSource()==del)&&(list.getSelectedIndex()>=0))handleDel(list.getSelectedIndex());elseif((e.getSource()==up)&&(list.getSelectedIndex()>0))handleIncPriority(list.getSelectedIndex());elseif ((e.getSource()==down)&&(list.getSelectedIndex()>=0))handleDecPriority(list.getSelectedIndex());elseif(e.getSource()==list)taskInput.setText(list.getSelectedItem());taskInput.requestFocus();}privatevoidhandleAdd(StringnewTask){list.add(newTask);list.select(list.getItemCount()-1);taskInput.setText(""); }privatevoidhandleDel(intpos){list.remove(pos);list.select(pos);}privatevoidhandleIncPriority(intpos){Stringitem=list.getItem(pos);list.remove(pos);list.add(item,pos-1);list.select(pos-1); }privatevoidhandleDecPriority(intpos){if(pos<list.getItemCount()-1){Stringitem=list.getItem(pos);list.remove(pos);list.add(item,pos+1);list.select(pos+1); } }}事件源是Window(WindowListener和WindowAdapter)publicclassMultipleEventTesterextendsFrameimplementsWindowListener,MouseListener,KeyListener{ publicMultipleEventTester(){ addKeyListener(this); addWindowListener(this); addMouseListener(this); setSize(400,400); show(); } //窗口事件處理方法 publicvoidwindowClosing(WindowEventwe){ System.exit(0); } publicvoidwindowOpened(WindowEventwe){ } publicvoidwindowClosed(WindowEventwe){ } publicvoidwindowIconified(WindowEventwe){ } publicvoidwindowDeiconified(WindowEventwe){ } publicvoidwindowActivated(WindowEventwe){ } publicvoidwindowDeactivated(WindowEventwe){ } //鼠標(biāo)事件處理方法 publicvoidmousePressed(MouseEventme){ } publicvoidmouseReleased(MouseEventme){ } publicvoidmouseEntered(MouseEventme){ } publicvoidmouseExited(MouseEventme){ } publicvoidmouseClicked(MouseEventme){ } //鍵盤事件處理方法 publicvoidkeyPressed(KeyEventke){ } publicvoidkeyReleased(KeyEventke){ } publicvoidkeyTyped(KeyEventke){ } // publicstaticvoidmain(Stringargs[]){ MultipleEventTesterp=newMultipleEventTester(); } }事件源是Mouse(MouseListener,MouseMotionListener,MouseAdapter)publicclassMouseEventDemoextendsFrame{ publicMouseEventDemo() { ButtonbtnMouse=newButton("測試鼠標(biāo)事件"); btnMouse.addMouseListener(newMyMouseListener()); btnMouse.addMouseMotionListener(newMyMouseMotionListener()); this.setLayout(newFlowLayout()); this.add(btnMouse); this.setSize(400,300); this.setVisible(true); } publicstaticvoidmain(Stringargs[]) { MouseEventDemoframe=newMouseEventDemo(); frame.addWindowListener(newWindowAdapter(){ publicvoidwindowClosing(WindowEvente) { System.exit(0); } }); } classMyMouseListenerextendsMouseAdapter { publicvoidmouseClicked(MouseEvente) { if(e.isPopupTrigger()) if(e.getClickCount()==2) } } classMyMouseMotionListenerextendsMouseMotionAdapter { publicvoidmouseMoved(MouseEvente) } }}事件源是KeyKeyEventDemo.javapublicclassKeyEventDemoextendsFrame{ privateTextFieldtf=newTextField(10); publicKeyEventDemo() { tf.addKeyListener(newTextfieldListener()); add(tf); pack(); setVisible(true); } publicstaticvoidmain(Stringargs[]) { KeyEventDemoframe=newKeyEventDemo(); frame.addWindowListener(newWindowAdapter(){ publicvoidwindowClosing(WindowEvente) { System.exit(0); } }); } classTextfieldListenerimplementsKeyListener { intnKeycode; //鍵盤按下事件 publicvoidkeyPressed(KeyEvente){ nKeycode=e.getKeyCode();//返回鍵代碼 //返回鍵代碼nKeyCode文字說明 } //鍵盤釋放事件 publicvoidkeyReleased(KeyEvente){ nKeycode=e.getKeyCode(); } //鍵盤中的非系統(tǒng)鍵 publicvoidkeyTyped(KeyEvente){ nKeycode=e.getKeyCode(); } }}繪制圖形Bird類publicclassBirdextendsThread{privateintxdir=2*(1-2*(int)Math.round(Math.random()));privateintydir=2*(1-2*(int)Math.round(Math.random())); privatebooleanrunning=false;privateCagecage=null;protectedintx,y;Imagebird=Toolkit.getDefaultToolkit().getImage("qq.jpg");publicBird(Cage_cage,int_x,int_y){cage=_cage;x=_x;y=_y;start(); }publicvoidstart(){running=true;super.start(); }publicvoidhalt(){running=false; }publicvoidrun(){while(running){move();try{ sleep(120);}catch(InterruptedExceptione){}cage.repaint();}}privatevoidmove(){x+=xdir;y+=ydir;if(x>cage.getSize().width){x=cage.getSize().width;xdir*=(-1); }if(x<0)xdir*=(-1);if(y>cage.getSize().height){y=cage.getSize().height;ydir*=(-1); }if(y<0)ydir*=-1; }publicvoiddraw(Graphicsg){g.drawImage(bird,x,y,30,30,cage); }}Cage類publicclassCageextendsFrameimplementsActionListener{privateButtonquit=newButton("Quit");privateButtonstart=newButton("Start");privateButtonstop=newButton("Stop");privateBirdbirds[]=newBird[20];Imagebird=Toolkit.getDefaultToolkit().getImage("qq.jpg");publicCage(){super("CagewithBirds");setLayout(newFlowLayout());add(quit);quit.addActionListener(this);add(start);start.addActionListener(this);add(stop);stop.addActionListener(this);setSize(400,500);this.setVisible(true);for(inti=0;i<birds.length;i++){intx=(int)(getSize().width*Math.random());inty=(int)(getSize().height*Math.random());birds[i]=newBird(thi
溫馨提示
- 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)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年度特色餐廳廚師團隊合作協(xié)議書4篇
- 2024珠寶首飾買賣合同
- 2025年昆山物業(yè)費調(diào)價與新收費標(biāo)準全面合同2篇
- 2025年河南鄭州熱力集團有限公司招聘筆試參考題庫含答案解析
- 2025年湖南華菱線纜股份有限公司招聘筆試參考題庫含答案解析
- 2025年度家庭保姆雇傭與家庭生活美學(xué)合同4篇
- 2025年消防工程總承包與應(yīng)急響應(yīng)服務(wù)合同
- 2025年社區(qū)宣傳欄制作及公益廣告投放合同3篇
- 二零二五版定制門窗設(shè)計研發(fā)與市場推廣合同4篇
- 湛江科技學(xué)院《語言基礎(chǔ)》2023-2024學(xué)年第一學(xué)期期末試卷
- T-SDLPA 0001-2024 研究型病房建設(shè)和配置標(biāo)準
- (人教PEP2024版)英語一年級上冊Unit 1 教學(xué)課件(新教材)
- 全國職業(yè)院校技能大賽高職組(市政管線(道)數(shù)字化施工賽項)考試題庫(含答案)
- 2024胃腸間質(zhì)瘤(GIST)診療指南更新解讀 2
- 光儲電站儲能系統(tǒng)調(diào)試方案
- 2024年二級建造師繼續(xù)教育題庫及答案(500題)
- 小學(xué)數(shù)學(xué)二年級100以內(nèi)連加連減口算題
- 建設(shè)單位如何做好項目管理
- 三年級上遞等式計算400題
- 一次性餐具配送投標(biāo)方案
- 《中華民族多元一體格局》
評論
0/150
提交評論