当前位置:文档之家› Java--登陆注册系统(模仿qq登陆界面)

Java--登陆注册系统(模仿qq登陆界面)

Java--登陆注册系统(数据库)import javax.swing.*;import java.awt.*;import java.awt.event.*;import java.sql.*;public class DengLu extends JFrame implements ActionListener,KeyListener,ItemListener,FocusListener{JLabel top,bott;JComboBox combobox;JPasswordField password;JButton b_login,b_regedit;JPanel panelMain,panel_center;public DengLu (){super("登陆系统");panelMain = new JPanel(new BorderLayout(10, 10)){public void paintComponent(Graphics g) {Graphics2D g2 = (Graphics2D)g;super.paintComponent(g);Image img = Toolkit.getDefaultToolkit().getImage("G:\\image\\Denglubg.jpg");g2.drawImage(img, 0, 0, this.getWidth(), this.getHeight(), this);}};top = new JLabel ();top.setPreferredSize(new Dimension(this.getWidth(),30));bott = new JLabel ();bott.setForeground(Color.white);bott.setPreferredSize(new Dimension(this.getWidth(),25));centerBuild();additem();panelMain.add(top,BorderLayout.NORTH);panelMain.add(bott,BorderLayout.SOUTH);panelMain.add(panel_center,BorderLayout.CENTER);combobox.addItemListener(this);password.addActionListener(this);b_login.addActionListener(this);b_regedit.addActionListener(this);combobox.addFocusListener(this);password.addFocusListener(this);b_login.addFocusListener(this);b_regedit.addFocusListener(this);combobox.addKeyListener(this);password.addKeyListener(this);this.addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent we) {quit();}});this.setContentPane(panelMain); //设置窗口面板this.setSize(350, 235);Dimension frm=this.getSize();Dimension scr=Toolkit.getDefaultToolkit().getScreenSize();this.setLocation((scr.width-frm.width)/2,(scr.height-frm.height)/2);this.setResizable (false); //设置窗口不可放大缩小this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);//sunswing.setWindowCenter(this);this.setVisible(true);//password.requestFocus(true); //设置焦点给密码框}private void centerBuild(){JLabel L1,L2;panel_center=new JPanel(null);panel_center.setOpaque(false);L1=new JLabel("用户名:");L1.setForeground(Color.white);L2=new JLabel("登录密码:");L2.setForeground(Color.white);b_login=new JButton("登陆");b_regedit=new JButton("注册");combobox=new JComboBox();combobox.addItem("");password=new JPasswordField();L1.setBounds(50,14,60,20);L2.setBounds(50,48,60,20);b_login.setBounds(68,87,86,28);b_regedit.setBounds(186,87,86,28);combobox.setBounds (115,12,171,23);password.setBounds (115,46,170,23);combobox.setEditable(true);panel_center.add(L1);panel_center.add(L2);panel_center.add(b_login);panel_center.add(b_regedit);panel_center.add(combobox);panel_center.add(password);}private void quit(){int flag = 0;String msg = "您现在要关闭系统吗?";flag = JOptionPane.showConfirmDialog(null, msg, "提示", JOptionPane.YES_NO_OPTION);if(flag == JOptionPane.YES_OPTION) {this.setVisible(false);System.exit(0);}}/**=======================================================================* ** 【## private void zhuce】注册用户界面* 参数:无* 返回值:无* 权限:private* 功能:通过“用户名,登陆密码”进行用户注册**=======================================================================** */private void zhuce(){String utext=String.valueOf(combobox.getSelectedItem());String ptext=password.getText();if(utext.trim().equals("")||ptext.equals("")){JOptionPane.showMessageDialog(null,"用户名、登陆密码不能为空!","警告",RMATION_MESSAGE);}else{Connection conn = null;PreparedStatement ps=null;ResultSet rs=null;try{conn=Utils.getConnection();String sql="select users from login.dlu where users=?";ps=conn.prepareStatement(sql);ps.setString(1, utext);rs=ps.executeQuery();if(rs.next()){ //用户存在JOptionPane.showMessageDialog(null,"该用户已注册,请重新输入用户名注册,\n或者直接登录!","提醒",RMATION_MESSAGE);}else{if(ptext.length()>=6){sql="insert into login.dlu (users,passwords)values(?,?)";ps=conn.prepareStatement(sql);ps.setString(1, utext);ps.setString(2, ptext);int up=ps.executeUpdate();if(up==1){JOptionPane.showMessageDialog(null,"注册成功,\n可以直接登录!","提醒",RMATION_MESSAGE);bott.setText("已注册成功!请登录!");combobox.removeAllItems();additem();}}else{bott.setText("提示:密码不能小于六位数!");}}}catch(Exception e){throw new RunException(e.getMessage(),e);}finally{Utils.free(rs, ps, conn);}}}/**=======================================================================* ** 【## private void additem()】增加最后登录用户的7名* 参数:无* 返回值:无* 权限:private* 功能:模仿qq的登录记录功能**=======================================================================** */private void additem(){Connection conn=null;PreparedStatement ps=null;ResultSet rs=null;try {conn=Utils.getConnection();String sql="select users from login.dlu";ps=conn.prepareStatement(sql);rs=ps.executeQuery();rs.afterLast();int i=0;String s=null;while(rs.previous()&& i<8){i++;s=rs.getString("users");combobox.addItem(s);}} catch (SQLException e) {throw new RunException(e.getMessage(),e);}finally{Utils.free(rs, ps, conn);}}private void getPassword(){Connection conn=null;PreparedStatement ps=null;ResultSet rs=null;String ut=String.valueOf(combobox.getSelectedItem());try {conn=Utils.getConnection();String sql="select users,passwords from login.dlu where users=?";ps=conn.prepareStatement(sql);ps.setString(1, ut);rs=ps.executeQuery();String s1=null;while(rs.next()){s1=rs.getString("passwords");}password.setText(s1);} catch (SQLException e) {throw new RunException(e.getMessage(),e);}finally{Utils.free(rs, ps, conn);}}/**=======================================================================* ** 【## private void denglu】登陆界面* 参数:无* 返回值:无* 权限:private* 功能:密码验证通过,进入主操作界面,仅类内使用**=======================================================================** */private void denglu(){String utext=String.valueOf(combobox.getSelectedItem());String ptext=password.getText();Connection conn=null;PreparedStatement ps=null;ResultSet rs=null;try {conn=Utils.getConnection();String sql="select users,passwords from login.dlu where users=?";ps=conn.prepareStatement(sql);ps.setString(1, utext);rs=ps.executeQuery();if(rs.next()){if(ptext.equals(rs.getString(2))){this.setVisible(false);new MainProgram();System.exit(1);}else{JOptionPane.showMessageDialog(null,"您输入的用户和密码不正确,\n请重新输入","错误", JOptionPane.ERROR_MESSAGE);}}else{JOptionPane.showMessageDialog(null,"您输入的用户不存在,请重新输入,\n或者先注册","错误", RMATION_MESSAGE);}} catch(Exception e){throw new RunException(e.getMessage(),e);}finally{Utils.free(rs, ps, conn);}}/**=======================================================================* ** ActionListener 监听**=======================================================================** */public void actionPerformed(ActionEvent en){if(en.getSource()==b_regedit){zhuce();}else if(en.getSource()==b_login){denglu();}}/**=======================================================================* ** ItemListener 监听**=======================================================================** */public void itemStateChanged(ItemEvent es){getPassword();password.requestFocus(true);}/**=======================================================================* ** FocusListener 监听**=======================================================================** */public void focusGained(FocusEvent fe){if(fe.getSource()==combobox){bott.setText("提示:请输入用户名!");}else if(fe.getSource()==password){bott.setText("提示:请输入密码!密码必须大于六位数");}else if(fe.getSource()==b_login){bott.setText("提示:登录系统!");}else if(fe.getSource()==b_regedit){bott.setText("提示:注册账号");}}public void focusLost(FocusEvent esd){}/**=======================================================================* ** KeyListener 监听**=======================================================================** */public void keyPressed(KeyEvent ke){if(ke.getKeyChar()==KeyEvent.VK_ENTER){if(ke.getSource()==combobox){password.requestFocus(true);}if(password.getPassword().length>0){denglu();}}else if(ke.getKeyChar() == KeyEvent.VK_ESCAPE) { //按ESC键退出系统quit();}}public void keyReleased(KeyEvent kr){}public void keyTyped(KeyEvent kt){}/**=======================================================================* ** 主函数main 接入口**=======================================================================** */public static void main(String args[]){new DengLu();}}class RunException extends RuntimeException {public RunException() {// TODO Auto-generated constructor stub}public RunException(String message) {super(message);// TODO Auto-generated constructor stub}public RunException(Throwable cause) {super(cause);// TODO Auto-generated constructor stub}public RunException(String message, Throwable cause) {super(message, cause);// TODO Auto-generated constructor stub}}class Utils {private static String url="jdbc:mysql://localhost:3306/login";private static String user="root";private static String password="136597";private Utils(){}static{try {Class.forName("com.mysql.jdbc.Driver");} catch (Exception e) {throw new RunException(e.getMessage(),e);}}static Connection getConnection(){try {return DriverManager.getConnection(url,user,password);} catch (SQLException e) {throw new RunException(e.getMessage(),e);}}public static void free(ResultSet rs,PreparedStatement ps,Connection conn){ try{if(ps!=null){ps.close();}}catch(Exception e){e.printStackTrace();}。

相关主题