//用戶注冊(cè)/登陸镊折,將用戶注冊(cè)信息寫入文件鹦筹,注冊(cè)時(shí)匹配填寫信息
//當(dāng)?shù)卿洉r(shí)夜畴,若用戶已經(jīng)存在時(shí)拖刃,顯示登陸成功;否則登錄失敗
//****運(yùn)用到的技能:對(duì)象序列化與反序列化贪绘,輸入與輸出流兑牡,事件與窗體監(jiān)聽,用戶類和線性表的使用
//****注意事項(xiàng):見程序末尾兔簇。
//主要方法:產(chǎn)生界面方法发绢,寫入文件方法硬耍,從文件中讀取,信息匹配等
package day1;
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();
}
//當(dāng)事件發(fā)生時(shí)边酒,自動(dòng)調(diào)用actionPerformed()方法
public void actionPerformed(ActionEvent e) {
String str = e.getActionCommand();
if ("注冊(cè)".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("密碼輸入錯(cuò)誤");
jtf3.setText("密碼輸入錯(cuò)誤");
} else {
jf.setTitle("注冊(cè)成功" + "歡迎您" + 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("用戶注冊(cè)");
//當(dāng)用戶關(guān)閉窗口時(shí),多個(gè)用戶信息自動(dòng)保存到本地文件系統(tǒng)(通過調(diào)用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("注冊(cè)");
jb51.addActionListener(this);
JButton jb52 = new JButton("登錄");
jb52.addActionListener(this);
jp5.add(jb51);jp5.add(jb52);
jf.setSize(500, 300);
jf.setVisible(true);
}
//readFromFile()方法用于從文件中讀取用戶對(duì)象信息墩朦,并將用戶對(duì)象添加進(jìn)線性表(list)
public void readFromFile() throws IOException, Exception {
FileInputStream fis = new FileInputStream(new File(
"d:\\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");
match(temp);
ois.close();
}
//writeToFile()方法坯认,實(shí)現(xiàn)用戶信息保存功能。
public void writeToFile() throws IOException {
File file = new File("d:\\userRegister.dat");
file.createNewFile();
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(
file));
for (User usr : list) {
oos.writeObject(usr);
}
oos.flush();
oos.close();
}
//當(dāng)用戶試圖登錄時(shí)氓涣,match()方法可以將用戶輸入與保存的信息牛哺,進(jìn)行匹配。
//如果用戶名和密碼正確劳吠,登錄成功引润;否則,登錄失敗痒玩。
public void match(User user) {
boolean match = false;
for (User usr : list) {
if (usr.getUserName().equals(user.getUserName())
&& usr.getPassWord().equals(user.getPassWord())) {
jf.setTitle("登錄成功");
match = true;
break;
}
}
if (!match) {
jf.setTitle("用戶名或密碼錯(cuò)誤淳附,請(qǐng)重新輸入!");
jtf1.setText("");
jtf2.setText("");
jtf3.setText("");
jtf4.setText("");
jtf1.requestFocus();
}
}
public static void main(String[] args) throws Exception {
new userRegister();
}
}
//------------------------------------------------------------//
User類是個(gè)用戶信息類蠢古,包括userName,passWord,email等屬性奴曙。
public class User implements java.io.Serializable {
private String userName;
private String passWord;
private String email;
public User() {
}
public User(String userName, String passWord, String email) {
super();
this.setUserName(userName);
this.setPassWord(passWord);
this.setEmail(email);
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassWord() {
return passWord;
}
public void setPassWord(String passWord) {
this.passWord = passWord;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String toString(){
return userName+":"+passWord+":"+email;
}
}
//徐亮:如果將文件保存為txt等有一定格式的文件,對(duì)象讀取與寫入可能會(huì)發(fā)生錯(cuò)誤草讶。