[今日进展]
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import javax.swing.*;
public class userRegister implements ActionListener {
ArrayList<User> list = new ArrayList<User>();
JTextField jtf1 = new JTextField(14);
JTextField jtf2 = new JTextField(14);
JTextField jtf3 = new JTextField(14);
JTextField jtf4 = new JTextField(14);
JFrame jf;
public userRegister() throws Exception {
createGUI();
}
//当事件发生时,自动调用actionPerformed()方法
@Override
public void actionPerformed(ActionEvent e) {
String str = e.getActionCommand();
if ("注册".equals(str)) {
User user = new User();
user.setUserName(jtf1.getText());
user.setPassWord(jtf2.getText());
user.setEmail(jtf4.getText());
String rePassWord = jtf3.getText();
if (!(user.getPassWord().equalsIgnoreCase(rePassWord))) {
jtf2.setText("密码输入错误");
jtf3.setText("密码输入错误");
} else {
jf.setTitle("注册成功" + "欢迎您" + user.getUserName());
jtf2.setText("******");
jtf3.setText("******");
list.add(user);
}
} else if ("登录".equals(str)) {
try {
readFromFile();
} catch (Exception e1) {
e1.printStackTrace();}}}
//createGui()方法,用于生成图形用户界面
public void createGUI() {
jf = new JFrame("用户注册");
//当用户关闭窗口时,多个用户信息自动保存到本地文件系统(通过调用writeToFile()方法)
jf.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
try {
writeToFile();
} catch (IOException e1) {
e1.printStackTrace();
}
System.exit(0);
}
});
jf.setLayout(new GridLayout(4, 2));
JPanel jp1 = new JPanel();
jf.add(jp1);
JLabel jl1 = new JLabel("User Name:");
jp1.add(jl1);jp1.add(jtf1);
JPanel jp2 = new JPanel();
jf.add(jp2);
JLabel jl2 = new JLabel("Pass Word:");
jp2.add(jl2);jp2.add(jtf2);
JPanel jp3 = new JPanel();
jf.add(jp3);
JLabel jl3 = new JLabel("ReInputPwd:");
jp3.add(jl3);
jp3.add(jtf3);
JPanel jp4 = new JPanel();
jf.add(jp4);
JLabel jl4 = new JLabel("E-mail:");
jp4.add(jl4);jp4.add(jtf4);
JPanel jp5 = new JPanel();
jf.add(jp5);
JButton jb51 = new JButton("注册");
jb51.addActionListener(this);
JButton jb52 = new JButton("登录");
jb52.addActionListener(this);
jp5.add(jb51);jp5.add(jb52);
jf.setSize(500, 300);
jf.setVisible(true);
}
//readFromFile()方法用于从文件中读取用户对象信息,并将用户对象添加进线性表(list)
public void readFromFile() throws IOException, Exception {
FileInputStream fis = new FileInputStream(new File(
"C:\\Users\\Jason tong\\IdeaProjects\\GoldPoint\\src\\userRegister.dat"));
ObjectInputStream ois = new ObjectInputStream(fis);
User read = null;
try {
while (true) {
read = (User) ois.readObject();
if (read == null) {
break;
}
list.add(read);
}
} catch (Exception e) {
}
// System.out.println(list);
User temp = new User(jtf1.getText(), jtf2.getText(), "q");
ois.close();
}
//writeToFile()方法,实现用户信息保存功能。
public void writeToFile() throws IOException {
File file = new File("C:\\Users\\Jason tong\\IdeaProjects\\GoldPoint\\src\\userRegister.dat");
file.createNewFile();
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(
file));
for (User usr : list) {
oos.writeObject(usr);
}
oos.flush();
oos.close();
}
public static void main(String[] args) throws Exception {
new userRegister();
}
}
[问题困难]
- 若对统一用户进行重复注册,若不注意可能会导致统一用户多口令均能成功登陆。
[明日任务]
- 王梓鸿:优化图形登陆界面
- 童皓桢:协助编写登陆模块
- 林郅聪:整合测试现有模块
- 丁文涛:测试数据处理和比较
- 张雪莹:编写登录模块代码
[今日贡献量]
王梓鸿 |
童皓桢 |
林郅聪 |
丁文涛 |
张雪莹 |
2 |
3 |
2 |
2 |
3 |
[燃尽图]