Java event handling mechanisms and Adapters

Source: Internet
Author: User

The most important thing is to understand the concepts of event sources , monitors , and interfaces that handle events .
1. Event Source: An object capable of producing time can be called an event source, such as a text box, a button, a drop-down list, and so on.

2. Monitor: Event source to generate an event, there must be something to monitor it so that the corresponding Ah, that is the monitor, the role of the monitor is to monitor the event source in order to process the time that occurs.

The event source registers its own listener with the appropriate method. such as AddListener (listener);
3. Interface for handling events: The monitor is responsible for handling events that occur at the source of the event, and the monitor is an object that, in order to handle events that occur at the event source, automatically calls a method to handle the event. The method that is called is the method in the interface that handles the event.

java specifies that the class for this object must declare the implementation of the appropriate interface, that is, all methods in the class weight rewrite interface, and the monitor will automatically invoke the interface method overridden by the class when the event source has an event event.
Handling Events:

Common listening interfaces for Java:

In addition, there are Mousemotionlistener,mousewheellistener,windowfocuslistener,
Windowstatelistener, monitor the interface.

Sometimes there are many abstract methods in the listening interface, in the implementation of the interface, you need to rewrite all the methods, but we often only use one or two of them, which is undoubtedly a kind of exertion, superfluous.
So there's the adapter class , and for the sake of simplicity, each listener interface with multiple methods is equipped with an adapter class that implements all the methods in the interface, but each method doesn't do anything.
Like what:
Focusadapter,keyadapter,mouseadapter,mousemotionadapter,
Windowadapter.

This provides another way to inherit the adapter class, overriding the methods that need to be used , and not having to rewrite all the methods, which is simple and convenient.

class A extends WindowAdapter{   void windowClosing(WinwEvent e){//重写WindowAdapter中的方法        //处理时间代码   }}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Java event handling mechanisms and Adapters

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.