Experiment 4 Graphical user interface and event handling


1. The purpose of the experiment

  1. Master the basic components of the graphical user interface
  2. Master the main containers and layout of the graphical user interface
  3. Familiar with the event processing model of the graphical interface
  4. Able to perform simple interface design and incident response

2. Experimental content

1. Create a window, when the mouse clicks in the window, the click position coordinates are displayed in the window title bar. As shown:

Insert picture description here

2. Write a program to implement the following interface (when the user name and password are inputted as 000 and 111 respectively, "Welcome, 000" is displayed at the bottom right):

Insert picture description here

提示:
1) The user's operation of the button will trigger the ActionEvent event, which needs to be handled by the class that implements the ActionListener interface; the user's operation of the frame's close button will trigger the WindowEvent event, which needs to be carried out by the class that implements the WindowListener interface deal with. (You can also use adapters or anonymous inner classes to handle events)


2) Layout with GridLayout


3) Return (return) characters in the password box. If the interface uses an awt component, use the setEchoChar method of TextField to set; if it uses a swing component, use JPasswordField (a lightweight text box) and setEchoChar directly Method settings instead of using JTextField. See JDK API


4) The window should be displayed in the appropriate size normally, pay attention to the use of setVisible, pack, setSize and other methods, and refer to the JDK API.


5) Completed in steps, first design the interface, define the window and internal elements, create and display the interface, and then fill in the correct event handling.


problem:

Considering that the user accidentally enters a space before or after the input, such as getting the string "111" from the text box, and still think that the input is correct, what should I do in the program?

3. Experimental analysis, code and operating results

1 )

import java.awt.event.*;
import javax.swing.JFrame;
public class Work{
    
    
	public Work() {
    
    
		JFrame jf = new JFrame("jf");
		// 添加鼠标监听
		jf.addMouseListener(new MouseAdapter() {
    
    
			public void mouseClicked(MouseEvent e) {
    
    
				System.out.println(e.getX()+","+e.getY());
				jf.setTitle("("+ e.getX()+","+e.getY()+")");
			}
		});
		
		// 设置窗口默认关闭事件
		jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		jf.setSize(500,200);
		jf.setVisible(true);
		
	}
	
	public static void main(String[] args) {
    
    
		Work w1 = new Work();
	}
}

operation result:

Insert picture description here

analysis:

Add the mouse click event to the listener, and implement the mouseClicked method, which is triggered when the window is clicked. The setTitle method of the JFrame object is used to set the title content,
and the getX and getY methods of the event object e are used to obtain the click coordinates.

2 )

import java.awt.Button;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
/**
 * 窗口界面
 * @author sweetheart
 *
 */

public class Work01 {
    
    

	public static void main(String[] args) {
    
    
		
		// 创建顶层容器对象
		JFrame jf = new JFrame("");
		
		// 获取jf对象的面板
		Container con = jf.getContentPane();
		
		// 设置网格分布
		con.setLayout(new GridLayout(0,2));
		
		// 创建组件对象添加到顶层容器中
		JLabel L1 = new JLabel("请输入用户名  ");
		JLabel L2 = new JLabel("请输入密码  ");
		JLabel L3 = new JLabel("");
		JTextField jtf1 = new JTextField();
		JPasswordField jtf2 = new JPasswordField();
		Button bt = new Button("确定");
		
		// 监听鼠标点击按钮bt的事件(适配器、匿名类)
		bt.addMouseListener(new MouseAdapter() {
    
    
			public void mouseClicked(MouseEvent e) {
    
    
				// 实现mouseClicked方法
				// 获取用户名输入框输入的内容并将前后的空格去除
				String user = jtf1.getText().strip();
				// 获取输入的密码
				String pass = jtf2.getText();
				// 检验密码输入是否正确
				if(user.equals("000")&&pass.equals("111")) {
    
    
					L3.setText("欢迎,000");
				}else {
    
    
					L3.setText("用户名或密码错误");
				}
			}
		});
		
		jf.add(L1);
		jf.add(jtf1);
		jf.add(L2);
		jf.add(jtf2);
		jf.add(bt);
		jf.add(L3);
		
		// 添加窗口关闭事件监听器
		jf.addWindowListener(new WindowAdapter() {
    
    
			
			public void windowClosing(WindowEvent e) {
    
    
				System.exit(0);
			}
			
		});
		
//		jf.setSize(300,150);
		jf.setBounds(100,100,400,150);
		jf.setVisible(true);
		
	}
}

operation result:

Insert picture description here

analysis:

Basic ideas:
1) Create the top-level container object
2) Get the panel of the jf object
3) Set the grid distribution
4) Create the component object and add it to the top-level container
5) Listen to the event of mouse click button bt (adapter, anonymous class)
6) Implementation mouseClicked method
7) Add a window close event listener
8) Set the window to be visible

to sum up:

AWT components:

Insert picture description here

Swing component class hierarchy:

Insert picture description here

Commonly used containers:

1) Top container-JFrame
2) General container-JPanel

Layout manager:

FlowLayout------Flow Layout Manager
BorderLayout-----Border Layout Manager
GridLayout-----Grid Layout Manager
CardLayout----Card Layout Manager
GridBagLayout----Grid Pack Layout manager
BoxLayout-----box layout manager

Common interfaces (and common methods) for event monitoring:

ActionListener
---- public void actionPerformed(ActionEvent e)
WindowListener
---- public void windowClosing(WindowEvent e)
MouseListener
---- public void mouseClicked(MouseEvent e)

Cooperate with adapter usage (anonymous class)

// Add a window close event listener
jf.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } });



Guess you like

Origin blog.csdn.net/qq_46456049/article/details/109240847