java event monitoring

Description: According to the video link written in the video of learning station b [Mad God speaks java]

Case 1

package bili_02;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

//ActionListener是一个接口
class MyActionListener implements ActionListener{
    
    
	@Override
	public void actionPerformed(ActionEvent e) {
    
    
		// TODO Auto-generated method stub
		System.out.println("点击了btn");
	}
	 
 }
public class TestAction {
    
    
	public static void main(String[] args) {
    
    
		//按下按钮,触发一些事件
		Frame frame = new Frame("事件监听窗口");
		Button btn = new Button("btn");
		//因为addActionListener()需要一个ActionListener,所以我们需要构造一个ActionListener
		MyActionListener myActionListener = new MyActionListener();
		btn.addActionListener(myActionListener);
		frame.add(btn);
		//自适应大小
		frame.setSize(200, 100);
		frame.setVisible(true);
		windowClosing(frame);
	}
	//关闭窗口的事件
	public static void windowClosing(Frame frame) {
    
    
		frame.addWindowListener(new WindowAdapter() {
    
    
			public void windowClosing(WindowEvent e) {
    
    
				System.exit(0);
			}
		});
	}
}

operation result:

Insert picture description here

Case 2

Two buttons share one monitor

package bili_02;

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

class MyMonitor implements ActionListener{
    
    

	@Override
	public void actionPerformed(ActionEvent e) {
    
    
		// TODO Auto-generated method stub
		System.out.println("点击了"+e.getActionCommand());
		if(e.getActionCommand().equals("start")) {
    
    
			
		}
	}
	
}
public class TestActionListener {
    
    
	public static void main(String[] args) {
    
    
		//两个按钮,实现一个监听
		//开始 结束
		Frame frame = new Frame("开始-结束");
		Button btn1 = new Button("start");
		Button btn2 = new Button("stop");
		frame.add(btn1,BorderLayout.NORTH);
		frame.add(btn2,BorderLayout.SOUTH);
		frame.setSize(200,100);
		//设置显示内容
		btn2.setActionCommand("btn2-stop");
		
		MyMonitor myMonitor = new MyMonitor();
		btn1.addActionListener(myMonitor);
		btn2.addActionListener(myMonitor);
		
		windowClosing(frame);
		frame.setVisible(true);
	}
	public static void windowClosing(Frame frame) {
    
    
		frame.addWindowListener((WindowListener) new WindowAdapter() {
    
    
			public void windowClosing(WindowEvent e) {
    
    
				System.exit(0);
			}
		});
	}
}

The results of the operation are:
Insert picture description here

Case 3

Input text box monitoring, input text box cannot wrap, text field can wrap

package bili_03;

import java.awt.*;
import java.awt.event.*;

class MyFrame extends Frame {
    
    
	public MyFrame() {
    
    
		TextField textField = new TextField();
		add(textField);
		pack();

		// 监听这个文本框输入的文字
		MyActionListener myActionListener = new MyActionListener();
		// 按下enter 会触发这个输入框的事件
		textField.addActionListener(myActionListener);

		// 设置替换编码 输入的字符显示为*
		textField.setEchoChar('*');
		setVisible(true);
	}
}

class MyActionListener implements ActionListener {
    
    
	
	@Override
	public void actionPerformed(ActionEvent e) {
    
    
		// TODO Auto-generated method stub
		// 在监听器中拿到了TextField对象,然后借用TextField对象的方法获得文本
		TextField field = (TextField) e.getSource(); // 获得一些资源,返回一个对象
		System.out.println(field.getText()); // 获得输入框的文本
		field.setText(""); // 按下enter之后清空
	}
}

public class TestText {
    
    
	public static void main(String[] args) {
    
    
		MyFrame myFrame = new MyFrame();
		windowClosing(myFrame); //关闭
	}
	//监听关闭事件
	public static void windowClosing(Frame frame) {
    
    
		frame.addWindowListener((WindowListener) new WindowAdapter() {
    
    
			public void windowClosing(WindowEvent e) {
    
    
				System.exit(0);
			}
		});
	}
}

The results of the operation are as follows:
Case 3 running results

Guess you like

Origin blog.csdn.net/tscn1/article/details/106745399