/*题目:创建一个登录界面,通过文件来保存用户名和密码。要求:
 1、如果文件中不存在该用户,则将用户信息保存到文件中,以便下次登录时验证。
 2、如果用户存在,密码三次输入错误,程序退出。
 
 
 以下为我做的源码,但有错误,编译无误,但有运行时错误,请帮忙指证错误,并说明原因。
 因刚学IO流,事件处理我是用顺序结构方式编写的,请哪位高手帮忙给点源码,将事件处理
 以模块的形式编写,最好请高手帮我把我的源码修改一下,以模块的形式来完成,谢谢,
 非常感谢!
 */
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;public class LoginTest extends JFrame implements ActionListener
{
//界面成员
JLabel jlbUserName, jlbUserPwd;
JTextField jtfUserName;
JPasswordField jpfUserPwd;
JButton jbtnLogin, jbtnCencel;
JPanel p1, p2, p3, p4;

//流操作成员
UserInfo ui, uitemp;
File f;
FileInputStream fis;
FileOutputStream fos;
ObjectInputStream ois;
ObjectOutputStream oos;

//辅助成员
ArrayList al;
Iterator it;


public LoginTest()
{
//界面组织
jlbUserName = new JLabel("用户名");
jlbUserPwd = new JLabel("密  码");
jtfUserName = new JTextField(10);
jpfUserPwd = new JPasswordField(10);
jbtnLogin = new JButton("登录");
jbtnCencel = new JButton("退出");
p1 = new JPanel();
p2 = new JPanel();
p3 = new JPanel();
p4 = new JPanel();

p1.add(jlbUserName);
p1.add(jtfUserName);

p2.add(jlbUserPwd);
p2.add(jpfUserPwd);

p3.add(jbtnLogin);
p3.add(jbtnCencel);

p4.setLayout(new GridLayout(3, 1));
p4.add(p1);
p4.add(p2);
p4.add(p3);

this.setContentPane(p4);
this.setSize(300, 150);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jbtnLogin.addActionListener(this);
jbtnCencel.addActionListener(this);

try
{
f = new File("System.ini");
}
catch(Exception ie)
{
ie.printStackTrace();
}

al = new ArrayList();
}

public void actionPerformed(ActionEvent e)
{
ui = new UserInfo();

if (e.getSource().equals(jbtnLogin))
{
//1.点击登录以后,首先判断文本框内容是否为空,再将文本框的内容写入到对象
if (jtfUserName.getText().equals("") || jpfUserPwd.getText().equals(""))
{
System.out.println ("用户名或密码不能为空!");
return ;
}
ui.setUserName(jtfUserName.getText());
ui.setUserPwd(jpfUserPwd.getText());

//2.判断保存信息的文件是否存在,如不存在,就创建它
try
{
if(!f.exists())
{
f.createNewFile();
}
}
catch (Exception et)
{
et.printStackTrace();
}

//3.将文件的内容读入到一个集合中
try
{
fis = new FileInputStream(f);
ois = new ObjectInputStream(fis);

//此处意图是通过对象流读取文件中的对象,
//每读取一个对象就在集合中增加一个对象,但不知语法是否正确
while ((uitemp = (UserInfo)ois.readObject()) != null)
{
al.add(uitemp);
}
}
catch(Exception ie)
{
ie.printStackTrace();
}

//4.将ui与集合中的对象进行比较
it = al.iterator(); //迭代器绑定
while(it.hasNext())
{
uitemp = (UserInfo)it.next();//获取集合中当前对象

if (ui.equals(uitemp))//首先验证用户名
{
System.out.println ("用户名正确,正在验证密码……");
if (ui.isLeapPwd(uitemp))
{
System.out.println ("密码正确,登录成功……");
//如果登录成功,因只开启了二个流,所以只用关闭二个流
try
{
fis.close();
ois.close();
}
catch (Exception ie)
{
ie.printStackTrace();
}
System.exit(0);//如果都正确,程序结束
}
else
{
System.out.println ("密码不正确,请重新输入……");
ui.setCount();
if (ui.getCount() > 3)
{
System.out.println ("连续三次错误输入密码,程序自动退出……");
try
{
fis.close();
ois.close();
}
catch(Exception ie)
{
ie.printStackTrace();
}
System.exit(0);//密码输入三次错误,程序也会自动退出
}
return;
}
}
else
{
//5.如果用户在集合中不存在,则追加到System.ini文件中
System.out.println ("数据库中没有您的信息,您的初始信息会自动添加到数据库……");
try
{
fos = new FileOutputStream(f, true);
oos = new ObjectOutputStream(fos);
oos.writeObject(ui);//写对象通过对象流直接写入到文件中
System.out.println ("初始信息写入文件成功……");
fis.close();
ois.close();
fos.close();
oos.close();
}
catch(Exception ce)
{
ce.printStackTrace();
}

System.exit(0);//将对象写入文件后,程序自动退出
}
}
}
else if (e.getSource().equals(jbtnCencel))
{
System.exit(0);
}
}

public static void main(String[] args)
{
new LoginTest();
}
}/************************************************************/class UserInfo implements java.io.Serializable
{
private String userName;
private String userPwd;
private int count;

public UserInfo()
{
userName = "";
userPwd = "";
count = 0;
}

public UserInfo(String userName, String userPwd)
{
this.userName = userName;
this.userPwd = userPwd;
this.count = 0;
}

public void setUserName(String value)
{
this.userName = value;
}

public String getUserName()
{
return this.userName;
}

public void setUserPwd(String value)
{
this.userPwd = value;
}

public String getUserPwd()
{
return this.userPwd;
}

public String toString()
{
return  "用户名:" + userName + "\t" + "密码:" + userPwd;
}

public void setCount()
{
count++;
}

public int getCount()
{
return count;
}

public boolean equals(UserInfo ui)
{
if (this.getUserName() == ui.getUserName())
return true;
else
return false;
}

public boolean isLeapPwd(UserInfo ui)
{
if (this.getUserPwd() == ui.getUserPwd())
return true;
else
return false;
}
}