?? userlogin.java
字號:
package bookmanager;
import java.awt.*;
import javax.swing.*;
import com.borland.jbcl.layout.*;
import java.awt.event.*;
import java.sql.*;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
public class userLogin
extends JDialog {
//定義結果集
ResultSet rs;
//定義數據庫操作對象
private DBManager db = new DBManager();
//定義主窗口類,用于操作父窗體
BookManagerMain myMain;
JPanel panel1 = new JPanel();
XYLayout xYLayout1 = new XYLayout();
XYLayout xYLayout2 = new XYLayout();
JLabel jLabel1 = new JLabel();
JLabel jLabel2 = new JLabel();
JTextField jTextFieldusername = new JTextField();
JButton jButtonOK = new JButton();
JButton jButtonCancel = 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(BookManagerMain bMain) {
this(null, "", false);
myMain = bMain;
}
//初始化對象
private void jbInit() throws Exception {
panel1.setLayout(xYLayout2);
this.getContentPane().setLayout(xYLayout1);
jLabel1.setFont(new java.awt.Font("Dialog", 0, 16));
jLabel1.setText("用戶名");
jLabel2.setFont(new java.awt.Font("Dialog", 0, 16));
jLabel2.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));
jButtonCancel.setFont(new java.awt.Font("Dialog", 0, 16));
jButtonCancel.setText("取消");
jButtonCancel.addMouseListener(new userLogin_jButtonCancel_mouseAdapter(this));
jPasswordold.setText("");
xYLayout1.setWidth(400);
xYLayout1.setHeight(271);
this.setResizable(true);
this.setTitle("用戶登錄");
panel1.add(jTextFieldusername, new XYConstraints(210, 49, 111, 38));
panel1.add(jPasswordold, new XYConstraints(211, 112, 111, 35));
panel1.add(jLabel1, new XYConstraints(59, 50, 99, 34));
panel1.add(jLabel2, new XYConstraints(60, 116, 80, 34));
panel1.add(jButtonCancel, new XYConstraints(212, 209, 94, 30));
panel1.add(jButtonOK, new XYConstraints(94, 208, 91, 31));
this.getContentPane().add(panel1, new XYConstraints( -2, 0, 400, 272));
}
//若用戶直接按取消,則取消用戶權限
void jButtonCancel_mouseClicked(MouseEvent e) {
myMain.setEnable("else");
this.dispose();
}
void jButtonOK_mouseClicked(MouseEvent e) {
//校驗用戶名是否為空
if (jTextFieldusername.getText().trim().equals("")) {
JOptionPane.showMessageDialog(null, "用戶名不可為空!");
return;
}
//校驗密碼是否為空
if (jPasswordold.getText().trim().equals("")) {
JOptionPane.showMessageDialog(null, "密碼不可為空!");
return;
}
String strSQL;
//生成sql操作語句,查詢要登陸的用戶名是否存在,若存在執行下一步操作,若不存在提示并返回
strSQL = "select * from user where Username='" +
jTextFieldusername.getText().trim() + "' and Password='" +
jPasswordold.getText().trim() + "'";
rs = db.getResult(strSQL);
boolean isexist = false;
try {
isexist = rs.first();
}
catch (SQLException ex1) {
}
//若用戶名不存在,提示警告信息
if (!isexist) {
JOptionPane.showMessageDialog(null, "用戶名不存在,或密碼不正確!");
myMain.setEnable("else");
}
// 若用戶名存在,設置權限
else {
try {
rs.first();
myMain.setEnable(rs.getString("Power").trim());
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 + -