Logincs.java~67~
上传用户:liming9091
上传日期:2014-10-27
资源大小:3376k
文件大小:8k
源码类别:

Java编程

开发平台:

Java

  1. package uml_gaokai;
  2. import java.awt.*;
  3. import javax.swing.*;
  4. import com.borland.jbcl.layout.*;
  5. import java.awt.event.*;
  6. import com.borland.dx.sql.dataset.*;
  7. import java.sql.*;
  8. import javax.swing.border.*;
  9. /**
  10.  * <p>Title: </p>
  11.  * <p>Description: </p>
  12.  * <p>Copyright: Copyright (c) 2003</p>
  13.  * <p>Company: </p>
  14.  * @author unascribed
  15.  * @version 1.0
  16.  */
  17. public class Logincs extends JDialog implements ActionListener{
  18.   private JPanel panel1 = new JPanel();
  19.   private BorderLayout borderLayout1 = new BorderLayout();
  20.   private JPanel jPanel1 = new JPanel();
  21.   private XYLayout xYLayout1 = new XYLayout();
  22.   private JLabel jLabel1 = new JLabel();
  23.   private JLabel jLabel2 = new JLabel();
  24.   private JTextField jTextField1 = new JTextField();
  25.   private JButton jButton1 = new JButton();
  26.   private JButton jButton2 = new JButton();
  27.   private JPasswordField jPasswordField1 = new JPasswordField();
  28.   private Database database1 = new Database();
  29.   private QueryDataSet queryDataSet1 = new QueryDataSet();
  30.   private JLabel jLabel3 = new JLabel();
  31.   private JLabel jLabel4 = new JLabel();
  32.   private JLabel jLabel5 = new JLabel();
  33.   private TitledBorder titledBorder1;
  34.   private TitledBorder titledBorder2;
  35.   private JLabel jLabel6 = new JLabel();
  36.   public Logincs(Frame frame, String title, boolean modal) {
  37.     super(frame, title, modal);
  38.     try {
  39.       jbInit();
  40.       pack();
  41.     }
  42.     catch(Exception ex) {
  43.       ex.printStackTrace();
  44.     }
  45.   }
  46.   public Logincs() {
  47.     this(null, "", false);
  48.   }
  49.   private void jbInit() throws Exception {
  50.     titledBorder1 = new TitledBorder("");
  51.     titledBorder2 = new TitledBorder("");
  52.     panel1.setLayout(borderLayout1);
  53.     jPanel1.setLayout(xYLayout1);
  54.     jLabel1.setForeground(Color.blue);//标签控件的字体颜色
  55.     jLabel1.setText("用户名"); //标签标题
  56.     jLabel2.setForeground(Color.blue);
  57.     jLabel2.setText("密码");
  58.     jButton1.setForeground(UIManager.getColor("List.selectionBackground"));//按钮控件字体颜色
  59.     jButton1.setBorder(BorderFactory.createRaisedBevelBorder());//按钮外观
  60.     jButton1.setText("登录");//按钮标题
  61.     jButton1.addActionListener(this);//加入按钮事件
  62.     jButton2.setForeground(UIManager.getColor("Menu.selectionBackground"));
  63.     jButton2.setBorder(BorderFactory.createRaisedBevelBorder());
  64.     jButton2.setToolTipText("");
  65.     jButton2.setText("取消");
  66.     jButton2.addActionListener(this);
  67.     jPanel1.setBorder(BorderFactory.createEtchedBorder());
  68.     jPanel1.setMinimumSize(new Dimension(600, 600));//设置面板大小
  69.     jPanel1.setPreferredSize(new Dimension(400, 300));
  70.     jPasswordField1.setBorder(titledBorder1);
  71.     jPasswordField1.setToolTipText("");
  72.     database1.setConnection(new com.borland.dx.sql.dataset.ConnectionDescriptor("jdbc:odbc:gaokaiSQL", "sa", "", false, "sun.jdbc.odbc.JdbcOdbcDriver"));
  73.     queryDataSet1.setQuery(new com.borland.dx.sql.dataset.QueryDescriptor(database1, "SELECT [user].userName,[user].userPassword,[user].userAuthority n" +
  74.         "FROM [UML_CS].[dbo].[user]", null, true, Load.ALL));
  75.     this.setResizable(false);
  76.     this.setTitle("Login");
  77.     jLabel3.setBackground(Color.white);//标签背景颜色
  78.     jLabel3.setForeground(Color.green);
  79.     jLabel3.setBorder(BorderFactory.createEtchedBorder());
  80.     jLabel3.setText("                                        小型超市管理系统");
  81.     jLabel4.setBorder(BorderFactory.createEtchedBorder());
  82.     jLabel4.setText(" ——制作人:高凯");
  83.     jLabel5.setBorder(BorderFactory.createEtchedBorder());
  84.     jLabel5.setToolTipText("");
  85.     jLabel5.setText("——版本号: 1.1");
  86.     jTextField1.setBorder(titledBorder2);
  87.     jLabel6.setBorder(BorderFactory.createEtchedBorder());
  88.     getContentPane().add(panel1);
  89.     panel1.add(jPanel1, BorderLayout.WEST);
  90.     jPanel1.add(jLabel3,     new XYConstraints(-1, 0, 395, 30));
  91.     jPanel1.add(jLabel5,     new XYConstraints(274, 30, 121, 32));
  92.     jPanel1.add(jLabel4, new XYConstraints(127, 32, 143, 30));
  93.     jPanel1.add(jPasswordField1, new XYConstraints(143, 196, 132, 24));
  94.     jPanel1.add(jTextField1, new XYConstraints(143, 136, 132, 24));
  95.     jPanel1.add(jLabel2, new XYConstraints(23, 195, 60, 25));
  96.     jPanel1.add(jLabel1, new XYConstraints(23, 134, 60, 25));
  97.     jPanel1.add(jLabel6,    new XYConstraints(-2, 65, 396, 191));
  98.     jPanel1.add(jButton1, new XYConstraints(90, 274, 68, 28));
  99.     jPanel1.add(jButton2, new XYConstraints(193, 275, 69, 28));
  100.   }
  101.   public void actionPerformed(ActionEvent e){
  102.     //the user login
  103.     if(e.getSource() == jButton1) //得到界面单击事件
  104.     {
  105.       try{
  106.         String userAuthority = null ;
  107.         String userName = null ;
  108.         String userPassword = null ;
  109.         String testName = jTextField1.getText().toString() ;//从jTextField1中得到用户名
  110.         String testPassword = String.valueOf(jPasswordField1.getPassword()) ;//
  111.         Statement sql = database1.createStatement() ; //
  112.         ResultSet rs = sql.executeQuery("SELECT userName,userPassword , userAuthority FROM [UML_CS].[dbo].[user] WHERE userName = '"+testName+"'") ;
  113.         //
  114.         while(rs.next()) //
  115.         {
  116.           userName = rs.getString("userName") ; //
  117.           userPassword = rs.getString("userPassword") ;//
  118.           userAuthority = rs.getString("userAuthority") ;//
  119.         }
  120.         verifyUser(userName,userPassword,testPassword,userAuthority); //
  121.         cancel() ;//
  122.       }
  123.       catch(SQLException eee){eee.printStackTrace();}
  124.     }
  125.     //exit the System
  126.     else if(e.getSource() == jButton2){
  127.       cancel();
  128.     }
  129.   }
  130.   void cancel(){
  131.     dispose();//
  132.   }
  133.   //验证用户名以及密码
  134.   void verifyUser(String userName , String userPassword , String testPassword, String userAuthority)
  135.   {
  136.     if(userName == null)
  137.     {
  138.       JOptionPane.showMessageDialog(null,"用户名不存在,请先注册","verify user",JOptionPane.DEFAULT_OPTION);
  139.     }
  140.     else
  141.     {
  142.       if(userPassword.equals(testPassword) == false)
  143.       {
  144.         JOptionPane.showMessageDialog(null,"您输入的密码不正确,请重试","verify password" , JOptionPane.DEFAULT_OPTION);
  145.         //
  146.       }
  147.       else{
  148.         JOptionPane.showMessageDialog(null,"success","",JOptionPane.DEFAULT_OPTION);
  149.         //
  150.         verifyUserAuthority(userAuthority) ;//
  151.       }
  152.     }
  153.   }
  154.   //验证用户权限
  155.   void verifyUserAuthority(String userAuthority){
  156.     if(userAuthority.equals("admin"))//如果用户为admin,则弹出管理员界面
  157.     {
  158.       AdminWindow adminWindow = new AdminWindow() ;
  159.       //设置对话框
  160.       Dimension dlgSize = adminWindow.getPreferredSize();
  161.       Dimension frmSize = getSize();
  162.       Point loc = getLocation();
  163.       adminWindow.setLocation((frmSize.width - dlgSize.width) / 2 + loc.x, (frmSize.height - dlgSize.height) / 2 + loc.y);
  164.       adminWindow.setModal(true);
  165.       adminWindow.pack();
  166.       adminWindow.show();
  167.     }
  168.     else if (userAuthority.equals("customer")){
  169.       CustomerWindow customerWindow =new CustomerWindow() ;
  170.       Dimension dlgSize = customerWindow.getPreferredSize();
  171.       Dimension frmSize = getSize();
  172.       Point loc = getLocation();
  173.       customerWindow.setLocation((frmSize.width - dlgSize.width) / 2 + loc.x, (frmSize.height - dlgSize.height) / 2 + loc.y);
  174.       customerWindow.setModal(true);
  175.       customerWindow.pack();
  176.       customerWindow.show();
  177.     }
  178.     else if (userAuthority.equals("buyer")){
  179.       BuyerWindow buyerWindow =new BuyerWindow() ;
  180.       Dimension dlgSize = buyerWindow.getPreferredSize();
  181.       Dimension frmSize = getSize();
  182.       Point loc = getLocation();
  183.       buyerWindow.setLocation((frmSize.width - dlgSize.width) / 2 + loc.x, (frmSize.height - dlgSize.height) / 2 + loc.y);
  184.       buyerWindow.setModal(true);
  185.       buyerWindow.pack();
  186.       buyerWindow.show();
  187.     }
  188.     else if (userAuthority.equals("saler")){
  189.       SalesWindow salesWindow =new SalesWindow() ;
  190.       Dimension dlgSize = salesWindow.getPreferredSize();
  191.       Dimension frmSize = getSize();
  192.       Point loc = getLocation();
  193.       salesWindow.setLocation((frmSize.width - dlgSize.width) / 2 + loc.x, (frmSize.height - dlgSize.height) / 2 + loc.y);
  194.       salesWindow.setModal(true);
  195.       salesWindow.pack();
  196.       salesWindow.show();
  197.     }
  198.     else if (userAuthority.equals("manager")){
  199.       ManagementWindow managementWindow =new ManagementWindow() ;
  200.       Dimension dlgSize = managementWindow.getPreferredSize();
  201.       Dimension frmSize = getSize();
  202.       Point loc = getLocation();
  203.       managementWindow.setLocation((frmSize.width - dlgSize.width) / 2 + loc.x, (frmSize.height - dlgSize.height) / 2 + loc.y);
  204.       managementWindow.setModal(true);
  205.       managementWindow.pack();
  206.       managementWindow.show();
  207.     }
  208.   }
  209. }