当前位置:网站首页>Experiment 5 components and event handling
Experiment 5 components and event handling
2022-04-23 03:13:00 【A happy wild pointer D】
One 、 The experiment purpose
Learn to use component classes ; Learn to use layout classes ; Study Java Event processing mode and processing of various events .
Two 、 Experimental content
The implementation of the login window is shown in the figure below , Fill in the registration information in the registration window , Click the register button , Will switch to the login window , Enter the user name and password in the login window , If the user name and password are consistent with those entered during registration, the login success dialog box will pop up , Otherwise, the login failure dialog box will pop up .
reflection : Modify the program , Save the registration information to a file , Login time , If the user name and password entered are consistent with those saved, the login success dialog box will pop up , Otherwise, the login failure dialog box will pop up .

3、 ... and 、 Code implementation
// Login screen
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
public class LoginFrame extends JFrame {
public LoginFrame() {
init();
}
public void init() {
JFrame frame = new JFrame(" Login window ");
frame.setBounds(400, 100, 480, 410);
frame.setLayout(null);
Font font = new Font("Serief", Font.BOLD, 18);
Label lb_username = new Label(" user name :");
lb_username.setBounds(100, 76, 76, 20);
lb_username.setFont(font);
JTextField jtf_username = new JTextField();
jtf_username.setBounds(199, 73, 127, 30);
jtf_username.setFont(font);
frame.add(lb_username);
frame.add(jtf_username);
Label lb_password = new Label(" password :");
lb_password.setBounds(100, 143, 76, 20);
lb_password.setFont(font);
JPasswordField jpf_password = new JPasswordField();
jpf_password.setBounds(199, 140, 127, 30);
jpf_password.setFont(font);
frame.add(lb_password);
frame.add(jpf_password);
JButton btn_login = new JButton("login");
btn_login.setBounds(190, 220, 80, 40);
btn_login.setFont(font);
btn_login.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String username = jtf_username.getText().trim();
String password = String.valueOf(jpf_password.getPassword());
File file = new File("./user.txt");
boolean result = false;
if (file.exists()) {
try {
BufferedReader bfr = new BufferedReader(new FileReader(file));
String curLine = null;
while ((curLine = bfr.readLine()) != null) {
System.out.println(curLine);
String[] infos = curLine.split("---");
if (username.equals(infos[0]) && password.equals(infos[1])) {
result = true;
break;
}
}
bfr.close();
if (result) {
frame.setVisible(false);
JOptionPane.showMessageDialog(null, " Login successful !", " Login successful ", JOptionPane.INFORMATION_MESSAGE);
} else {
JOptionPane.showMessageDialog(null, " Login failed !", " Login failed ", JOptionPane.ERROR_MESSAGE);
}
} catch (Exception ep) {
ep.printStackTrace();
}
} else {
JOptionPane.showMessageDialog(null, " Login failed !", " Login failed ", JOptionPane.ERROR_MESSAGE);
}
}
});
frame.add(btn_login);
frame.setVisible(true);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
// The registration screen
import javafx.scene.control.ComboBox;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileWriter;
public class RegisterFrame {
public RegisterFrame() {
init();
}
public void init() {
JFrame frame = new JFrame(" Registration window ");
frame.setLayout(new FlowLayout(FlowLayout.LEFT, 50, 30));
frame.setBounds(400, 100, 760, 600);
Font font = new Font("Serief", Font.BOLD, 18);
JPanel row1 = new JPanel(new FlowLayout(FlowLayout.LEFT, 20, 5));
Label lb_username = new Label(" user name :");
lb_username.setSize(76, 20);
lb_username.setFont(font);
row1.add(lb_username);
JTextField jtf_username = new JTextField();
jtf_username.setPreferredSize(new Dimension(140, 30));
jtf_username.setFont(font);
row1.add(jtf_username);
Label lb_password = new Label(" password :");
lb_password.setSize(66, 20);
lb_password.setFont(font);
row1.add(lb_password);
JPasswordField jpf_password = new JPasswordField();
jpf_password.setPreferredSize(new Dimension(140, 30));
jpf_password.setFont(font);
row1.add(jpf_password);
JPanel row2 = new JPanel(new FlowLayout(FlowLayout.LEFT, 20, 5));
Label lb_sex = new Label(" Gender :");
lb_sex.setSize(66, 20);
lb_sex.setFont(font);
row2.add(lb_sex);
ButtonGroup rbtgp = new ButtonGroup();
JRadioButton rb1 = new JRadioButton(" male ");
rb1.setFont(font);
rb1.setSelected(true);
JRadioButton rb2 = new JRadioButton(" Woman ");
rb2.setFont(font);
rbtgp.add(rb1);
rbtgp.add(rb2);
row2.add(rb1);
row2.add(rb2);
Label lb_hobby = new Label(" hobby :");
lb_hobby.setSize(66, 20);
lb_hobby.setFont(font);
row2.add(lb_hobby);
JCheckBox cb1 = new JCheckBox(" music ");
cb1.setFont(font);
JCheckBox cb2 = new JCheckBox(" Domestic tourism ");
cb2.setFont(font);
JCheckBox cb3 = new JCheckBox(" read ");
cb3.setFont(font);
row2.add(cb1);
row2.add(cb2);
row2.add(cb3);
JPanel row3 = new JPanel(new FlowLayout(FlowLayout.LEFT, 20, 5));
Label lb_job = new Label(" occupation :");
lb_job.setSize(66, 20);
lb_job.setFont(font);
row3.add(lb_job);
JComboBox<String> cb_job = new JComboBox<>();
cb_job.setFont(font);
cb_job.setEnabled(true);
cb_job.addItem(" Student ");
cb_job.addItem(" Enterprise employees ");
cb_job.addItem(" Teachers' ");
row3.add(cb_job);
Label lb_note = new Label("Note:");
lb_note.setSize(66, 20);
lb_note.setFont(font);
row3.add(lb_note);
JTextArea ta_note = new JTextArea(10,10);
ta_note.setFont(font);
row3.add(ta_note);
JButton btn_register = new JButton("register");
btn_register.setFont(font);
btn_register.setPreferredSize(new Dimension(130, 35));
btn_register.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String username = jtf_username.getText().trim();
String password = String.valueOf(jpf_password.getPassword()).trim();
if (username.equals("") || password.equals("")) {
JOptionPane.showMessageDialog(null, " User name or password cannot be empty !", " Registration failed ", JOptionPane.WARNING_MESSAGE);
return;
}
try {
File file = new File("./user.txt");
// create a file
file.createNewFile();
// Write user information to file
FileWriter fw = new FileWriter(file, true);
String content = username + "---" + password + "\n";
fw.write(content);
fw.close();
// Jump to the login screen
frame.setVisible(false);
new LoginFrame();
} catch (Exception ep) {
ep.printStackTrace();
}
}
});
row3.add(btn_register);
frame.add(row1);
frame.add(row2);
frame.add(row3);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
// The main program
public class Test5 {
public static void main(String[] args) {
new RegisterFrame();
}
}
Four 、 Running results

