Java登录界面

来源:互联网 发布:linux arm编译源码包 编辑:程序博客网 时间:2024/06/05 16:17

import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;


public class Login extends JFrame implements ActionListener{
 private JPanel jp = new JPanel();
 private JLabel[] jlArray = {
   new JLabel("用户名"),new JLabel("密     码"),new JLabel("")   
 };
 
 private JButton [] jbArray= {
   new JButton("login"),new JButton("regsiter"),new JButton("update"),new JButton("delU")
 };
 
 private JTextField jtf = new JTextField();
 private JPasswordField jpf = new JPasswordField();
 
 public Login() {
  jp.setLayout(null);
  for(int i = 0;i<jlArray.length-1;i++) {
   jlArray[i].setBounds(30, 20+50*i, 100, 20);
   jp.add(jlArray[i]);
  }
  jlArray[2].setBounds(100,180, 100, 20);
  jp.add(jlArray[2]);
  
  jtf.setBounds(130, 20, 150, 20);
  jpf.setBounds(130, 70, 150, 20);
  jpf.setEchoChar('*');
  jtf.addActionListener(this);
  jpf.addActionListener(this);
  jp.add(jtf);
  jp.add(jpf);
  
  jbArray[0].setBounds(30, 110, 100, 20);
  jbArray[1].setBounds(150, 110, 100, 20);
  jbArray[2].setBounds(30, 160, 100, 20);
  jbArray[3].setBounds(150,160, 100, 20);
  
  for(int i =0 ;i<jbArray.length;i++) {
   jbArray[i].addActionListener(this);
   jp.add(jbArray[i]);
  }
  
  Image icon = Toolkit.getDefaultToolkit().getImage("img/ico.igf");
  this.setIconImage(icon);
  this.add(jp);
  this.setTitle("login");
  this.setResizable(false);
  this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  
  this.setBounds(100, 100, 300, 250);
  this.setVisible(true);
  
 }

 @Override
 public void actionPerformed(ActionEvent e) {
  String user = jtf.getText().trim();
  String pwd = String.valueOf(jpf.getPassword());
  
  String sql = "";//login
  if(e.getSource() ==jbArray[0]||e.getSource()== jpf) {
   if(DButil.check(user,pwd)){
    System.out.print("success login");
    this.dispose();
   }
   else{
    jlArray[2].setText("error");
    this.clear();
   }
  }
  // regsit
  else if (e.getSource()==jbArray[1]){
   if(user.equals("")||pwd.equals("")){
    jlArray[2].setText("user or pwd is null");
    this.clear();
   }
   else {
    sql = "select uid from user where uid = '" +user+"'";
    if(DButil.isExit(sql)){
     jlArray[2].setText("sorry ,user already exit");
     this.clear();
    }else {
     sql = "insert into user values('"+user+"','"+pwd+"')";
     if (DButil.update(sql)>0) jlArray[2].setText("regsiter successfully");
    }
   }
  }
  else if(e.getSource()==jbArray[2]){
   if(user.equals("")||pwd.equals("")){
    jlArray[2].setText("please input right pwd!!!");
    this.clear();
   }else if(DButil.check(user,pwd)){
    String password=JOptionPane.showInputDialog(this,"修改密码:","请输入新密码",
              JOptionPane.PLAIN_MESSAGE);
    if(password==null||password.equals("")){
     JOptionPane.showMessageDialog(this,"密码不得为空!!!","错误",
           JOptionPane.WARNING_MESSAGE);     
    }else{
     sql="update user set pwd='"+password+"' where uid='"+user+"'";
     if(DButil.update(sql)>0){
      this.clear();
      jlArray[2].setText("恭喜您!!!密码修改成功,请用新密码登陆");
     }     
    }    
   }
   else{   
    JOptionPane.showMessageDialog(this,"用户名或者密码错误!!!","错误",
           JOptionPane.WARNING_MESSAGE);
    this.clear();
   }
  }  
  else if(e.getSource()==jbArray[3]){   
    if(DButil.check(user,pwd)){//密码和用户都对的情况
     int yn=JOptionPane.showConfirmDialog(this,"是否删除?","删除",
           JOptionPane.YES_NO_OPTION);
     if(yn==JOptionPane.YES_OPTION){
      int count=DButil.delUser(user);
      jlArray[2].setText("用户"+user+"删除成功"+"共删除了"+count+"个联系人");
      this.clear();//清空输入文本框
     }
    }
    else{
     jlArray[2].setText("对不起,非法的用户名和密码!!!");
     this.clear();//清空输入文本框
    }   
  }  
 }
 
 private void clear() {
  jtf.setText("");
  jpf.setText("");
  jtf.requestFocus();
  
 }

 public static void main(String [] args) {
  Login login = new Login();
 }
}

原创粉丝点击