所有功能均已实现,如有不满意的地方我再修改
importjavax.swing.*;
importjava.awt.*;
importjava.awt.event.*;
importjava.io.*;
publicclassLoginextendsJPanel
{
//声明各个控件
privateJLabeluser_name_label=null;
privateJLabelpassword_label=null;
privateJTextFielduser_name_text=null;
privateJTextFieldpassword_text=null;
privateJButtonlogin=null;
privateJButtonregist=null;
//声明文件用以保存注册信息
privatefinalStringfile_name="注册.txt";
publicLogin()
{
//获得各个控件并且为之设置显示文本
user_name_label=newJLabel();
user_name_label.setText("姓名:");
password_label=newJLabel();
password_label.setText("密码:");
user_name_text=newJTextField();
password_text=newJTextField();
login=newJButton();
login.setText("登录");
regist=newJButton();
regist.setText("注册");
//设置面板的布局为网格布局
setLayout(newGridLayout(3,2));
//将控件添加到面板里
add(user_name_label);
add(user_name_text);
add(password_label);
add(password_text);
add(login);
add(regist);
//为两个按钮添加监听
regist.addActionListener(newActionListener()
{
publicvoidactionPerformed(ActionEvente)
{
Stringname=user_name_text.getText().toString();
Stringpassword=password_text.getText().toString();
Stringstr=null;
String[]result=null;
try
{
if((name.length()==0)&&(password.length()==0))
{
inta=JOptionPane.showConfirmDialog(null,"请输入用户名和密码","确认对话框",JOptionPane.YES_NO_OPTION);
thrownewException("");
}
elseif(name.length()==0)
{
inta=JOptionPane.showConfirmDialog(null,"请输入用户名","确认对话框",JOptionPane.YES_NO_OPTION);
}
elseif(password.length()==0)
{
inta=JOptionPane.showConfirmDialog(null,"请输入密码","确认对话框",JOptionPane.YES_NO_OPTION);
}
InputStreamin=newFileInputStream(file_name);
InputStreamReaderreader=newInputStreamReader(in);
BufferedReaderbuffered_reader=newBufferedReader(reader);
while((str=buffered_reader.readLine())!=null)
{
result=str.split("");
if(result[0].equals(name))
{
inta=JOptionPane.showConfirmDialog(null,"该用户已存在,请重新注册","确认对话框",JOptionPane.YES_NO_OPTION);
thrownewException("");
}
}
OutputStreamout=newFileOutputStream(file_name,true);
OutputStreamWriterwriter=newOutputStreamWriter(out);
BufferedWriterbuffered_writer=newBufferedWriter(writer);
buffered_writer.write(name+""+password);
buffered_writer.newLine();
buffered_writer.close();
inta=JOptionPane.showConfirmDialog(null,"恭喜你,注册成功!","确认对话框",JOptionPane.YES_NO_OPTION);
}
catch(Exceptione1)
{
}
}
});
login.addActionListener(newActionListener()
{
publicvoidactionPerformed(ActionEvente)
{
Stringname=user_name_text.getText().toString();
Stringpassword=password_text.getText().toString();
Stringresult=null;
try
{
if((name.length()==0)&&(password.length()==0))
{
inta=JOptionPane.showConfirmDialog(null,"请输入用户名和密码","确认对话框",JOptionPane.YES_NO_OPTION);
thrownewException("");
}
elseif(name.length()==0)
{
inta=JOptionPane.showConfirmDialog(null,"请输入用户名","确认对话框",JOptionPane.YES_NO_OPTION);
}
elseif(password.length()==0)
{
inta=JOptionPane.showConfirmDialog(null,"请输入密码","确认对话框",JOptionPane.YES_NO_OPTION);
}
InputStreamin=newFileInputStream(file_name);
InputStreamReaderreader=newInputStreamReader(in);
BufferedReaderbuffered_reader=newBufferedReader(reader);
while((result=buffered_reader.readLine())!=null)
{
if(result.equals(name+""+password))
{
inta=JOptionPane.showConfirmDialog(null,"登陆成功","确认对话框",JOptionPane.YES_NO_OPTION);
break;
}
}
if(!(result.equals(name+""+password)))
{
inta=JOptionPane.showConfirmDialog(null,"用户名或密码错误","确认对话框",JOptionPane.YES_NO_OPTION);
}
}
catch(Exceptione1)
{
//e1.printStackTrace();
}
}
});
}
publicstaticvoidmain(String[]args)
{
JFrameframe=newJFrame();
frame.setSize(500,500);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(newBorderLayout());
frame.add(newLogin(),BorderLayout.NORTH);
}
}