Home >Java >javaTutorial >What is the event processing process implemented by Java AWT?
Event processing is mainly to respond to user operations
Event object (Event ): Encapsulates a specific event that occurs on a GUI component (usually an operation by the user).
Event source (component): The place where the event occurs, usually the component that generates the event
Listener (Listener): Responsible for listening to events Events that occur on the source, and objects that handle various events accordingly (the object contains event handlers).
Event handler: The listener object handles the received event object accordingly.
An event listener class MyWindowListener that implements the WindowListener interface is created in the program. After binding the window to the listener object through the addWindowListener() method, clicking the close button will trigger the windowClosing() method of the listener object, hiding and releasing 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("我的世界"); f.setSize(400,300); f.setLocation(300,200); f.setVisible(true); //给窗口注册一个监听器 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
In order to solve the problem of empty implementation of the generated method, jdk Some adapter classes are provided, 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("我的世界"); f.setSize(400,300); f.setLocation(300,200); f.setVisible(true); //给窗口注册一个监听器 MyWindowListener mw=new MyWindowListener(); f.addWindowListener(mw); } } //继承WindowAdapter类,重写windowClosing()方法 class MyWindowListener1 extends WindowAdapter { @Override public void windowClosing(WindowEvent e) { Window window=(Window) e.getComponent(); window.dispose(); } }
Since the MyWindowListener class inherits the adapter class WindowAdapter, and since the function is to close the window, you only need to rewrite the windowClosing() method. Almost all listener interfaces have corresponding adapter classes. By inheriting the adapter When a class implements the listener interface and needs to handle those events, directly override the method corresponding to the event.
Use anonymous inner classes to implement event processing
The above event adapter implements monitoring of the event source object by inheriting the adapter class, but for the simplicity of the code, you can use anonymous inner classes to create events The listener object handles the 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("我的世界"); f.setSize(400,300); f.setLocation(300,200); 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, the addMouseListner() method of btn is called. In this method, a mouse event listener is registered for the button using the anonymous inner class method. Because we only need to monitor the click event of the button, we 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. .
The above is the detailed content of What is the event processing process implemented by Java AWT?. For more information, please follow other related articles on the PHP Chinese website!