Event and event Source:
An event can be defined as a signal that occurs in a program. the component that can create an event and trigger the event is called the source object or source component of the event. the root class of the event class is Java. util. the eventobject event object includes all the attributes related to the event. You can use the instance method getsource () in eventobject to obtain the event source object. Objects of the eventobject subclass process events of a specific type. Such as Action events, window events, component events, mouse events, and button events. Java uses a delegate-based model to process events: the original object triggers an event and processes the event as an object of interest. An object that is interested in this event is called a listener (listener). To become an event listener on the original object, two conditions are required. (1) Call source. addxlistener (listener) to register an instance. The registration method depends on the event type. If the event type is actionevent, the corresponding registration method is addactionlistener. Generally, the Registration Method of xevent is addxlistener. A source object can trigger several types of events. (2) define the class of the listener object to ensure that the listener has the correct method to handle this event. Java provides a listener interface for each type of events. Generally, the xevent listener interface is xlistener, except for the mousemotionlistener listener. Each listener of actionlistener should implement the actionlistener interface. Register a listener: jbutton jbtok = new jbutton ("OK"); actionlistener listener1 = new oklistenerclass (); jbtok. addactionlistener (listener1); the event object contains event-related information. e can be used to determine whether an object is a button, a check box, or a single-choice button. getsource () method to obtain the source object. Write a program and use two buttons to control the circle size (1) define the listener class named enlargelistener, implement actionlistener (2) create a listener and register it to jbtenlarge (3) add a method named enlarge () in the circlepanel to increase the radius, and then re-draw (4) The action1_med method in enlargelistener to call canvas. enlarge () (5) defines enlargelistener as an internal class of the controlcircle class to enlargelistener for the actionreceivmed method to access the reference variable canvas.
ImportJavax. Swing .*;
ImportJava. AWT .*;
ImportJava. AWT. event .*;
Public Class Controlcircle ExtendsJframe
{
PrivateJbutton jbtenlarge =
NewJbutton ("Enlarge ");
PrivateJbutton jbtshrink =
NewJbutton ("shrink ");
PrivateCirclepanel canvas =
NewCirclepanel ();
PublicControlcircle () {jpanel Panel =
NewJpanel (); panel. Add (jbtenlarge); panel. Add (jbtshrink );
This. Add (canvas, borderlayout.
Center);
This. Add (panel, borderlayout.
South); Jbtenlarge. addactionlistener (
NewEnlargelistener ());}
Public Static VoidMain (string []
ARGs) {jframe frame =
NewControlcircle (); frame. settitle ("controlcircle"); frame. setlocationrelativeto (
Null); Frame. setsize (200,200); frame. setdefaclocloseoperation (jframe.
Exit_on_close); Frame. setvisible (
True);}
ClassEnlargelistener
ImplementsActionlistener {
Public VoidActionreceivmed (actionevent
E) {canvas. Enlarge ();}}
Class Circlepanel ExtendsJpanel {
Private IntRadius =
5;
Public VoidEnlarge () {radius ++; repaint ();}
Protected VoidPaintcomponent (Graphics
G ){
Super. Paintcomponents (g); G. drawoval (getwidth ()/2-radius, getheight ()/2
-Radius, 2 * radius,
2 * radius );}}}
Internal class:The class defined in the range of another class becomes an internal class. Internal classes can be used as regular classes. If an internal class is used only by an external class, it is defined as an internal class. Internal classes can reference objects of internal classes that define data and methods in its nested external classes. Applications that frequently create internal classes in external classes are to merge subordinate classes into the main class, reduces the number of source files for ease of Management