Java事件处理

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

„„„„„„Java1.01.1„JDK1.01.1ƒ?.ƒ.ƒEvent,,ƒApplet.JDK1.0java.awt.Eventjava.lang.ObjectEventidif(Ev.id==ACTION_EVENT){…}„importjava.awt.Event;„Eventjava.awt,,,,AWT,.„„:„handEvent(Eventevt,Objectarg)„:„MouseDown(Eventevt,Objectarg)ƒEventEventObjectargintclickCountintidintkeyintmodifiersObjecttargetintxintylongwhenƒEvent9ACTION_EVENTKEY_ACTIONMOUSE_DOWNMOUSE_ENTERWINDOW_DISTORY9F1DELETEDOWNESCAPEEventHandleEvent()MouseUp()MouseDown()MouseDrag()MouseMove()MouseEnter()MouseExit()keyDown()KeyUp()action()AWT„:1.(?)2.?3.(x,y)„:„publicbooleanmouseDown(Eventevt,intx,inty)„{….}mouseDown(x,y)(Eventevt,intx,inty)(x,y)paint()drawLine„:“”.(MouseClick.html)„:„1.Pointmarks[]=newPoint[20];(init)„2.marks[i++]=newPoint(x,y);(MouseDown)„3.(paint)„g.fillOval(x,y,10,10);importjava.awt.*;importjava.applet.Applet;publicclassCountClickextendsApplet{intCurrentMarks=0;publicbooleanmouseDown(Eventevt,intx,inty){CurrentMarks++;repaint();returntrue;}publicvoidpaint(Graphicsg){g.drawString(+CurrentMarks,10,10);}}keyDown(Eventevt,intkey)EventDOWNENDEndF1F1F2F2F3F3F4F4F5F5F6F6F7F7F8F8F9F9F10F10F11F11F12F12HOMEHomeLEFTPGDNPageDownPGUPPageUpRIGHTUP„:importjava.applet.Applet;importjava.awt.*;{charPresskey;publicbooleankeyDown(Eventevt,intkey){Presskey=(char)key;repaint();returntrue;}publicvoidpaint(Graphicsg){g.drawString(Presskey,10,10);}}„ACTION_EVENT,:action().musicsportsart„action(Eventevt,Objectarg)„evt.target:„(button,checkbox,list,...)EventObjectargintclickCountintidintkeyintmodifiersObjecttargetintxintylongwhen:,.importjava.awt.*;importjava.applet.Applet;publicclassCountClickextendsApplet{intCurrentMarks=0;publicinit(){Buttonb1=newButton(“”);add.b1;}publicbooleanaction(Eventevt,Objectarg){CurrentMarks++;repaint();returntrue;}publicvoidpaint(Graphicsg){g.drawString(+CurrentMarks,10,10);}}10importjava.awt.*;importjava.applet.Applet;publicclassdrawingextendsApplet{booleancirclemark=true;publicinit(){Buttonb1=newButton(“”);Buttonb2=newButton(“”);add.b1;add.b2;}„:„1.()„2.action„,„3„g.drawCirlce(50,50,50,50);„g.drawRect(25,25,75,75);publicvoidpaint(Graphicsg){if(circlemark)g.filloval(10,10,30,30);elseg.fillRect(20,20,40,40};}publicbooleanaction(Eventevt,Objectarg){if(evt.target==b1)circlrmark=true;elsecirclrmark=false;repaint();returntrue;}Demo:classMyButtonextendsButton{MyButton(Stringstr){super(str);}publicbooleanaction(Evente,Objectarg){if(arg==Test){System.out.println(ButtonTestpressed.);}else{System.exit(0);}returntrue;}}publicclassComponentEvent{publicstaticvoidmain(Stringargs[]){Framefr=newFrame(ComponentEventTest1.0);MyButtonmyB1=newMyButton(Test);MyButtonmyB2=newMyButton(Exit);fr.setLayout(newFlowLayout());fr.add(myB1);fr.add(myB2);fr.resize(200,200);fr.show();}}Demo:classMyFrameextendsFrame{MyFrame(Stringstr){super(str);}publicbooleanaction(Evente,Objectarg){if(arg==Test){System.out.println(ButtonTestpressed.);}else{System.exit(0);}returntrue;}}publicclassContainerEvent{publicstaticvoidmain(Stringargs[]){MyFramefr=newMyFrame(ContainerEventTest1.0);ButtonB1=newButton(Test);ButtonB2=newButton(Exit);fr.setLayout(newFlowLayout());fr.add(B1);fr.add(B2);fr.resize(200,200);fr.show();}}JavahandleEvent(Evente)publicbooleanhandleEvent(Eventevt){switch(evt.id){caseEvent.MOUSE_ENTER:returnmouseEnter(evt,evt.x,evt.y);caseEvent.MOUSE_EXIT:returnmouseExit(evt,evt.x,evt.y);caseEvent.MOUSE_MOVE:returnmouseMove(evt,evt.x,evt.y);caseEvent.MOUSE_DRAG:returnmouseDrag(evt,evt.x,evt.y);caseEvent.MOUSE_UP:returnmouseUp(evt,evt.x,evt.y);caseEvent.MOUSE_DOWN:returnmouseDown(evt,evt.x,evt.y);caseEvent.KEY_PRESS:caseEvent.KEY_ACTION:returnkeyDown(evt,evt.key);caseEvent.KEY_RELEASE:caseEvent.KEY_ACTION_RELEASE:returnkeyUp(evt,evt.key);caseEvent.ACTION_EVENT:returnaction(evt,evt.arg);caseEvent.GOT_FOCUS:returngotFocus(evt,evt.arg);caseEvent.LOST_FOCUS:returnlostFocus(evt,evt.arg);}returnfalse;}Java1.0„„„handleEvent()Appletcomponent.„handleEvent()false,.„,actionJava1.0JDK1.0action()handleEvent()AWTactionJDK1.0Java1.1„Java1.0Java1.1?„Java1.1,,,ava1.1,,.Java1.1buttonpanelJDK1.1java.awt.eventinterfaceXXXListenerextendsjava.util.EventListener{publicvoidXXXEventMethod(XXXEvente);}interfaceActionListenerextendsjava.util.EventListener{publicvoidactionPerformed(ActionEvente)}interfaceKeyListenerextendsjava.util.EventListener{publicvoidkeyPressed(KeyEvente);publicvoidkeyReleased(KeyEvente);publicvoidkeyTyped(KeyEvente);}Java1.1JDK1.1[listener]ComponentaddXXXListener(ListenerTypelistener)removeXXXListener(ListenerTypelistener)/„:1.classButtonTrackerimplementsActionListener{…}2.()3.„button1.addActionListener(this);„checkbox.addActionListener(this);Demo:Javaimportjava.awt.*;importjava.awt.event.*;publicclasseventTest1implementsWindowListener{publicstaticvoidmain(Stringargs[]){FramefrObj=newFrame(Eventtesting!);Buttonbutton1=newButton(first);Buttonbutton2=newButton(second);frObj.setLayout(newFlowLayout(FlowLayout.CENTER,50,50));frObj.add(button1);frObj.add(button2);frObj.addWindowListener(neweventTest1());frObj.setBounds(100,100,260,160);frObj.setBackground(Color.red);frObj.set

1 / 54
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功