Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Java event handling mechanism and how to use the adapter

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article focuses on "Java event handling mechanism and how to use the adapter", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn the Java event handling mechanism and how to use the adapter.

Java event handling mechanism and adapter

The most important thing is to understand the concept of event source, monitor, and interface for handling events.

1. Event source: objects that can generate time can be called event sources, such as text boxes, buttons, drop-down lists, and so on.

two。 Monitor: for an event source to produce an event, there must be something to monitor it in order to make a response, that is, the monitor, the role of the monitor is to monitor the event source in order to process the time when it occurred.

The event source registers its own listener through the appropriate method. Such as addListener (listener)

3. Interface for handling events: the monitor is responsible for handling events that occur from the event source. The monitor is an object. In order to handle events from the event source, the monitor object automatically calls a method to handle the event. The method being called is the method in the interface that handles the event.

Java stipulates that the class of the monitor object must declare and implement the corresponding interface, that is, all methods in the interface must be rewritten in the class body. When an event occurs in the event source, the monitor will automatically call the interface method overridden by the class.

Schematic diagram of handling events

Listening Interface commonly used in java

In addition, there are MouseMotionListener,MouseWheelListener,WindowFocusListener, WindowStateListener, listening interface.

Sometimes there are many abstract methods in the listening interface, and when implementing the interface, we need to rewrite all the methods, but we often use only one or two of them, which is undoubtedly tiring and redundant.

So there are adapter classes, and for simplification purposes, each listener interface with multiple methods is equipped with an adapter class that implements all the methods in the interface, but each method does nothing.

For example:

FocusAdapter,KeyAdapter,MouseAdapter,MouseMotionAdapter, WindowAdapter.

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

Class An extends WindowAdapter {pubilc void windowClosing (WinwEvent e) {/ / override method / / processing time Code in WindowAdapter}} Action Adapter for Java event Adapter

Actually, it evolved from the interface event. It's the equivalent of a trigger. To put it simply, it's just some action.

For example, the click of the mouse and the keyboard and so on. An adapter is a class that implements interface events, but it's not a real implementation, it's just an empty implementation, and there's no specific method body. The adapter is mainly for the convenience of programmers and avoids the repetition of the code. As long as an object or property adds the adapter, it monitors the object or property. For example, a button.

Press the button to add a MouseAdapter adapter and override the mouseClicked (MouseEvent e) method inside.

Public void mouseClicked (MouseEvent e) {System.out.println ("you clicked the mouse");}

When you click the button, the background prints "you clicked the mouse." To be clear, you have added a mouse object to the button. You can produce the corresponding action through the mouse!

MouseAdapter implements the MouseListener,MouseWheelListener,MouseMotionListener interface, and the methods are empty, you can override the mouseClicked (MouseEvent e) method to respond to the mouse button click event on the component, override the mouseDragged (MouseEvent e) method to respond to the mouse button press and drag event on the component, and so on.

At this point, I believe that you have a deeper understanding of "Java event handling mechanism and how to use the adapter", you might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report