?? userlogin.java
字號:
package Manager;
import java.awt.*;
import javax.swing.*;
import com.borland.jbcl.layout.*;
import java.awt.event.*;
import java.sql.*;
public class userLogin extends JDialog{
/**
* <p>Title:用戶登錄</p>
* <p>Description:系統開始用戶登錄 </p>
* <p>Copyright: Copyright (c) 2008</p>
* <p>Company:南華大學計算機系 </p>
* @author 王云飛
* @version 1.0
*/
private static final long serialVersionUID = 1L;
ResultSet rs; //定義結果集
private DBManager db =new DBManager();//定義數據庫操作對象
MainFrame myMain; //定義主窗口類,用于操作父窗體
JPanel panel1 = new JPanel();
BorderLayout borderLayout1 = new BorderLayout();
XYLayout xYLayout1 = new XYLayout();
JLabel jLabel2 = new JLabel();
JButton jButtonCancel = new JButton();
JLabel jLabel1 = new JLabel();
JTextField jTextFieldusername = new JTextField();
JButton jButtonOK = new JButton();
JPasswordField jPasswordold = new JPasswordField();
public userLogin(Frame frame, String title, boolean modal) {
super(frame, title, modal);
try {
jbInit();
pack();
}
catch(Exception ex) {
ex.printStackTrace();
}
}
public userLogin() {
this(null, "", false);
}
public userLogin(MainFrame pmain) {
this(null, "", false);
myMain=pmain;
}
private void jbInit() throws Exception {
this.getContentPane().setLayout(xYLayout1);
jLabel2.setFont(new java.awt.Font("Dialog", 0, 16));
jLabel2.setText("密碼");
jButtonCancel.setFont(new java.awt.Font("Dialog", 0, 16));
jButtonCancel.setText("退出");
jButtonCancel.addMouseListener(new userLogin_jButtonCancel_mouseAdapter(this));
jLabel1.setFont(new java.awt.Font("Dialog", 0, 16));
jLabel1.setText("用戶名");
jTextFieldusername.setFont(new java.awt.Font("Dialog", 0, 16));
jTextFieldusername.setText("");
jButtonOK.setFont(new java.awt.Font("Dialog", 0, 16));
jButtonOK.setText("確定");
jButtonOK.addMouseListener(new userLogin_jButtonOK_mouseAdapter(this));
jPasswordold.setText("");
xYLayout1.setWidth(410);
xYLayout1.setHeight(239);
this.setTitle("用戶登錄");
getContentPane().add(panel1,new XYConstraints(0, 0, -1, -1));
this.getContentPane().add(jLabel1,new XYConstraints(86, 49, 99, 34));
this.getContentPane().add(jLabel2,new XYConstraints(88, 98, 80, 34));
this.getContentPane().add(jTextFieldusername,new XYConstraints(203, 50, 111, 31));
this.getContentPane().add(jPasswordold,new XYConstraints(204, 93, 110, 33));
this.getContentPane().add(jButtonCancel,new XYConstraints(201, 162, 94, 32));
this.getContentPane().add(jButtonOK,new XYConstraints(89, 163, 91, 31));
}
void jButtonCancel_mouseClicked(MouseEvent e) {
this.dispose();
}
//操作員登陸
void jButtonOK_mouseClicked(MouseEvent e) {
//檢驗用戶名是否為空
if(jTextFieldusername.getText().toString().trim().equals("") )
{ JOptionPane.showMessageDialog(null,"用戶名不可為空!");
return;}
//檢驗密碼是否為空
if(jPasswordold.getText().toString().trim().equals("") )
{JOptionPane.showMessageDialog(null,"密碼不可為空!");
return;}
//定義字符串對象,代表sql語句
String strSQL ;
//生成sql語句,用戶名和密碼從相應文本框取得
strSQL="select * from user where Username='"+jTextFieldusername.getText().toString().trim() +"' and Password='"+jPasswordold.getText().toString().trim()+"'";
//由DBManager對象執行查詢過程
rs=db.getResult(strSQL) ;
//判斷結果集是否為空
boolean isexist=false;
try {
isexist = rs.first();
}
catch (SQLException ex1) {
}
//若為空,則說明用戶名不存在或密碼不正確,彈出警告信息,并設定主窗體權限為無
if(!isexist )
{JOptionPane.showMessageDialog(null,"用戶名不存在,或密碼不正確!");
myMain.setEnable(false,false);
}
//若不為空,則說明用戶名存在且密碼正確,設定主窗體權限為有
else
{
try {
rs.first();
//若為管理員...
if (rs.getString("Power").equals("管理員")) {
myMain.setEnable(true, true);
this.dispose();
}
//若為操作員
else {
myMain.setEnable(true, false);
this.dispose();
}
}
catch (SQLException ex) {
}
}
}
}
class userLogin_jButtonCancel_mouseAdapter extends java.awt.event.MouseAdapter {
userLogin adaptee;
userLogin_jButtonCancel_mouseAdapter(userLogin adaptee) {
this.adaptee = adaptee;
}
public void mouseClicked(MouseEvent e) {
adaptee.jButtonCancel_mouseClicked(e);
}
}
class userLogin_jButtonOK_mouseAdapter extends java.awt.event.MouseAdapter {
userLogin adaptee;
userLogin_jButtonOK_mouseAdapter(userLogin adaptee) {
this.adaptee = adaptee;
}
public void mouseClicked(MouseEvent e) {
adaptee.jButtonOK_mouseClicked(e);
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -