The event model of Javase Learning 56:gui programming (I.)

Source: Internet
Author: User
Tags ack event listener gettext response code

Overview of an event

Event Monitoring:


(1) Event

A functional operation of a user on a program. The event classes in Java are included in the JDK's Java.awt.event package.

There are two main types of events in Java:

1) Component Class events: ComponentEvent, ContainerEvent, WindowEvent, FocusEvent, PaintEvent, MouseEvent total

Six categories, all of which occur when the state of a component changes.

2) Action event: ActionEvent, TextEvent, AdjustmentEvent, itemevent a total of four categories. They all correspond to a user's function

Sexual action.

(2) Event programming

User programming defines what response the program should respond to when each particular event occurs, and the response code is automatically adjusted by the system when the corresponding event occurs

Use.

(3) Event delegation authorization processing model

The mechanism of the event delegate authorization processing model is implemented in the JDK1.1 version.

1) Event Source: The component of the output event.  

2) Listener: The generation of specific responses to events generated by the component, i.e., event output and processing by two different classes (they can be placed in a separate

In the same program) are programmed to be implemented.

3) Event handling mechanism: The AWT component itself does not program the corresponding event, and the polygon is left to the event listener (it can be the container class in which the component resides or another

Java program classes, as long as they implement the relevant event listener Interface) processing (Event authorization processing model).

4) event-handling packages

Java.awt.event package, which provides the classes and interfaces required for AWT events

ActionEvent class corresponds to ActionListener interface;

MouseEvent class corresponds to Mousemotionlistener interface and MouseListener interface;

The Windonevent class corresponds to the Windonlistener interface---that is, an event of type xxxevent occurs, and the interface that handles the event is

Xxxlistener); Their parent class is the EventObject class.

description of each event class:

EventObject: Super class for all event classes

The most important method--getsource (), returns the object that generated the event

awtevent: Super class for all AWT event classes

The most important method--GetID (), returns the ID number of an event whose ID is an integer that specifies the type of event, such as a button event or mouse point

Hit event

ActionEvent: Events that occur when the component is activated

AdjustmentEvent: Events that occur when adjusting an adjustable component, such as moving a scroll bar

ComponentEvent: A high-level event that occurs when manipulating a component

containerevent: Occurs when a component is added or removed to a container

InputEvent: A high-level event generated by an input device

itemevent: Occurs when selected from a Select item, check box, or list

KeyEvent: Occurs when operating the keyboard

MouseEvent: Occurs when manipulating the mouse

PaintEvent: An event that occurs when a component is depicted

textevent: Occurs when text is changed

WindowEvent: An event that occurs when the window is manipulated, such as maximizing or minimizing a window.

(5) Basic principles of event programming

Event-handling class code to handle a class of events, the interface to which they correspond should be implemented, and all the event rings defined in the interface are given

The function of the function is implemented (overriding its function body), and then the Listener (responder) of the event is registered when the component is created.

(6) Event Registration

The event source registers a specific event to specify who the Listener (responder) of the event is.

(7) Event Registration function

The function name consists of the listener interface name corresponding to the "Add + Event Type", and the function parameter is the Listener object (the object that implements the event response class, such as capacity

The Listener object applies this representation in response to the event itself.

public void add<listenertype> (<listenerType> listenerobj) {       //function Body}

(8) Event programming steps

1) The listener interface that implements an event (defines the event-handling class and implements the Listener interface).

2) override (implement) the function body of its event handling in the event-handling class.

3) Register the event handling code when creating the AWT component to specify who the Listener (responder) of the event is.

two ActionEvent class and ActionListener Interface

(1) ActionEvent class

ActionEvent indicates that a semantic event has occurred for the action defined by the component. When a component-specific action (such as being pressed) occurs, the component (such as

Button) to generate this high-level event. Events are passed to each of the ActionListener objects, which are the addActionListener () method of using the component

Registered to receive such an event.

The object that implements the ActionListener interface gets this actionevent when an event occurs. Therefore, the listener does not have to handle individual mouse movements and mouse orders

And can handle "meaningful" (semantic) events like "press button".

construction methods and methods of the ActionEvent class:


(2) ActionListener interface

