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

What is the Java AWT implementation event handling flow?

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

Share

Shulou(Shulou.com)05/31 Report--

Today, I would like to share with you the relevant knowledge of what the Java AWT event handling flow is, detailed in content and clear in logic. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article.

Event handling of AWT

Event handling is mainly in response to user actions

Event object (Event): encapsulates a specific event that occurs on a GUI component (usually an action by the user).

Event source (component): the place where an event occurs, usually the component that produces the event

Listener: an object that listens for events that occur on an event source and handles various events accordingly (the object contains event handlers).

Event handler: the way in which the listener object handles the received event object accordingly.

Event inheritance Graph in AWT

An event listener class MyWindowListener that implements the WindowListener interface is created in the program. When the window is bound to the listener object by the addWindowListener () method, and the close button is clicked, the listener object's windowClosing () method is triggered to hide and release the current window, thus closing the window.

The code is as follows

Package AWT; import java.awt.*; import java.awt.event.WindowEvent; import java.awt.event.WindowListener; public class Example08 {public static void main (String [] args) {Frame f=new Frame ("my World"); f.setSize (400300); f.setLocation (300200); f.setVisible (true); / / register a listener for the window MyWindowListener mw=new MyWindowListener () F.addWindowListener (mw);}} class MyWindowListener implements WindowListener {@ Override public void windowOpened (WindowEvent e) {} @ Override public void windowClosing (WindowEvent e) {Window window= e.getWindow (); window.setVisible (false); window.dispose () @ Override public void windowClosed (WindowEvent e) {} @ Override public void windowIconified (WindowEvent e) {} @ Override public void windowDeiconified (WindowEvent e) {} @ Override public void windowActivated (WindowEvent e) {} @ Override public void windowDeactivated (WindowEvent e) {}}

Result

Event adapter

In order to solve the problem of empty implementation of the generated method, jdk provides some adapter classes, which are the default implementation classes of the listener interface. These implementation classes implement all the methods in the interface, but there is no code in the methods. The program can achieve the purpose of implementing the listener interface by inheriting the adapter class.

Package AWT; import java.awt.*; import java.awt.Frame; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.awt.event.WindowListener; public class Example08 {public static void main (String [] args) {Frame f=new Frame ("my World"); f.setSize (400300); f.setLocation (300200); f.setVisible (true) / / register a listener MyWindowListener mw=new MyWindowListener (); f.addWindowListener (mw);}} / / inherit the WindowAdapter class and override the windowClosing () method class MyWindowListener1 extends WindowAdapter {@ Override public void windowClosing (WindowEvent e) {Window window= (Window) e.getComponent (); window.dispose ();}}

Because the MyWindowListener class inherits the adapter class WindowAdapter, because the function of the implementation is to close the window, you only need to rewrite the windowClosing () method. Almost all listener interfaces have corresponding adapter classes. When you need to handle those events to implement the listener interface by inheriting the adapter class, you directly override the method corresponding to the event.

Using anonymous inner classes to implement event handling

The event adapter above listens to the event source object by inheriting the adapter class, but for the sake of simplicity of the code, you can use anonymous inner classes to create listener objects for events and handle events that occur.

Add a button with a click event to the window

Package AWT; import java.awt.*; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; public class NiMing {public static void main (String [] args) {Frame f=new Frame (my World); f.setSize (400300); f.setLocation (300200); f.setVisible (true); Button btn=new Button ("Exit"); f.add (btn) Btn.addMouseListener (new MouseAdapter () {@ Override public void mouseClicked (MouseEvent e) {System.exit (0);});}}

First, we call the addMouseListner () method of btn, in which we register a mouse event listener to the button with the method of anonymous inner class, because we only need to listen to the click event of the button, use the MouseAdapter adapter class, and rewrite the mouseClicked () method. When the button is clicked, the click event will be passed to the event listener as an object to exit the program.

These are all the contents of the article "what is the Java AWT implementation event handling flow?" Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

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