Summary of knowledge points of Java event processing model

In this article, Xiao Zhui shared with you a summary of the knowledge points about the java event processing model, and friends who are interested can learn from it.

When we start a new project, it is inevitable that some errors will occur if it is not suitable for an unfamiliar environment. At this time, we need experienced people to help. The event processing model in java is similar to this principle, divided into three types of objects. In a specific environment, the event source is supervised by the listener. Below we will study the basic principles, three types of objects, and examples of the java event processing model.

1. Basic principles

Each event source can emit several different types of events. Specify one or more listeners for each event source in the program, which can monitor certain events. If a certain event occurs, call the method in the corresponding listener.

2. Three types of objects

(1) Event Source: The place where the event occurs, usually components, such as buttons and windows;

(2) Event: Event encapsulates specific things that happen on interface components.

(3) Event Listener (event listener): responsible for monitoring events that occur from the event source, and responding to various events accordingly.

3. Examples

package Swing;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Event_Exam extends JFrame implements ActionListener {
    
    
static Event_Exam mainJFrame=new Event_Exam();
static JLabel labl1,labl2;
static JLabel showlb1=new JLabel("0");
static JLabel showlb2=new JLabel("0.0");
static JTextField text1,text2;
public static void main(String[] args) {
    
    
// TODO Auto-generated method stub
mainJFrame.setTitle("操作事件示例!");
mainJFrame.setSize(200, 160);
Container container=mainJFrame.getContentPane();
container.setLayout(new FlowLayout());
labl1=new JLabel("输入整数型:");
container.add(labl1);
text1=new JTextField("0",10);
text1.addActionListener(mainJFrame);//把监听者mainJFrame向事件源text1注册
container.add(text1);
labl2=new JLabel("输入浮点数:");
container.add(labl2);
text2=new JTextField("0.0",10);
text2.addActionListener(mainJFrame);//把监听者mainJFrame向事件源text2注册
container.add(text2);
showlb1.setForeground(Color.blue);
showlb1.setHorizontalTextPosition(SwingConstants.LEFT);
showlb2.setForeground(Color.green);
showlb2.setHorizontalTextPosition(SwingConstants.LEFT);
container.add(showlb1);
container.add(showlb2);
mainJFrame.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
    
    //事件发生时的处理操作
//提取文本框内容并显示在showlb1、showlb2中
showlb1.setText("整数为"+text1.getText());
showlb2.setText("浮点数为"+text2.getText());
}
}

This is the end of this article about Java programming timeout tool class examples. Thank you for watching. Remember to like and collect.

Guess you like

Origin blog.csdn.net/p1830095583/article/details/114932488