AWT Event Model

Source: Internet
Author: User

    • 1. What is an event

1) Event------describes what happened to the object [event is similar to an exception, is composed of a class, when an event is generated, it is actually the corresponding event of the class to generate an object, which encapsulates the information related to this event, We can get event-related information through this object].

2) Event source------Event Generator [For example, a button].

3) Event handlers------methods of receiving events, interpreting events, and handling user interaction [Note is a method].

If the user performs an action at the user interface layer (mouse clicks and keystrokes), this causes an event to occur. An event is an object that describes what happened. There are different types of event classes used to describe various types of user interaction.

    • 2. Event Source

The event source is the creator of an event. For example, clicking the mouse on the button component produces a actionevent[with the button as the source of the event that the JDK gives us the mouse click button, which is the object of this class ActionEvent. This ActionEvent instance is an object that contains information about the event that occurred just now, including:

1) Getactioncommand-Returns the command name associated with the action

2) getwhen-returns the time that the event occurred

To view the ActionEvent class in the JDK Doc document:

java.awt.eventClass ActionEventjava.lang.Object  java.util.EventObject      java.awt.AWTEvent          java.awt.event.ActionEventpublic class ActionEventextends AWTEvent    A semantic event which indicates that a component-defined action occurred.

[Events in Java are typically located under the Java.awt.event package

This class represents a semantic event that indicates that a component-defined action has occurred, such as clicking on a button, and this event occurs. It is actually a class, and we can access it through some of its objects, such as Etactioncommand () and Getwhen ().

    • 3. Event handler

An event handler is a way to receive events, interpret events, and handle user interaction. [In the final analysis is a way to handle the action of a click, and when the Click event occurs, the event handler is automatically called.] ]

    • JDK1.1 Event Model: the delegate model

Event Listener: A class that implements the listener interface. A listener object is an instance of a class that implements a specialized listener interface.

The demo is as follows:

Package com.ahueir.awt;Import Java.awt.BorderLayout;Import Java.awt.Button;Import Java.awt.Frame;Import java.awt.event.ActionEvent;Import Java.awt.event.ActionListener;PublicClassTestbutton {PublicStaticvoidMain (string[] args) {Frame frame =New Frame ("Test button"); Button Button =new button ( "press me!"); //increase event handler Button.addactionlistener (new ButtonHandler ()); Frame.add (button, borderlayout.center); Frame.pack (); Frame.setvisible (true);}} Class Buttonhandler implements actionlistener{/* * This side why give actionperformed () Pass a actionevent argument e? Why not pass a string? * The reason is that when we click on the button, an event object is generated that encapsulates the information of all objects when the object is generated, including the label on the button, the time when the event occurred, etc. * The object generated by this actionevent is similar to the one previously mentioned by the underlying system to help us generate the exception. * This object is passed to the event handler associated with this event, and the event handler is called to get some information about the encapsulation. */ @Override public void actionperformed (ActionEvent e) {System.out.println ( "button is Pressed !"); String str = E.getactioncommand (); System.out.println (str); }}

The compile execution result produces a button that clicks this button to output the following (not on this side):

Button is pressed!

Press me!

"Description 1":

1) When you click with the mouse on the button object, a ActionEvent event is sent. This ActionEvent event is received by all ActionListener actionperformed () methods that are registered using the addActionListener () method.

2) The Getactioncommand () method of the ActionEvent class returns the command name associated with the action.

3) Take the button's click action as an example to return the label of the buttons.

"Description 2":

View the button in the JDK Doc document class to see the method inside addActionListener ()

addActionListener   public void addActionListener(ActionListener l)Adds the specified action listener to receive action events from this button.

[Add a specific action when the supervisor hears the event received from this button].

    • Delegate Model Benefits

1) events are not handled unexpectedly.

2) It is possible to create and classify event actions using the adapter (adapter) class.

3) The delegate model facilitates the distribution of work to various classes. [For example, there are frame, Panel, button three components, we can write three event handlers, can be processed for these three components, click on a component to correspond to an event manager for processing, the corresponding relationship is relatively clear].

AWT Event Model

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.