?? logincs.java
字號:
package uml_gaokai;import java.awt.*;import javax.swing.*;import com.borland.jbcl.layout.*;import java.awt.event.*;import com.borland.dx.sql.dataset.*;import java.sql.*;import javax.swing.border.*;/** * <p>Title: </p> * <p>Description: </p> * <p>Copyright: Copyright (c) 2003</p> * <p>Company: </p> * @author unascribed * @version 1.0 */public class Logincs extends JDialog implements ActionListener{ private JPanel panel1 = new JPanel(); private BorderLayout borderLayout1 = new BorderLayout(); private JPanel jPanel1 = new JPanel(); private XYLayout xYLayout1 = new XYLayout(); private JLabel jLabel1 = new JLabel(); private JLabel jLabel2 = new JLabel(); private JTextField jTextField1 = new JTextField(); private JButton jButton1 = new JButton(); private JButton jButton2 = new JButton(); private JPasswordField jPasswordField1 = new JPasswordField(); private Database database1 = new Database(); private QueryDataSet queryDataSet1 = new QueryDataSet(); private JLabel jLabel3 = new JLabel(); private JLabel jLabel4 = new JLabel(); private JLabel jLabel5 = new JLabel(); private TitledBorder titledBorder1; private TitledBorder titledBorder2; private JLabel jLabel6 = new JLabel(); public Logincs(Frame frame, String title, boolean modal) { super(frame, title, modal); try { jbInit(); pack(); } catch(Exception ex) { ex.printStackTrace(); } } public Logincs() { this(null, "", false); } private void jbInit() throws Exception { titledBorder1 = new TitledBorder(""); titledBorder2 = new TitledBorder(""); panel1.setLayout(borderLayout1); jPanel1.setLayout(xYLayout1); jLabel1.setForeground(Color.blue);//標簽控件的字體顏色 jLabel1.setText("用戶名"); //標簽標題 jLabel2.setForeground(Color.blue); jLabel2.setText("密碼"); jButton1.setForeground(UIManager.getColor("List.selectionBackground"));//按鈕控件字體顏色 jButton1.setBorder(BorderFactory.createRaisedBevelBorder());//按鈕外觀 jButton1.setText("登錄");//按鈕標題 jButton1.addActionListener(this);//加入按鈕事件 jButton2.setForeground(UIManager.getColor("Menu.selectionBackground")); jButton2.setBorder(BorderFactory.createRaisedBevelBorder()); jButton2.setToolTipText(""); jButton2.setText("取消"); jButton2.addActionListener(this); jPanel1.setBorder(BorderFactory.createEtchedBorder()); jPanel1.setMinimumSize(new Dimension(600, 600));//設置面板大小 jPanel1.setPreferredSize(new Dimension(400, 300)); jPasswordField1.setBorder(titledBorder1); jPasswordField1.setToolTipText(""); database1.setConnection(new com.borland.dx.sql.dataset.ConnectionDescriptor("jdbc:odbc:gaokaiSQL", "sa", "", false, "sun.jdbc.odbc.JdbcOdbcDriver")); queryDataSet1.setQuery(new com.borland.dx.sql.dataset.QueryDescriptor(database1, "SELECT [user].userName,[user].userPassword,[user].userAuthority \n" + "FROM [UML_CS].[dbo].[user]", null, true, Load.ALL)); this.setResizable(false); this.setTitle("Login"); jLabel3.setBackground(Color.white);//標簽背景顏色 jLabel3.setForeground(Color.green); jLabel3.setBorder(BorderFactory.createEtchedBorder()); jLabel3.setText(" 小型超市管理系統"); jLabel4.setBorder(BorderFactory.createEtchedBorder()); jLabel4.setText(" ——制作人:高凱"); jLabel5.setBorder(BorderFactory.createEtchedBorder()); jLabel5.setToolTipText(""); jLabel5.setText("——版本號: 1.1"); jTextField1.setBorder(titledBorder2); jLabel6.setBorder(BorderFactory.createEtchedBorder()); getContentPane().add(panel1); panel1.add(jPanel1, BorderLayout.WEST); jPanel1.add(jLabel3, new XYConstraints(-1, 0, 395, 30)); jPanel1.add(jLabel5, new XYConstraints(274, 30, 121, 32)); jPanel1.add(jLabel4, new XYConstraints(127, 32, 143, 30)); jPanel1.add(jPasswordField1, new XYConstraints(143, 196, 132, 24)); jPanel1.add(jTextField1, new XYConstraints(143, 136, 132, 24)); jPanel1.add(jLabel2, new XYConstraints(23, 195, 60, 25)); jPanel1.add(jLabel1, new XYConstraints(23, 134, 60, 25)); jPanel1.add(jLabel6, new XYConstraints(-2, 65, 396, 191)); jPanel1.add(jButton1, new XYConstraints(90, 274, 68, 28)); jPanel1.add(jButton2, new XYConstraints(193, 275, 69, 28)); } public void actionPerformed(ActionEvent e){ //the user login if(e.getSource() == jButton1) //得到界面單擊事件 { try{ String userAuthority = null ; String userName = null ; String userPassword = null ; String testName = jTextField1.getText().toString() ;//從jTextField1中得到用戶名 String testPassword = String.valueOf(jPasswordField1.getPassword()) ;//從jPassword中得到用戶密碼 Statement sql = database1.createStatement() ; //打開Statement對象 ResultSet rs = sql.executeQuery("SELECT userName,userPassword , userAuthority FROM [UML_CS].[dbo].[user] WHERE userName = '"+testName+"'") ; //執行SQL語句,從數據庫當中選擇用戶名,密碼,和權限。 while(rs.next()) // { userName = rs.getString("userName") ; //從數據庫當中得到用戶名 userPassword = rs.getString("userPassword") ;//從數據庫當中得到密碼 userAuthority = rs.getString("userAuthority") ;//從數據庫當中得到權限 } verifyUser(userName,userPassword,testPassword,userAuthority); //調用verifyUser方法 cancel() ;//退出 } catch(SQLException ex){ex.printStackTrace();} } //exit the System else if(e.getSource() == jButton2){ cancel(); } } void cancel() { dispose(); } //驗證用戶名以及密碼 void verifyUser(String userName , String userPassword , String testPassword, String userAuthority) { if(userName == null) //判斷用戶名是否為空 { JOptionPane.showMessageDialog(null,"用戶名不存在,請先注冊","verify user",JOptionPane.DEFAULT_OPTION); //拋出對話框 } else { if(userPassword.equals(testPassword) == false) //判斷密碼是否相等 { JOptionPane.showMessageDialog(null,"您輸入的密碼不正確,請重試","verify password" , JOptionPane.DEFAULT_OPTION); //拋出對話框 } else{ JOptionPane.showMessageDialog(null,"success","",JOptionPane.DEFAULT_OPTION); // verifyUserAuthority(userAuthority) ;//進行權限判斷 } } } //驗證用戶權限 void verifyUserAuthority(String userAuthority){ if(userAuthority.equals("admin"))//如果用戶為admin,則彈出管理員界面 { AdminWindow adminWindow = new AdminWindow() ; //設置對話框 Dimension dlgSize = adminWindow.getPreferredSize(); Dimension frmSize = getSize(); Point loc = getLocation(); adminWindow.setLocation((frmSize.width - dlgSize.width) / 2 + loc.x, (frmSize.height - dlgSize.height) / 2 + loc.y); adminWindow.setModal(true); adminWindow.pack(); adminWindow.show(); } else if (userAuthority.equals("customer")){ CustomerWindow customerWindow =new CustomerWindow() ; Dimension dlgSize = customerWindow.getPreferredSize(); Dimension frmSize = getSize(); Point loc = getLocation(); customerWindow.setLocation((frmSize.width - dlgSize.width) / 2 + loc.x, (frmSize.height - dlgSize.height) / 2 + loc.y); customerWindow.setModal(true); customerWindow.pack(); customerWindow.show(); } else if (userAuthority.equals("buyer")){ BuyerWindow buyerWindow =new BuyerWindow() ; Dimension dlgSize = buyerWindow.getPreferredSize(); Dimension frmSize = getSize(); Point loc = getLocation(); buyerWindow.setLocation((frmSize.width - dlgSize.width) / 2 + loc.x, (frmSize.height - dlgSize.height) / 2 + loc.y); buyerWindow.setModal(true); buyerWindow.pack(); buyerWindow.show(); } else if (userAuthority.equals("saler")){ SalesWindow salesWindow =new SalesWindow() ; Dimension dlgSize = salesWindow.getPreferredSize(); Dimension frmSize = getSize(); Point loc = getLocation(); salesWindow.setLocation((frmSize.width - dlgSize.width) / 2 + loc.x, (frmSize.height - dlgSize.height) / 2 + loc.y); salesWindow.setModal(true); salesWindow.pack(); salesWindow.show(); } else if (userAuthority.equals("manager")){ ManagementWindow managementWindow =new ManagementWindow() ; Dimension dlgSize = managementWindow.getPreferredSize(); Dimension frmSize = getSize(); Point loc = getLocation(); managementWindow.setLocation((frmSize.width - dlgSize.width) / 2 + loc.x, (frmSize.height - dlgSize.height) / 2 + loc.y); managementWindow.setModal(true); managementWindow.pack(); managementWindow.show(); } }}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -