3 Star 0 Fork 0

TY / Super Mario

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
register.java 4.98 KB
一键复制 编辑 原始数据 按行查看 历史
TY 提交于 2020-01-06 22:16 . 登录注册部分
import java.awt.*;
import java.awt.event.*;
import java.util.Arrays;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class register extends JFrame {
private JPanel contentPane;
private JTextField textField;
private JPasswordField passwordField1;
private JPasswordField passwordField2;
public static void main(String[] args) {
register frame = new register();
frame.setLocationRelativeTo(frame.getOwner());
frame.getContentPane().setBackground(Color.white);
frame.setVisible(true);
}
public register() {
setTitle("用户注册");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(500, 500, 750, 550);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(15, 15, 15, 15));
setContentPane(contentPane);
contentPane.setLayout(new GridLayout(5, 1, 5, 5));
JPanel panel1 = new JPanel();
contentPane.add(panel1);
JLabel label1 = new JLabel("新用户注册");
label1.setFont(new Font("宋体", 0, 33));
panel1.add(label1);
panel1.setBackground(Color.white);
JPanel panel2 = new JPanel();
panel2.setLayout(new FlowLayout(FlowLayout.CENTER));
contentPane.add(panel2);
JLabel label2 = new JLabel("用 户 名 ");
panel2.add(label2);
label2.setFont(new Font("宋体", 0, 30));
panel2.setBackground(Color.white);
textField = new JTextField();
textField.setFont(new Font("宋体", 0, 27));
panel2.add(textField);
textField.setColumns(23);
JPanel panel3 = new JPanel();
panel3.setLayout(new FlowLayout(FlowLayout.CENTER));
contentPane.add(panel3);
JLabel label3 = new JLabel("密 码 ");
label3.setFont(new Font("宋体", 0, 30));
panel3.add(label3);
panel3.setBackground(Color.white);
passwordField1 = new JPasswordField();
passwordField1.setFont(new Font("宋体", 0, 27));
passwordField1.setColumns(23);
panel3.add(passwordField1);
JPanel panel4 = new JPanel();
panel4.setLayout(new FlowLayout(FlowLayout.CENTER));
contentPane.add(panel4);
JLabel label4 = new JLabel("确认密码 ");
label4.setFont(new Font("宋体", 0, 30));
panel4.add(label4);
panel4.setBackground(Color.white);
passwordField2 = new JPasswordField();
passwordField2.setFont(new Font("宋体", 0, 27));
passwordField2.setColumns(23);
panel4.add(passwordField2);
JPanel panel5 = new JPanel();
contentPane.add(panel5);
JButton button = new JButton("提交");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
registerActionPerformed(e);
}
});
button.setFont(new Font("宋体", 0, 27));
panel5.add(button);
panel5.setBackground(Color.white);
}
protected void registerActionPerformed(ActionEvent e) {
String filePath = "D://123.txt";
File file = new File(filePath);
boolean flag = true;
String s = " ",str = null;
String userName = textField.getText();
String password1=new String(passwordField1.getPassword());
String password2=new String(passwordField2.getPassword());
//char[] password1 = passwordField1.getPassword();// 获得密码框1中的密码
//char[] password2 = passwordField2.getPassword();// 获得密码框2中的密码
try {
if (!file.exists())
file.createNewFile();
FileReader fr = new FileReader(file); // 创建文件输输入流
BufferedReader in = new BufferedReader(fr); // 使用缓冲区数据流封装输入流
FileWriter fw = new FileWriter(file,true); // 创建文件输出流
BufferedWriter bw = new BufferedWriter(fw); // 使用缓冲区数据流封装输出流
if (password1.equals(password2)) {// 如果密码相同则提示注册成功
while ((str = in.readLine()) != null) {
String[] data = str.split(" ");
if(data[0].equals(userName)){
flag = false;
break;
}
}
if(flag==false)
JOptionPane.showMessageDialog(this, "用户名重复", null,JOptionPane.WARNING_MESSAGE);
else if(password1==null||password1.length()==0)
JOptionPane.showMessageDialog(this, "密码不能为空", null,JOptionPane.WARNING_MESSAGE);
else if(userName.contains(s))
JOptionPane.showMessageDialog(this, "用户名不能包含空格", null,JOptionPane.WARNING_MESSAGE);
else if(Arrays.asList(password1).contains(s))
JOptionPane.showMessageDialog(this, "密码不能包含空格", null,JOptionPane.WARNING_MESSAGE);
else {
JOptionPane.showMessageDialog(this, "用户注册成功!", null,JOptionPane.INFORMATION_MESSAGE);
bw.write(userName+" "+password1+" ");// 写入数据到输出流
bw.newLine(); // 写入换行符
bw.flush(); // 刷新缓冲区
this.dispose();
}
} else {// 如果密码不同则提示密码不同
JOptionPane.showMessageDialog(this, "两次密码不同!", null,JOptionPane.WARNING_MESSAGE);
passwordField2.setText("");
}
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
1
https://gitee.com/ty0805/Super-Mario.git
git@gitee.com:ty0805/Super-Mario.git
ty0805
Super-Mario
Super Mario
master

搜索帮助