Event monitoring mechanism in Java

Event monitoring mechanism in Java

1. Definition of event monitoring mechanism

To understand the event monitoring mechanism in Java, we must first understand how events are defined in Java! After writing an interface in Java, we will perform some operations on the interface, such as entering data in an input box, clicking a button or a menu, and these operations are called events .
The event monitoring mechanism , as Gu Ming suggests, is to monitor whether an event has occurred, similar to what happens in a house when we want to monitor it, and we will attach a camera to the house. For handling such events, there is a similar mechanism in Java. For example, if we need to monitor whether a button is clicked, we need to install something like a monitor on the button. We call this thing an event listener . The mechanism for monitoring whether the button is clicked is the event monitoring mechanism.
In Java, every event has a corresponding event listener. They are all defined in the package Java.awt.event :

Second, the composition of the event monitoring mechanism

1) Event object , which usually inherits Java.awt.eventObject , can generally be used to determine the role of event type;
2) Event source , which is the source of the trigger event, in GUI and Swing programming, such as Button buttons and other objects It is an event source;
3) Event listener , that is, the object responsible for listening to the event emitted by the event source and responding to the action, usually implements the java.util.EventListener interface

Three, the general process of event monitoring

1) The event source registers the listener
2) When an event occurs, the event source sends the event object to the listener
3) The listener object responds to the event

Four, add a listener to the component

Since the definition of event listeners are all interfaces, we must define a class to implement the corresponding interface. For example, if we want to monitor whether a button is clicked, we can do so, the code is as follows:

package com.yf1006;
import java.awt.event.ActionEvent;

//通过定义一个ButtonListener类来实现对应的接口,这样一个简单的动作事件监听器实现:监听按钮是否被点击了!
//其中java.awt.event.ActionListener是一种动作监听器,它是属于一种接口的,所以需要定义一个类来实现接口的!!
public class ButtonListener implements java.awt.event.ActionListener{
    
    
	//在这里必须要重写接口中的方法:事件发生时,这个方法被自动调用了。
		public void actionPerformed(ActionEvent e) {
    
    
			System.out.println("按钮被点击了!");
		}
	}

In this way, we can complete the preparation of an event listener class, but we need to create a main class (the so-called main class, that is, the class name and the file name are consistent, and include the main function class ), to add the button in the main class Wait for components, and create a listener object, and then add the listener to the button, so as to fully implement a button's monitoring event mechanism. The example code is as follows:

package com.yf1006;

//import java.awt.Dimension;

//import javax.swing.JLabel;

//import java.awt.FlowLayout;

//import javax.swing.JFrame;

public class Login_test {
    
    
	public static void main(String[] args) {
    
    
		Login_test log = new Login_test();
		log.showUI();
	}
	public void showUI() {
    
    
		//创建窗口
		javax.swing.JFrame jf = new javax.swing.JFrame();
		jf.setSize(600, 600);
		jf.setTitle("窗口");
		
		//居中显示
		jf.setLocationRelativeTo(null);
		//关闭进程
		jf.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
		
		//流式布局管理器
		java.awt.FlowLayout fl = new java.awt.FlowLayout();
		jf.setLayout(fl);
		
		//导入图片
		javax.swing.ImageIcon img = new javax.swing.ImageIcon("F:\\桌面\\1.jpg");//为啥导入照片2则没有啥用呢
		//标签(导入的图片是需要先赋给标签,通过标签的形式去给窗体上面加图片)
		javax.swing.JLabel jl = new javax.swing.JLabel(img);
		jf.add(jl);
		
		//用户名
		javax.swing.JLabel user = new javax.swing.JLabel("账户");
		jf.add(user);
		
		//文本框
		javax.swing.JTextField jtf = new javax.swing.JTextField();
		java.awt.Dimension dim = new java.awt.Dimension(400, 40);
		jtf.setPreferredSize(dim);
		jf.add(jtf);
		
		//用户名1
		javax.swing.JLabel user1 = new javax.swing.JLabel("密码");
		jf.add(user1);
		
		//文本框1
		javax.swing.JTextField jtf1 = new javax.swing.JTextField();
		java.awt.Dimension dim1 = new java.awt.Dimension(400, 40);
		jtf1.setPreferredSize(dim1);
		jf.add(jtf1);
		
		//按钮
		javax.swing.JButton jb = new javax.swing.JButton("登录");
		jf.add(jb);
		
		//利用ButtonListener类,创建监听器对象
		ButtonListener bl = new ButtonListener();
		
		//将监听器添加给按钮
		jb.addActionListener(bl);
		
		//可视化
		jf.setVisible(true);
	}
}

The output result:

Guess you like

Origin blog.csdn.net/qq_39350172/article/details/109169568