用java模拟设计一个简单的“用户注册”程序。当用户输入用户名和密码时,单击“注

用户注册
我们经常会在一些网站进行用户的注册,现在请你模拟设计一个简单的“用户注册”程序。当用户输入用户名和密码时,单击“注册”按钮,弹出提示信息“恭喜你,注册成功!”;当用户输入用户名已存在时,弹出提示信息“该用户已存在,请重新注册!”;如果用户没有输入用户名或密码,弹出提示信息“用户名或密码不能为空”。
求大神帮忙,本人菜鸟刚刚接触java

第1个回答  2012-07-26
所有功能均已实现,如有不满意的地方我再修改

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class Login extends JPanel
{
//声明各个控件
private JLabel user_name_label = null;
private JLabel password_label = null;
private JTextField user_name_text = null;
private JTextField password_text = null;
private JButton login = null;
private JButton regist = null;

//声明文件用以保存注册信息
private final String file_name = "注册.txt";

public Login()
{
//获得各个控件并且为之设置显示文本
user_name_label = new JLabel();
user_name_label.setText("姓名:");
password_label = new JLabel();
password_label.setText("密码:");
user_name_text = new JTextField();
password_text = new JTextField();
login = new JButton();
login.setText("登录");
regist = new JButton();
regist.setText("注册");

//设置面板的布局为网格布局
setLayout(new GridLayout(3,2));

//将控件添加到面板里
add(user_name_label);
add(user_name_text);
add(password_label);
add(password_text);
add(login);
add(regist);

//为两个按钮添加监听
regist.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String name = user_name_text.getText().toString();
String password = password_text.getText().toString();
String str = null;
String[] result = null;
try
{
if((name.length() == 0)&&(password.length() == 0))
{
int a = JOptionPane.showConfirmDialog(null,"请输入用户名和密码","确认对话框",JOptionPane.YES_NO_OPTION);
throw new Exception("");
}
else if(name.length() == 0)
{
int a = JOptionPane.showConfirmDialog(null,"请输入用户名","确认对话框",JOptionPane.YES_NO_OPTION);
}
else if(password.length() == 0)
{
int a = JOptionPane.showConfirmDialog(null,"请输入密码","确认对话框",JOptionPane.YES_NO_OPTION);
}

InputStream in = new FileInputStream(file_name);
InputStreamReader reader = new InputStreamReader(in);
BufferedReader buffered_reader = new BufferedReader(reader);
while((str = buffered_reader.readLine()) != null)
{
result = str.split(" ");
if(result[0].equals(name))
{
int a = JOptionPane.showConfirmDialog(null,"该用户已存在,请重新注册","确认对话框",JOptionPane.YES_NO_OPTION);
throw new Exception("");
}
}

OutputStream out = new FileOutputStream(file_name,true);
OutputStreamWriter writer = new OutputStreamWriter(out);
BufferedWriter buffered_writer = new BufferedWriter(writer);
buffered_writer.write(name+" "+password);
buffered_writer.newLine();
buffered_writer.close();
int a = JOptionPane.showConfirmDialog(null,"恭喜你,注册成功!","确认对话框",JOptionPane.YES_NO_OPTION);
}
catch(Exception e1)
{

}
}
});

login.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String name = user_name_text.getText().toString();
String password = password_text.getText().toString();
String result = null;

try
{
if((name.length() == 0)&&(password.length() == 0))
{
int a = JOptionPane.showConfirmDialog(null,"请输入用户名和密码","确认对话框",JOptionPane.YES_NO_OPTION);
throw new Exception("");
}
else if(name.length() == 0)
{
int a = JOptionPane.showConfirmDialog(null,"请输入用户名","确认对话框",JOptionPane.YES_NO_OPTION);
}
else if(password.length() == 0)
{
int a = JOptionPane.showConfirmDialog(null,"请输入密码","确认对话框",JOptionPane.YES_NO_OPTION);
}

InputStream in = new FileInputStream(file_name);
InputStreamReader reader = new InputStreamReader(in);
BufferedReader buffered_reader = new BufferedReader(reader);

while((result = buffered_reader.readLine()) != null)
{
if(result.equals(name+" "+password))
{
int a = JOptionPane.showConfirmDialog(null,"登陆成功","确认对话框",JOptionPane.YES_NO_OPTION);
break;
}
}
if(!(result.equals(name+" "+password)))
{
int a = JOptionPane.showConfirmDialog(null,"用户名或密码错误","确认对话框",JOptionPane.YES_NO_OPTION);
}
}
catch(Exception e1)
{
//e1.printStackTrace();
}
}
});
}

public static void main(String[] args)
{
JFrame frame = new JFrame();
frame.setSize(500,500);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new Login(),BorderLayout.NORTH);
}
}本回答被提问者和网友采纳
第2个回答  2012-07-26
单机?后面是什么啊追问

我的问题后面又补充了