java登录注册详细代码
新手
import java.awt.*; import javax.swing.*; import java.awt.event.*; public class Login { private JFrame jf; private Container con; private JPanel userJPanel, passJPanel, regJPanel; private JLabel userJLabel, passJLabel; private JTextField userJtf; private JPasswordField passJtf; private JButton login; public void init() { jf = new JFrame("登录"); con = jf.getContentPane(); con.setLayout(new GridLayout(5, 1)); userJLabel = new JLabel("用户名:"); userJtf = new JTextField(10); userJPanel = new JPanel(); userJPanel.add(userJLabel); userJPanel.add(userJtf); passJLabel = new JLabel("密 码:"); passJtf = new JPasswordField(10); passJPanel = new JPanel(); passJPanel.add(passJLabel); passJPanel.add(passJtf); login = new JButton("登陆"); login.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { String userName = userJtf.getText(); String pwd = new String(passJtf.getPassword()); User user = new User(); user.setUserName(userName); user.setPwd(pwd); LoginService service = new LoginService(); boolean b = service.login(user); if(b) { JOptionPane.showMessageDialog(jf, "登录成功"); } else { JOptionPane.showMessageDialog(jf, "登录失败"); } } }); regJPanel = new JPanel(); regJPanel.add(login); con.add(userJPanel); con.add(passJPanel); con.add(regJPanel); jf.setSize(300, 300); jf.setVisible(true); } public static void main(String[] args) { Login login = new Login(); login.init(); } } import java.awt.*; import javax.swing.*; import java.awt.event.*; public class Register { private JFrame jf; private Container con; private JPanel userJPanel, passJPanel, spassJPanel, regJPanel, msgJPanel; private JLabel userJLabel, passJLabel, spassJLabel; private JLabel msg; private JTextField userJtf; private JPasswordField passJtf, spassJtf; private JButton reset, register; public void init() { jf = new JFrame("注册"); con = jf.getContentPane(); con.setLayout(new GridLayout(5, 1)); userJLabel = new JLabel("用户名:"); userJtf = new JTextField(10); userJPanel = new JPanel(); userJPanel.add(userJLabel); userJPanel.add(userJtf); passJLabel = new JLabel("密 码:"); passJtf = new JPasswordField(10); passJPanel = new JPanel(); passJPanel.add(passJLabel); passJPanel.add(passJtf); spassJLabel = new JLabel("确认密码:"); spassJtf = new JPasswordField(10); spassJPanel = new JPanel(); spassJPanel.add(spassJLabel); spassJPanel.add(spassJtf); msg = new JLabel(); msgJPanel = new JPanel(); msgJPanel.add(msg); reset = new JButton("清空"); reset.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ userJtf.setText(""); passJtf.setText(""); spassJtf.setText(""); } }); register = new JButton("注册"); register.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ String pass = new String(passJtf.getPassword()); String spass= new String(spassJtf.getPassword()); if(!pass.equals(spass)) { msg.setText("两次密码输入不一致"); return ; } String userName = userJtf.getText(); User user = new User(); user.setUserName(userName); user.setPwd(pass); RegisterService service = new RegisterService(); service.register(user); msg.setText("注册成功"); } }); regJPanel = new JPanel(); regJPanel.add(reset); regJPanel.add(register); con.add(userJPanel); con.add(passJPanel); con.add(spassJPanel); con.add(msgJPanel); con.add(regJPanel); jf.setSize(300, 300); jf.setVisible(true); } public static void main(String[] args) { Register ref = new Register(); ref.init(); } } import *; public class RegisterService{ private static final String FILE_PATH = "c:\\user.txt"; public void register(User user) { //获取到用户名和密码 //存储到文件当中 String msg = user.getUserName() + "," + user.getPwd(); BufferedWriter bw = null; try { bw = new BufferedWriter(new FileWriter(FILE_PATH)); bw.write(msg); bw.flush(); } catch (IOException ex) { ex.printStackTrace(); } finally { try{ if(bw != null) bw.close(); }catch(IOException e) { e.printStackTrace(); } } } } public class User { private String userName; private String pwd; public void setUserName(String userName) { this.userName = userName; } public String getUserName() { return this.userName; } public void setPwd(String pwd) { this.pwd = pwd; } public String getPwd() { return this.pwd; } } import *; public class LoginService { private static final String FILE_PATH = "c:\\user.txt"; public boolean login(User user) { //获取文件当中存储的用户名和密码 //与参数(前台)传递来的用户名和密码进行匹配 try { BufferedReader br = new BufferedReader(new FileReader(FILE_PATH)); String msg = br.readLine(); br.close(); if(msg == null) { return false; } String[] temp = msg.split(","); return temp[0].equals(user.getUserName()) && temp[1].equals(user.getPwd()); } catch (Exception ex) { return false; } } }