Analysis of event handling mechanism
The Event processing mechanism of swing is actually a delegated Event processing, which is composed of Event source (component), Event, Listener and Event processor (processing method after triggering an Event).
-
Event source
The component that generates the event is called the event source
-
event
An event is an operation (usually completed using various input devices, such as keyboard, mouse, etc.).
-
monitor
The class that contains event processing and is responsible for checking whether an event occurs. If an event occurs, activate the event handler to process it is called the event listening class. Its example is the event listening object. The event listener must implement the event listener interface or inherit the event listener adapter class.
-
Event handler
Event handler is a method to receive event objects and process them accordingly. The event handler is contained in a class.
- Any operating environment that supports GUI should constantly listen to events such as cases or mouse clicks, and then report such events to the running programs. Each program will decide how to respond to these events
- When an event listener is notified that an event has occurred, the relevant information of the event will be encapsulated in an Event object. In java, all event objects are finally derived from java util. Eventobject class
There are four implementation methods of listener
- Own class as event listener
- External classes act as event listeners
- Anonymous inner classes act as event listeners (or lambda expressions)
- Inner class as event listener
The processing mechanism of AWT events in java is as follows:
- Event listener is a class instance that implements listener interface, such as ActionListener
- Event source objects can register listener objects and send event objects to them
- When an event occurs, the event source sends the event object to all registered listeners
- The listener object then uses the information in the event object to decide how to respond to the event
For example, the following listener example:
ActionListener listener = new ...(any Class implements ActionListener); JButton yellowButton = new JButton("btn"); btn.addActionListener(listener);
After that, as long as the button generates an "action event", the listener object will be notified to execute the method corresponding to the event
Implementation of ActionListener
To implement the ActionListener interface, the implementation class must have a method named actionPerformed, which receives an ActionEvent object as a parameter
As long as the user clicks the button, the JButton object creates a ActionEvent object and calls listener.. Actionperformed (event) and pass in the event object.
An event can have multiple listeners. In this way, as long as the user clicks the button, the button will call the actionPerformed method of all listeners
public class MyListener implements ActionListener{ public void actionPerformed(ActionEvent event){ // Matching operation after clicking the button System.out.println("The button was clicked"); } }
Implementation of Action
-
Action interface is a mechanism provided by Swing package to encapsulate commands and associate them to multiple event sources
-
An action is an object that encapsulates the following
- Description of the command (a text string and an optional icon)
- Parameters required to execute the command
The Action interface contains the following methods
void actionPerformed(ActionEvent event); void setEnabled(boolean b); boolean isEnabled(); void putValue(String key,Object value); Object getValue(String key); void addPropertyChangeListener(PropertyChangeListener listener); void removePropertyChangeListener(PropertyChangeListener listener);
-
The first method is the method in the ActionListener interface. In fact, the Action interface extends the ActionListener interface. Therefore, the Action object can be used wherever the ActionListener object is required
-
The next two methods allow the action to be enabled or disabled, and check whether the action is currently enabled
-
The putValue and GetValue methods allow you to store and get any name / value pair in an action object
-
The last two methods let other objects be notified when the properties of the action object change
Note that Action is an interface, not a class. To implement this class, we must implement the above seven methods. After consulting the data, we found that someone provided a class AbstarctAction, which implements all methods except actionPerformed method. We can directly extend this class and provide an actionPerformed method
Example: construct an action object that can execute the color change command
// ColorAction.java public class ColorAction.ja extends AbstractAction{ public ColorAction(String name,Color c){ putValue(Action.NAME,name); putValue("color",c); putValue(Action.SHORT_DESCRIPTION,"Set panel color to" + name.toLowerCase()); } public void actionPerformed(ActionEvent event){ Color c = (Color) getValue("color"); buttonPanel.setBackground(c); } }
Case analysis
In this case, the task is to add four buttons to a panel. The program is responsible for monitoring which button the mouse clicks and displaying the corresponding button on the screen
Operation results:
analysis:
- In this case, there are four button components. Each component adds a different listener. The corresponding four components are BTN1 - > btn1action. The task of the listener is to display which button is clicked in the TextArea after listening to the button being clicked
- For this case, each button component is an event source, each click is an event Action, the corresponding btnAction object is a listener, which is responsible for listening to button click events, and the ActionPerformed inside the btnAction object is the corresponding event handler, After each click of the button, the event processor in the event listener can print the representation of the corresponding case in the TextArea
import java.awt.event.*; import javax.swing.*; import java.awt.*; public class ButtonFrame extends JFrame { public static void main(String[] args) { var bt = new ButtonFrame(); } private JPanel buttonPanel; private JTextArea textArea; public ButtonFrame() { setSize(300, 200);// width height setTitle("Action test"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Create button component JButton btn1 = new JButton("W"); JButton btn2 = new JButton("A"); JButton btn3 = new JButton("S"); JButton btn4 = new JButton("D"); buttonPanel = new JPanel(); textArea = new JTextArea(); // Add button to panel buttonPanel.add(btn1); buttonPanel.add(btn2); buttonPanel.add(btn3); buttonPanel.add(btn4); // Create related actions var btn1Action = new keyAction('W'); btn1.addActionListener(btn1Action); var btn2Action = new keyAction('A'); btn2.addActionListener(btn2Action); var btn3Action = new keyAction('S'); btn3.addActionListener(btn3Action); var btn4Action = new keyAction('D'); btn4.addActionListener(btn4Action); // Add the panel and text area to the Frame add(buttonPanel); add(buttonPanel, BorderLayout.NORTH); add(textArea); setVisible(true); } /** * Which button did you press */ private class keyAction implements ActionListener { private char key; public keyAction(char key) { this.key = key; } public void actionPerformed(ActionEvent event) { System.out.println(key); textArea.setText("Yes" + key + "key"); } } }