The listener interface for receiving operation events. Classes that are interested in handling operation events can implement this interface, and objects created with that class may use the group

addActionListener () method to register the component. When an action event occurs, the Actionperformed () method of the object is called.

The ActionListener interface has only one method:


Instance code:

Testactionevent.java Source code:

Import Java.awt.*;import java.awt.event.*;/*  example Name: Java Event Handling Example *  source file name: Testactionevent.java *  to  point: *  1. Java Event handling mechanism *  2. Event source, Event listener concept and role *  3. How to register listeners on an existing component */public class testactionevent{public    static void main ( String args[]) {frame f = new Frame ("Java window"); Button B = New button ("Click me! "); Monitor BH = new monitor (); B.addactionlistener (BH); F.add (B,borderlayout.center); F.pack (); f.setvisible (true);}    } Class Monitor implements ActionListener {public    void actionperformed (ActionEvent e) {        System.out.println ("a Button has been pressed ");}        }

Operation Result:


Testactionevent2.java Source code:

Import Java.awt.*;import java.awt.event.*;/* Example name: Java Event Handling Example * source file name: Testactionevent2.java * to  point: *  1. Multiple listeners can be registered at the same time on an event source component. A listener object can be registered to multiple event source components at the same time. The information for the event source can be automatically passed to all registered listeners with the event it fires */public class testactionevent2{public    static void Main (String args[]) {        Frame f = new Frame ("Java window"); Button B1 = New button ("Start"); Button B2 = New button ("End"); Monitor2 bh = new Monitor2 ();                A listener object listens on two button object B1.addactionlistener (BH);       B2.addactionlistener (BH), B2.setactioncommand ("Game Over"), F.add (B1, "North");       F.add (B2, "Center"); F.pack ();        F.setvisible (True);}    } Class Monitor2 implements actionlistener{public    void actionperformed (ActionEvent e) {    System.out.println (" Be clicked, "+" I am: "+ e.getactioncommand ());}    }

Operation Result:


Three textfiled class

The Java.awt.TextFiled class is used to create a text box object. The TextField object is a text component that allows editing of single-line text.

How to construct the Textfiled class:


Common methods of the Textfiled class:





The Textfiled object may have an action (the cursor will hit enter in the text box) event. The event class that corresponds to the event is

Java.awt.event.ActionEvent.

The object used to handle the ActionEvent event is the class that implements the Java.awt.event.ActionListener interface. ActionListener Interface Definition

Method:

public void actionperformed (ActionEvent e) {     //method body}

The class that implements the interface adds the statement that handles the event (Action) in the method.

Use the addActionListener (ActionListener L) method to register a ActionListener object for the Textfiled object when the Textfiled object occurs

Action event, a ActionEvent object is generated that passes the actionperformed () method of the object as a parameter to the ActionListener object in the square

The information of the object can be obtained in the method and processed accordingly.
Instance code:

Tfactionevent.java Source code:

Import Java.awt.*;import java.awt.event.*;p ublic class tfactionevent{public static void Main (string[] args) {new tfframe ();}} Class Tfframe extends frame{//construction method Tfframe () {TextField tf = new TextField (); add (TF); Tf.addactionlistener (new Tfactionlistener ());p ack (); setvisible (True);}} Class Tfactionlistener implements Actionlistener{public void actionperformed (ActionEvent e) {TextField tf = (TextField) E.getsource (); System.out.println (Tf.gettext ()); Tf.settext ("");}}

Operation Result:


Tfpassword.java Source code:

Import Java.awt.*;import java.awt.event.*;p ublic class Tfpassword {public static void main (string[] args) {new TFFrame2 () ;}} Class TFFrame2 extends Frame{tfframe2 () {TextField tf = new TextField (); add (TF); Tf.addactionlistener (new TFActionListener2 ());//Set the echo character Tf.setechochar (' * ');p ack (); setvisible (True);}} Class TFActionListener2 implements Actionlistener{public void actionperformed (ActionEvent e) {TextField tf = (TextField) E.getsource (); System.out.println (Tf.gettext ()); Tf.settext ("");}}

Operation Result:


Above



The event model of Javase Learning 56:gui programming (I.)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.