}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (e.getActionCommand()==“登录”)
{
try {
login();
} catch (HeadlessException | IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
else if(e.getActionCommand()==“注册”)
{
// clear();
new Register();
dispose(); //使窗口消失
}
}
//清空账号和密码框
private void clear() {
// TODO Auto-generated method stub
jtf.setText(“”); //设置为空
jpf.setText(“”);
}
//验证登录信息,并做处理
@SuppressWarnings(“deprecation”)
public void login() throws HeadlessException, IOException
{
//判断账户名和密码是否包含中文
if (new Check().checkcountname(jtf.getText())||new Check().checkcountname(jpf.getText()))
{
JOptionPane.showMessageDialog(null, “用户名或密码存在中文,不合法!”,“消息提示”,JOptionPane.WARNING_MESSAGE);
}
else if(jtf.getText().isEmpty()&&jpf.getText().isEmpty())
{
JOptionPane.showMessageDialog(null, “账号密码为空,请输入!”,“消息提示”,JOptionPane.WARNING_MESSAGE);
}
else if (jtf.getText().isEmpty())
{
JOptionPane.showMessageDialog(null, “账号为空,请输入!”,“消息提示”,JOptionPane.WARNING_MESSAGE);
}
else if (jpf.getText().isEmpty())
{
JOptionPane.showMessageDialog(null, “密码为空,请输入!”,“消息提示”,JOptionPane.WARNING_MESSAGE);
}
else if (new Check().check1(jtf.getText(),jpf.getText()))
{
JOptionPane.showMessageDialog(null,“登录成功!”,“提示消息”,JOptionPane.WARNING_MESSAGE);
dispose(); //使文原窗体消失
}
else
{
JOptionPane.showMessageDialog(null, “账号密码错误请重新输入!”,“消息提示”,JOptionPane.ERROR_MESSAGE);
clear(); //调用清除函数
}
}
}
3.Register类
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import javax.swing.*;
/**
-
此类完成对注册页面的编写, 用户需填写 账户,密码
-
并且会进行验证操作, 如姓名是否合法(中文)
*/
@SuppressWarnings(“serial”)
public class Register extends JFrame implements ActionListener{
JButton jb1, jb2; //按钮
JLabel jlb1, jlb2, jlb3,jlb4,jlb5, jlb6; //标签
JTextField jtf3,jtf4; //文本框
JPasswordField jpf; //密码框
JPanel jp3, jp4,jp6,jp7; //面板
public Register() {
// TODO Auto-generated constructor stub
//按钮
jb1 = new JButton(“提交”);
jb2 = new JButton(“登录”);
//设置按钮监听
jb1.addActionListener(this);
jb2.addActionListener(this);
//标签信息
jlb3 = new JLabel(" 账号");
jlb4 = new JLabel(" 密码");
jtf3 = new JTextField(13);
jtf4 = new JTextField(13);
jp3 = new JPanel();
jp4 = new JPanel();
jp6 = new JPanel();
jp7 = new JPanel();
//将对应信息加入面板中
jp3.add(jlb3);
jp3.add(jtf3);
jp4.add(jlb4);
jp4.add(jtf4);
jp6.add(jb1);
jp6.add(jb2);
//将JPane加入JFrame中
this.add(jp7); //先加入提示语
this.add(jp3);
this.add(jp4);
this.add(jp6);
//设置布局
this.setTitle(“注册信息”);
this.setLayout(new GridLayout(5, 1));
this.setSize(250, 240); //设置窗体大小
this.setLocationRelativeTo(null);//在屏幕中间显示(居中显示)
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE); //设置仅关闭当前窗口
this.setVisible(true); //设置可见
this.setResizable(false); //设置不可拉伸大小
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (e.getActionCommand()==“提交”)
{
try {
register();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
else if (e.getActionCommand()==“登录”)
{
new Login();
}
}
//验证注册信息,并做处理
public void register() throws IOException
{
//判断信息是否补全
if (jtf3.getText().isEmpty()||jtf4.getText().isEmpty())
{
JOptionPane.showMessageDialog(null, “信息有空缺,请补全!”,“消息提示”,JOptionPane.WARNING_MESSAGE);
}
//判断账户名和密码是否包含中文
else if (new Check().checkcountname(jtf3.getText())||new Check().checkcountname(jtf4.getText()))
{
JOptionPane.showMessageDialog(null, “用户名或密码存在中文,不合法!”,“消息提示”,JOptionPane.WARNING_MESSAGE);
}
//满足要求
else if (!jtf3.getText().isEmpty()&&!jtf4.getText().isEmpty())
{
//注册成功, 打包为信息数组传递给UserMessage进行更新操作
String []message = new String[2];
message[0] = jtf3.getText();
message[1] = jtf4.getText();
if (new Check().check2(message[0])) //调用Check的check方法检测用户是否存在, 如果不存在执行
{
new UserMessage().write(message); //调用UserMseeage的write方法进行写操作, 将信息格式化存入
JOptionPane.showMessageDialog(null,“注册成功!”,“提示消息”,JOptionPane.WARNING_MESSAGE);
dispose(); //使窗口消失
new Login();
}
else
{
JOptionPane.showMessageDialog(null,“账号已存在,请重新输入!”,“提示消息”,JOptionPane.WARNING_MESSAGE);
//dispose();
}
}
}
//清空账号和密码框
private void clear() {
// TODO Auto-generated method stub
jtf3.setText(“”);
jtf4.setText(“”);
}
}
4.UserMessage类
import java.io.*;
/**
-
此类事对用户信息的 写入和读取操作
*/
public class UserMessage
{
/*
- 将注册的信息写入文本
*/
public void write(String[] message)throws IOException
{
File file=new File(“Message.txt”);
String messagesum=“”;
for (int i=0; i<2; i++) //将信息格式化存储
messagesum+=message[i]+“~”;
if(!file.exists())
file.createNewFile();
FileOutputStream out=new FileOutputStream(file,true); //建立输出对象,true表示追加
StringBuffer sb=new StringBuffer(); //创建字符串流
sb.append(messagesum+“\n”); //向字符串流中添加信息
out.write(sb.toString().getBytes(“gb2312”)); //将字符串流中的信息写入文本
out.close(); //关闭
}
/*
- 读取信息,将用户名信息返回(如果不存在返回null),和Check类配合使用
*/
public String[] read(String countname) throws IOException
{
File file=new File(“Message.txt”);
if(!file.exists()||file.isDirectory())
throw new FileNotFoundException();
BufferedReader br=new BufferedReader(new FileReader(file));
String temp=null;
StringBuffer sb=new StringBuffer();
temp=br.readLine();
String []message = new String[5]; //按~拆分 成5个字符串数组,按账号和密码进行信息验证
while(temp!=null){
String sbstring = temp.toString();
int n = sbstring.length(); //测字符串长度
for (int i=0; i<5; i++)
message[i] = “”;
int k=0;
for (int i=0; i<n; i++)
{
if(sbstring.charAt(i)==‘~’)
{
//System.out.println(“@”+message[k]);
k++;
}
else
{
message[k] += sbstring.charAt(i);
}
}
if (message[2].equals(countname)) //返回找到用户的信息
{
return message;
}
temp=br.readLine();
}
return null;
}
//
//更新密码
public String updatepwd(String countname,String pwd) throws IOException
{
File file=new File(“Message.txt”);
if(!file.exists()||file.isDirectory())
throw new FileNotFoundException();
//读文件
BufferedReader br=new BufferedReader(new FileReader(file));
String temp=null;
StringBuffer sb=new StringBuffer();
//写文件
//FileOutputStream out=new FileOutputStream(file,false);
StringBuffer sb1=new StringBuffer();
String moneystring=“”;
temp=br.readLine();
String []message = new String[2]; //按~拆分 成5个字符串数组,按账号和密码进行信息验证
while(temp!=null){
String sbstring = temp.toString();
int n = sbstring.length(); //测字符串长度
for (int i=0; i<2; i++)
message[i] = “”;
int k=0;
for (int i=0; i<n; i++) //拆乘5个String
{
if(sbstring.charAt(i)==‘~’)
{
//System.out.println(“@”+message[k]);
k++;
}
else
{
message[k] += sbstring.charAt(i);
}
}
if (message[0].equals(countname)) //找到该账户名
{
//修改密码
for (int i=0; i<3; i++)
sb1.append(message[i]+“~”);
sb1.append(pwd+“~”);
sb1.append(message[4]+“~\n”);
}
else
{
sb1.append(temp+“\n”);
}
temp=br.readLine();
}
/*
-
说明:
-
本来的想法是在原文件对象中覆盖内容,但是发现覆盖后文本为空, 无法解决