Java程序设计教案(第五章)_第1页
Java程序设计教案(第五章)_第2页
Java程序设计教案(第五章)_第3页
Java程序设计教案(第五章)_第4页
Java程序设计教案(第五章)_第5页
已阅读5页,还剩33页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

Java程序设计教案(第五章)Java程序设计教案(第五章)38/38PAGE38Java程序设计教案(第五章)Java程序设计教案(第五章)教案纸第5章Java的图形用户界面(6学时)【主要讲授内容及时间分配】5.1图形用户界面概述(25分钟)5.2AWT中常用类的层次结构(20分钟)5.3基本组件的使用(45分钟)5.4布局管理器(45分钟)5.5事件处理(90分钟)5.6菜单、绘图类的使用(45分钟)【重点与难点】1、重点:(1)基本组件的使用,包括Label、Button、TextField、TextArea、List、Checkbox和CheckboxGroup的构造方法和常用方法的使用。(2)布局管理器的使用,包括FlowLayout、GridLayout、BorderLayout。(3)事件处理。2、难点:事件处理。【教学要求】1、可以构造出图形用户界面;2、可以为界面上的组件编写相应的事件处理代码;3、可以构造带有菜单的应用程序;4、可以写小游戏。【实施方法】课堂讲授,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(); }}事件处理事件类事件源事件监听接口处理事件的方法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点击按钮关闭程序。(法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点击按钮关闭程序。(法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("增加优先级");privateButtondown=newButton("降低优先级");privateListlist=newList();privateTextFieldtaskInput=newTextField();privateLabelpriorityLabel=newLabel("改变优先级");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){ } //鼠标事件处理方法 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("测试鼠标事件"); 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(); } //键盘中的非系统键 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. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论