Deep understanding of interface and event listener in Java

1: Interface

Interfaces are ubiquitous in our life. Through a USB interface, we can transmit data through U disk. This interface is defined. Only specified types can use this interface, and the data transmitted through this interface will not be damaged.

In Java, the function of interface is similar. Define an interface with specific characteristics, reference it in the appropriate place and execute the corresponding functions, so as to realize the separation of code.

Let's take a look at the definition of the interface:

Properties of the interface:

Interface method:

Use of interfaces:

An interface cannot create an object. For objects that require an interface type, you need to create a class to implement the interface

In summary, the interface is equivalent to formulating a rule. Only when this rule is met can the corresponding interface be entered.

2: Event monitoring mechanism

Imagine, for example, in life, a camera is watching Xiao Ming and supervising him to study hard. If he is found doing something else, he will issue a warning prompt to let him continue to study, and the principle is the same in Java.

Add an event listener to the event source component. When a 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 the computer

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.

Implementation of event listener:

Let's take a look at an example of a login interface

This section is to establish a login interface

public class UI {
 /**
 *
 */
 public void showIU(){
 //建立新界面
 JFrame frame=new JFrame();
 frame.setSize(600,900);
 //用户点击关闭窗口时关闭界面
 frame.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE);
 //流式布局
 FlowLayout layout=new FlowLayout();
 frame.setLayout(layout);
 //插入账号文本框
 JTextField register=new JTextField(30);
 frame.add(register);
 //标签1
 JLabel label1=new JLabel("注册账号");
 frame.add(label1);
 //密码
 JPasswordField word=new JPasswordField(30);
 frame.add(word);
 //标签2
 JLabel label2= new JLabel("找回密码");
 frame.add(label2);

 JCheck@R_834_2419@ check@R_834_2419@=new JCheck@R_834_2419@("记住密码 ");
 frame.add(check@R_834_2419@);
 //设置复选框大小
 Dimension check =new Dimension(200,35);
 check@R_834_2419@.setPreferredSize(check);

 //加入接口类
 lisener lisen=new lisener();
 //给按钮添加监听器
 butt.addActionListener(lisen);
 //让监听器的文本框和处理的文本框对应
 lisen.nameinput=register;
 lisen.passinput=word;
 //让监听器的框与此相同
 lisen.jiemian=frame;

 word.addActionListener(lisen);

 /*
 //加入接口
 Listener2 lisen1=new Listener2();
 //添加监听器
 word.addKeyListener(lisen1);
 //对应文本框和密码框
 lisen1.name1=register;
 lisen1.pass1=word;
 lisen1.frame=frame;
 */
 //设置界面可见
 frame.setVisible(true);

 }
 public static void main(String[] args){
 UI ui=new UI();
 ui.showIU();
 }
}

We have established two interfaces. Two action listeners monitor whether the button is clicked and whether the key on the keyboard is pressed respectively, and the corresponding results appear from then on.

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("鼠标点击了");
 String name=nameinput.getText();
 String password=passinput.getText();
 if(name.equals("majunlong")&&password.equals("12345678")){
  System.out.println("登陆成功");
  jiemian.dispose();
 }

 else{
  System.out.println("登陆失败");
  JOptionPane pane=new JOptionPane();
  JOptionPane.showMessageDialog(null,"登陆失败","警告",JOptionPane.ERROR_MESSAGE);

 }

 }

}

This is a mouse listener. When the button is clicked, a prompt box will be sent 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("鼠标点击了");
 String name=name1.getText();
 String password=pass1.getText();
 if(name.equals("majunlong")&&password.equals("12345678")){
  System.out.println("登陆成功");
  frame.dispose();
 }

 else{
  System.out.println("登陆失败");
  JOptionPane pane=new JOptionPane();

  JOptionPane.showMessageDialog(null,JOptionPane.ERROR_MESSAGE);

 }

 }
 }

Keyboard listener, press enter after entering the password 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 value for your study or work. Thank you for your support.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>