版权声明
本文为[A happy wild pointer D]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220627537775.html
边栏推荐
- TP5 multi conditional where query (using PHP variables)
- LNMP MySQL allows remote access
- A set of combination boxing to create an idea eye protection scheme
- 建立与遍历二叉树
- Recommend reading | share the trader's book list and ask famous experts for trading advice. The trading is wonderful
- Laravel's own paging query
- Flink实时数仓项目—DWS层设计与实现
- Blazor University (12) - component lifecycle
- How does Microsoft solve the problem of multiple programs on PC side -- internal implementation
- 【新版发布】ComponentOne 新增 .NET 6 和 Blazor 平台控件支持
猜你喜欢

手机连接电脑后,QT的QDIR怎么读取手机文件路径

2022t elevator repair test simulation 100 questions and online simulation test

Blazor University (12) - component lifecycle

微软是如何解决 PC 端程序多开问题的

Blazor University (11)组件 — 替换子组件的属性

C# 读写二进制文件

Impact of AOT and single file release on program performance

OLED多级菜单记录

腾讯视频VIP会员,周卡特价9元!腾讯官方直充,会员立即生效!

【VS Code】解决jupyter文件在vs code中显示异常的问题
随机推荐
Configuration table and page information automatically generate curd operation page
Tencent video price rise: earn more than 7.4 billion a year! Pay attention to me to receive Tencent VIP members, and the weekly card is as low as 7 yuan
MySQL port is occupied when building xampp
OLED多级菜单记录
Two methods are used to solve the "maximum palindrome product" problem
Mysql database
在.NE6 WebApi中使用分布式缓存Redis
A set of combination boxing to create an idea eye protection scheme
《C语言程序设计》(谭浩强第五版) 第9章 用户自己建立数据类型 习题解析与答案
The most detailed in the whole network, software testing measurement, how to optimize software testing cost and improve efficiency --- hot
Vs code setting line feed
ASP.NET和ASP.NETCore多环境配置对比
編碼電機PID調試(速度環|比特置環|跟隨)
If the deep replication of objects is realized through C #?
C WPF UI framework mahapps switching theme
Middle and rear binary tree
. net tip: talk about the problem that the scoped service cannot be obtained in the middleware structure
数据挖掘系列(3)_Excel的数据挖掘插件_估计分析
一文了解全面静态代码分析
Use DFS to solve the problem of "number of dictionary rows"