当前位置:网站首页>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
边栏推荐
- 2022G2电站锅炉司炉考试题库及在线模拟考试
- Judge whether there is a leap year in the given year
- TP5 multi conditional where query (using PHP variables)
- Flink实时数仓项目—DWS层设计与实现
- Middle and rear binary tree
- 《C语言程序设计》(谭浩强第五版) 第7章 用函数实现模块化程序设计 习题解析与答案
- PID debugging of coding motor (speed loop | position loop | follow)
- Creating wechat voucher process with PHP
- 手机连接电脑后,QT的QDIR怎么读取手机文件路径
- Laravel's own paging query
猜你喜欢
[mock data] fastmock dynamically returns the mock content according to the incoming parameters
Xamarin effect Chapter 21 expandable floating operation button in GIS
[untitled]
Recursion - outputs continuously increasing numbers
This new feature of C 11, I would like to call it the strongest!
Yes Redis using distributed cache in NE6 webapi
Source generator actual combat
Recommend reading | share the trader's book list and ask famous experts for trading advice. The trading is wonderful
The backtracking of stack is used to solve the problem of "the longest absolute path of file"
be based on. NETCORE development blog project starblog - (1) why do you need to write your own blog?
随机推荐
Laravel new route file
2022年做跨境电商五大技巧小分享
ASP. Net 6 middleware series - Custom middleware classes
【无标题】
C WPF UI framework mahapps switching theme
How does Microsoft solve the problem of multiple programs on PC side -- internal implementation
Data mining series (3)_ Data mining plug-in for Excel_ Estimation analysis
2022a special equipment related management (elevator) work license question bank and simulation examination
“如何实现集中管理、灵活高效的CI/CD”在线研讨会精彩内容分享
LNMP MySQL allows remote access
[Mysql] LEFT函數 | RIGHT函數
【鉴权/授权】自定义一个身份认证Handler
可以接收多种数据类型参数——可变参数
Laravel's own paging query
Aspnetcore configuration multi environment log4net configuration file
The most understandable life cycle of dependency injection
MAUI初体验:爽
Laravel8- use JWT
7-11 rearrange the linked list (25 points)
再战leetcode (290.单词规律)