import java.awt.GridLayout;
import java.awt.event.*;
import javax.swing.*;
import com.sdu.wh.bll.UserQueryBll;
import com.sdu.wh.dao.*;
import com.sdu.wh.sql.*;
public class LoginFrame extends JDialog implements ActionListener {
private JTextField textName = new JTextField();
private JPasswordField textPW = new JPasswordField();
private JButton btnOK = new JButton("登录");
private JButton btnCancel = new JButton("取消");
//构造方法,参数1:宿主窗口, 参数2:是否模态窗口
public LoginFrame(JFrame jf, boolean model) {
//调用父类 JDialog 的构造方法
super(jf, model);
this.setTitle("学生选课管理系统登录");
this.setSize(300, 180);
this.setLocation(500, 400);
getContentPane().setLayout(new GridLayout(3,2,10,10));
getContentPane().add(new JLabel("用户名:"));
getContentPane().add(textName);
getContentPane().add(new JLabel("密码:"));
getContentPane().add(textPW);
getContentPane().add(btnOK);
btnOK.addActionListener(this);
getContentPane().add(btnCancel);
btnCancel.addActionListener(this);
this.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand()== "登录") {
//登录密码的验证方法
//通过用户名到数据库中查到该用户名的密码
//与输入的密码比较
String uid=textName.getText();
String password=new String(textPW.getPassword());
String t=null;
String sql="select 用户名,密码,角色,学院 from users where 用户名="+uid;
SqlExecute excute=new SqlExecute();
User user = null;
try {
//如果两个输入框都不空
if(!uid.equals("")){
user = excute.getUser(sql);
}
}
catch (Exception e1) {
e1.printStackTrace();
}
boolean isPass=!uid.equals("")&& uid.equals(user.getUid()) && !password.equals("") && password.equals(user.getPassword());
//如果通过验证
if(isPass) {
//记录登录用户的id,学院,角色信息
StudentMainFrame.cUser.setUid(user.getUid());
StudentMainFrame.cUser.setDepart(user.getDepart());
StudentMainFrame.cUser.setType(user.getType());
//登录成功后销毁该登录窗口
this.dispose();
}
else {
//没有通过验证,弹出登陆错误提示信息
JOptionPane.showMessageDialog(this, "用户名或密码不正确!\r\n请重新填写。");
}
}
else //点取消按钮放弃登录,退出程序
System.exit(0);
}
}