您好,欢迎访问三七文档
Java1.01.1JDK1.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)EventEventObjectargintclickCountintidintkeyintmodifiersObjecttargetintxintylongwhenEvent9ACTION_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().musicsportsartaction(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,3g.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.0handleEvent()Appletcomponent.handleEvent()false,.,actionJava1.0JDK1.0action()handleEvent()AWTactionJDK1.0Java1.1Java1.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
本文标题:Java事件处理
链接地址:https://www.777doc.com/doc-4304255 .html