Logincs.java
上传用户:kingwang
上传日期:2022-08-09
资源大小:1562k
文件大小:9k
源码类别:

百货/超市行业

开发平台:

JavaScript

  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()) ;//从jPassword中得到用户密码
  111.         Statement sql = database1.createStatement() ; //打开Statement对象
  112.         ResultSet rs = sql.executeQuery("SELECT userName,userPassword , userAuthority FROM [UML_CS].[dbo].[user] WHERE userName = '"+testName+"'") ;
  113.         //执行SQL语句,从数据库当中选择用户名,密码,和权限。
  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); //调用verifyUser方法
  121.         cancel() ;//退出
  122.       }
  123.       catch(SQLException ex){ex.printStackTrace();}
  124.     }
  125.     //exit the System
  126.     else if(e.getSource() == jButton2){
  127.       cancel();
  128.     }
  129.   }
  130.   void cancel()
  131.   {
  132.     dispose();
  133.   }
  134.   //验证用户名以及密码
  135.   void verifyUser(String userName , String userPassword , String testPassword, String userAuthority)
  136.   {
  137.     if(userName == null) //判断用户名是否为空
  138.     {
  139.       JOptionPane.showMessageDialog(null,"用户名不存在,请先注册","verify user",JOptionPane.DEFAULT_OPTION);
  140.       //抛出对话框
  141.     }
  142.     else
  143.     {
  144.       if(userPassword.equals(testPassword) == false) //判断密码是否相等
  145.       {
  146.         JOptionPane.showMessageDialog(null,"您输入的密码不正确,请重试","verify password" , JOptionPane.DEFAULT_OPTION);
  147.         //抛出对话框
  148.       }
  149.       else{
  150.         JOptionPane.showMessageDialog(null,"success","",JOptionPane.DEFAULT_OPTION);
  151.         //
  152.         verifyUserAuthority(userAuthority) ;//进行权限判断
  153.       }
  154.     }
  155.   }
  156.   //验证用户权限
  157.   void verifyUserAuthority(String userAuthority){
  158.     if(userAuthority.equals("admin"))//如果用户为admin,则弹出管理员界面
  159.     {
  160.       AdminWindow adminWindow = new AdminWindow() ;
  161.       //设置对话框
  162.       Dimension dlgSize = adminWindow.getPreferredSize();
  163.       Dimension frmSize = getSize();
  164.       Point loc = getLocation();
  165.       adminWindow.setLocation((frmSize.width - dlgSize.width) / 2 + loc.x, (frmSize.height - dlgSize.height) / 2 + loc.y);
  166.       adminWindow.setModal(true);
  167.       adminWindow.pack();
  168.       adminWindow.show();
  169.     }
  170.     else if (userAuthority.equals("customer")){
  171.       CustomerWindow customerWindow =new CustomerWindow() ;
  172.       Dimension dlgSize = customerWindow.getPreferredSize();
  173.       Dimension frmSize = getSize();
  174.       Point loc = getLocation();
  175.       customerWindow.setLocation((frmSize.width - dlgSize.width) / 2 + loc.x, (frmSize.height - dlgSize.height) / 2 + loc.y);
  176.       customerWindow.setModal(true);
  177.       customerWindow.pack();
  178.       customerWindow.show();
  179.     }
  180.     else if (userAuthority.equals("buyer")){
  181.       BuyerWindow buyerWindow =new BuyerWindow() ;
  182.       Dimension dlgSize = buyerWindow.getPreferredSize();
  183.       Dimension frmSize = getSize();
  184.       Point loc = getLocation();
  185.       buyerWindow.setLocation((frmSize.width - dlgSize.width) / 2 + loc.x, (frmSize.height - dlgSize.height) / 2 + loc.y);
  186.       buyerWindow.setModal(true);
  187.       buyerWindow.pack();
  188.       buyerWindow.show();
  189.     }
  190.     else if (userAuthority.equals("saler")){
  191.       SalesWindow salesWindow =new SalesWindow() ;
  192.       Dimension dlgSize = salesWindow.getPreferredSize();
  193.       Dimension frmSize = getSize();
  194.       Point loc = getLocation();
  195.       salesWindow.setLocation((frmSize.width - dlgSize.width) / 2 + loc.x, (frmSize.height - dlgSize.height) / 2 + loc.y);
  196.       salesWindow.setModal(true);
  197.       salesWindow.pack();
  198.       salesWindow.show();
  199.     }
  200.     else if (userAuthority.equals("manager")){
  201.       ManagementWindow managementWindow =new ManagementWindow() ;
  202.       Dimension dlgSize = managementWindow.getPreferredSize();
  203.       Dimension frmSize = getSize();
  204.       Point loc = getLocation();
  205.       managementWindow.setLocation((frmSize.width - dlgSize.width) / 2 + loc.x, (frmSize.height - dlgSize.height) / 2 + loc.y);
  206.       managementWindow.setModal(true);
  207.       managementWindow.pack();
  208.       managementWindow.show();
  209.     }
  210.   }
  211. }