Deep understanding of interface and event listener in Java

Time:2021-1-23

1: Interface

Interface is everywhere in our life. Through a USB interface, we can transmit data through U disk. This interface is defined. Only the specified type can use this interface, and the data we transmit through this interface will not be damaged.

In Java, the function of interface is similar. Define an interface with specific characteristics, refer to it in the appropriate place, and execute the corresponding functions. In this way, the code can be separated.

Let’s take a look at the definition of interface

Key words: Interface
Format: public interface interface name {}

Properties of the interface:

[public static final] attribute type, attribute name = attribute value;
Static: static. The modified content can be accessed directly through the class name or interface name.
Final: unchangeable. If you modify a property, the property must be initialized.
The content in [] can be omitted

Interface method:

[public abstract] method return value type method name (parameter type parameter name)
Abstract: abstract, modify method, then method cannot have method body.
The content in the [] can be omitted

Interface usage:

An interface cannot create an object. If an object needs an interface type, it needs to create a class to implement the interface

Key words: implements
Format: public class name implements interface name {}
* * Note: when implementing an interface in a class, you must implement all the methods in the interface**

To sum up, the interface is equivalent to making a rule, only in line with this rule can enter the corresponding interface.

2: Event monitoring mechanism

Imagine, for example, in life, there is a camera monitoring Xiao Ming to supervise him to study hard. If he is found doing something else, he will be warned to continue learning. The same is true in Java.

Add an event listener to the event source component. When the corresponding type of event occurs on the event source component, the content in the listener will be called automatically.

There are three common types of events on a computer

Mouse event: use the mouse to operate components (mouse movement, click, release)
Keyboard event: operate the component with the keyboard (press the key of the keyboard)
Action events: including mouse click button, keyboard confirmation, etc.

Event source component: the component where the event occurs, such as on a created drawing board.

Event listener: Java provides a corresponding type of event listener for each type of event.

Mouse event listener: mouselistener
Keyboard event listener: keylistener
Action event listener: actionlistener

The implementation of event listener is as follows

Step 1: determine the event source component
For example, a button
Step 2: determine the event type
Click the button for action events
Step 3:
Create a listener class to implement the listener interface of the corresponding type
The fourth step is to create a listener object in the source program and add an event listener to the event source component.

Let’s take a look at an example of the login interface

This section is to create a login interface

public class UI {
 /**
 * 
 */
 public void showIU(){
 //Create a new interface
 JFrame frame=new JFrame();
 frame.setSize(600, 900);
 //Close the interface when the user clicks close window
 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 //Flow layout
 FlowLayout layout=new FlowLayout();
 frame.setLayout(layout);
 //Insert account text box
 JTextField register=new JTextField(30);
 frame.add(register);
 //Label 1
 Jlabel label1 = new jlabel ("registered account");
 frame.add(label1);
 //Password
 JPasswordField word=new JPasswordField(30);
 frame.add(word);
 //Label 2
 Jlabel label2 = new jlabel ("retrieve password");
 frame.add(label2);
 
 Jcheckbox checker = new jcheckbox ("remember password");
 frame.add(checkbox);
 //Set check box size
 Dimension check =new Dimension(200,35);
 checkbox.setPreferredSize(check);
 
 
 //Join interface class
 lisener lisen=new lisener();
 //Adding a listener to a button
 butt.addActionListener(lisen);
 //Let the text box of the listener correspond to the text box of the processing
 lisen.nameinput=register;
 lisen.passinput=word;
 //Make the listener's box the same as this
 lisen.jiemian=frame;
 
 word.addActionListener(lisen);
 
 /*
 //Join interface
 Listener2 lisen1=new Listener2();
 //Add listener
 word.addKeyListener(lisen1);
 //Corresponding text box and password box
 lisen1.name1=register;
 lisen1.pass1=word;
 lisen1.frame=frame;
 */
 //The setting interface is visible
 frame.setVisible(true);
 
 }
 public static void main(String[] args){
 UI ui=new UI();
 ui.showIU();
 }
}

This is the landing interface we have set up

We set up two interfaces. Two action monitors monitor whether the button is pressed or not, and then the corresponding results will appear.

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.JDialog;
import javax.swing.JTextField;
import javax.swing.JOptionPane;
import javax.swing.JFrame;
public class lisener implements ActionListener{
 JTextField nameinput;
 JTextField passinput;
 JFrame jiemian;
 public void actionPerformed(ActionEvent e){
 System.out.println (mouse click);
 String name=nameinput.getText();
 String password=passinput.getText();
 if(name.equals("majunlong")&&password.equals("12345678")){
  System.out.println ("login successful");
  jiemian.dispose();
 }
  
  
 else{
  System.out.println ("login failed");
  JOptionPane pane=new JOptionPane();
  JOptionPane.showMessageDialog (null, login failed, warning), JOptionPane.ERROR_ MESSAGE);
  
 }
  
 
 }

}

This is a mouse monitor, when the button is clicked, a prompt box will be sent out to indicate whether the login is successful or failed.

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JTextField;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class Listener2 implements KeyListener{
 JTextField name1;
 JFrame frame;
 JTextField pass1;

 public void keyTyped(KeyEvent e) {
 // TODO Auto-generated method stub
 
 
 }

 public void keyPressed(KeyEvent e) {
 // TODO Auto-generated method stub
 
 }

 public void keyReleased(KeyEvent e) {
 // TODO Auto-generated method stub
 System.out.println (mouse click);
 String name=name1.getText();
 String password=pass1.getText();
 if(name.equals("majunlong")&&password.equals("12345678")){
  System.out.println ("login successful");
  frame.dispose();
 }
  
  
 else{
  System.out.println ("login failed");
  JOptionPane pane=new JOptionPane();
  
  JOptionPane.showMessageDialog (null, login failed, warning), JOptionPane.ERROR_ MESSAGE);
  
 }
  
 
 }
 }

Keyboard monitor, after entering the password, press enter to get the corresponding prompt.

summary

The above is the whole content of this article, I hope the content of this article has a certain reference learning value for your study or work, thank you for your support to developer.