版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
Java程序設(shè)計(jì)教案(第五章)Java程序設(shè)計(jì)教案(第五章)38/38PAGE38Java程序設(shè)計(jì)教案(第五章)Java程序設(shè)計(jì)教案(第五章)教案紙第5章Java的圖形用戶界面(6學(xué)時(shí))【主要講授內(nèi)容及時(shí)間分配】5.1圖形用戶界面概述(25分鐘)5.2AWT中常用類的層次結(jié)構(gòu)(20分鐘)5.3基本組件的使用(45分鐘)5.4布局管理器(45分鐘)5.5事件處理(90分鐘)5.6菜單、繪圖類的使用(45分鐘)【重點(diǎn)與難點(diǎn)】1、重點(diǎn):(1)基本組件的使用,包括Label、Button、TextField、TextArea、List、Checkbox和CheckboxGroup的構(gòu)造方法和常用方法的使用。(2)布局管理器的使用,包括FlowLayout、GridLayout、BorderLayout。(3)事件處理。2、難點(diǎn):事件處理。【教學(xué)要求】1、可以構(gòu)造出圖形用戶界面;2、可以為界面上的組件編寫相應(yīng)的事件處理代碼;3、可以構(gòu)造帶有菜單的應(yīng)用程序;4、可以寫小游戲?!緦?shí)施方法】課堂講授,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點(diǎn)擊按鈕關(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點(diǎn)擊按鈕關(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)先級(jí)");privateButtondown=newButton("降低優(yōu)先級(jí)");privateListlist=newList();privateTextFieldtaskInput=newTextField();privateLabelpriorityLabel=newLabel("改變優(yōu)先級(jí)");privateLabeltaskLabel=newLabel("工作事項(xiàng):");privateclassWindowCloserextendsWindowAdapter{publicvoidwindowClosing(WindowEvente){System.exit(0); } }publicTaskList(){super("工作事項(xiàng)表");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("測(cè)試鼠標(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等.壓縮文件請(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ì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 二零二五年度環(huán)保工程財(cái)產(chǎn)保全擔(dān)保協(xié)議3篇
- 甘肅2025年甘肅省中醫(yī)藥研究院招聘高層次人才3人筆試歷年參考題庫附帶答案詳解
- 2025版智慧醫(yī)療健康項(xiàng)目承包服務(wù)合同2篇
- 昆明2025年云南昆明市五華區(qū)云銅中學(xué)合同制教師招聘筆試歷年參考題庫附帶答案詳解
- 新疆2025年新疆昌吉州引進(jìn)人才65人筆試歷年參考題庫附帶答案詳解
- 2025年度個(gè)人住房公積金貸款合同(異地購房)4篇
- 2024年滬科新版九年級(jí)歷史上冊(cè)月考試卷
- 2025年浙教版九年級(jí)地理下冊(cè)階段測(cè)試試卷
- 2025年粵教滬科版八年級(jí)歷史上冊(cè)月考試卷
- 2025年度個(gè)人二手房翻新裝修工程合同書
- 急性肺栓塞搶救流程
- 《統(tǒng)計(jì)學(xué)-基于Python》 課件全套 第1-11章 數(shù)據(jù)與Python語言-時(shí)間序列分析和預(yù)測(cè)
- 《形象價(jià)值百萬》課件
- 紅色文化教育國內(nèi)外研究現(xiàn)狀范文十
- 中醫(yī)基礎(chǔ)理論-肝
- 小學(xué)外來人員出入校門登記表
- 《土地利用規(guī)劃學(xué)》完整課件
- GB/T 25283-2023礦產(chǎn)資源綜合勘查評(píng)價(jià)規(guī)范
- 《汽車衡全自動(dòng)智能稱重系統(tǒng)》設(shè)計(jì)方案
- 義務(wù)教育歷史課程標(biāo)準(zhǔn)(2022年版)
- GB/T 2550-2016氣體焊接設(shè)備焊接、切割和類似作業(yè)用橡膠軟管
評(píng)論
0/150
提交評(píng)論