?? userlogin.java
字號:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
public class UserLogin
extends JFrame
implements ActionListener {
DataBaseManager db = new DataBaseManager();
MainFrame mainFrame;
JPanel panel1, panel2;
JLabel userLabel, passwordLabel;
JTextField userTextField;
JPasswordField passwordTextField;
JButton yesBtn, cancelBtn;
Container c;
ResultSet rs;
public UserLogin(MainFrame mainFrame) {
super("用戶登錄");
this.mainFrame = mainFrame;
userLabel = new JLabel("用戶名", JLabel.CENTER);
passwordLabel = new JLabel("密碼", JLabel.CENTER);
userTextField = new JTextField(10);
passwordTextField = new JPasswordField(10);
yesBtn = new JButton("確定");
cancelBtn = new JButton("取消");
yesBtn.addActionListener(this);
cancelBtn.addActionListener(this);
panel1 = new JPanel();
panel1.setLayout(new GridLayout(2, 2));
panel2 = new JPanel();
c = getContentPane();
c.setLayout(new BorderLayout());
panel1.add(userLabel);
panel1.add(userTextField);
panel1.add(passwordLabel);
panel1.add(passwordTextField);
c.add(panel1, BorderLayout.CENTER);
panel2.add(yesBtn);
panel2.add(cancelBtn);
c.add(panel2, BorderLayout.SOUTH);
setSize(300, 300);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == cancelBtn) {
mainFrame.setEnable("else");
this.dispose();
}
else {
char[] password = passwordTextField.getPassword();
String passwordSTR = new String(password);
if (userTextField.getText().trim().equals("")) {
JOptionPane.showMessageDialog(null, "用戶名不可為空!");
return;
}
if (passwordSTR.equals("")) {
JOptionPane.showMessageDialog(null, "密碼不可為空!");
return;
}
String strSQL;
strSQL = "select * from usertable where UserName='" +
userTextField.getText().trim() + "'and Password='" +
passwordSTR + "'";
rs = db.getResult(strSQL);
boolean isExist = false;
try {
isExist = rs.first();
}
catch (SQLException sqle) {
System.out.println(sqle.toString());
}
if (!isExist) {
JOptionPane.showMessageDialog(null, "用戶名不存在或者密碼不正確!");
mainFrame.setEnable("else");
}
else {
try {
rs.first();
mainFrame.setEnable(rs.getString("power").trim());
db.closeConnection();
this.dispose();
}
catch (SQLException sqle2) {
System.out.println(sqle2.toString());
}
}
}
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -