This is the 11th day of my participation in the Gwen Challenge in November. Check out the details: The last Gwen Challenge in 2021

Hello! Hello, everyone! I’m Grey Ape!

Today I will summarize the basic functions and functions of the action event listener function in Java GUI programming.

We know in the Java form of the component, itself is not have any function, so we must give control to add the corresponding event monitoring, in order to trigger the corresponding event processing in the interface, today to share with you in Java action event listener use process

Introduction to Listening Events

First of all, what listening event?

In swing event model, event processing is completed by three separate objects, namely event source, event and listener. The event source triggers an event, which is listened by one or more listeners, and the listener triggers the event and executes the corresponding event.

An event listener, on the other hand, is an “implement specific listener interface” class object. An event is almost always represented as an object. It is an object of an event class, and the event source (a control, such as a button) generates an event object when the user takes the corresponding action (such as clicking a button).

Note that all event sources have the addXXXListener() and removeXXXListener() methods, where XXX indicates the type of listener to be added. The former means to add the corresponding listener and the latter means to remove the corresponding listener.

Two common types of event listeners in Java are “action event listeners” and “focus event listeners”. Let me share with you two common event listening mechanisms.

Action event listeners

ActionEvent listener is a more commonly used listener in swing. Many events in Java forms need to use it to monitor, such as our common button click event, etc. The following is the interface of ActionEvent listener and common event source:

The name of the event

The event source

Listening to the interface

Methods to add or remove listeners of the corresponding type

ActionEvent

JButton, JList, JTextField

ActionListener

AddActionListener (), removeActionListener ()

To illustrate this event, click the button to pop up after adding an action event listener to the button control.

package actionEvent; import java.awt.Color; import java.awt.Container; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.WindowConstants; public class ActionEvent extends JFrame{ JButton jButton; Public ActionEvent() {setTitle(" ActionEvent listener "); SetSize (400400); Container container = getContentPane(); // Add container jButton = new jButton (" click to trigger action event "); jButton.setBounds(100, 150, 200, 30); container.add(jButton); / / button will add to the container jButton. AddActionListener (new jButtonActionEvent ()); // Add a listener event for the button. The listener event is the corresponding inner class setLayout(NULL) for the control. // Empty the form layout manager setLocationRelativeTo(null); // Set the form to center display setVisible(true); SetDefaultCloseOperation (WindowConstants.EXIT_ON_CLOSE); // Center the form setDefaultCloseOperation(WindowConstants. Public static void main(String[] args) {// TODO auto-generated method Stub new ActionEvent(); Class jButtonActionEvent implements ActionListener{@override public void implements ActionListener actionPerformed(java.awt.event.ActionEvent arg0) { jButton.setBackground(Color.RED); / / click later to replace button background with red JOptionPane showMessageDialog (null, "triggered action event listener!" , "tip", JOptionPane. INFORMATION_MESSAGE); }}}Copy the code

The effect is as follows:

Two things to note about action event listeners here:

  1. In the overridden actionPerformed() function, the clicked control should be checked to see if it was clicked.

  2. When listening for action events on a control, use the addActionListener() method to add event listeners to the control. Otherwise, it is useless to click the control without adding a listener, even if you write triggering events to the control.

  3. In general, listening events for an event source should be in the form of an anonymous inner class, as in the example above when adding an event to a button

Think useful remember to like attention yo!

Big bad Wolf is looking forward to progress with you!