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

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

2020 - 9 - 26 TAG :

所有功能均已实现,如有不满意的地方我再修改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); }}

170 人围观 / 0 条评论 ↓快速评论↓

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值