package n1;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.sql.ResultSet;
public class Login extends JFrame {
private JLabel l1,l2,backimg;
private JTextField user;
private JPasswordField pwd;
private JButton bt1,bt2;
private ImageIcon img,logoimg;
private Icon imgIcon;
private Image logo;
private DataBaseManager mydb;
private ResultSet rs;
public Login() {
super("学生管理系统");
Container c = this.getContentPane();
logoimg = new ImageIcon("img/logo.gif");
logo = logoimg.getImage();
this.setIconImage(logo);
c.setLayout(null);
img = new ImageIcon("img/001.jpg");
imgIcon = img;
backimg = new JLabel(imgIcon);
l1 = new JLabel("用户名");
l2 = new JLabel("密码");
user = new JTextField("");
pwd = new JPasswordField("");
bt1 = new JButton("ok");
bt2 = new JButton("reset");
l1.setBounds(50,20, 60, 20);
l2.setBounds(50,50,60,20);
l1.setForeground(Color.red);
l2.setForeground(Color.red);
backimg.setBounds(0,0,300,200);
user.setBounds(120,20,80,20);
pwd.setBounds(120,50,80,20);
bt1.setBounds(70,90,70,20);
bt2.setBounds(140,90,70,20);
bt1.setForeground(Color.red);
bt2.setForeground(Color.red);
// 添加组件到容器。
c.add(l1);
c.add(l2);
c.add(user);
c.add(pwd);
c.add(bt1);
c.add(bt2);
c.add(backimg);
// 设置组件的属性(颜色,大小等)。
this.getRootPane().setWindowDecorationStyle(JRootPane.FRAME); //设置javaGUI.
this.setUndecorated(true); //取掉标题栏.
this.setResizable(false); //窗口大小固定,不可调整。
this.setSize(300,200);
this.setLocation(200,200);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //关闭窗口。
// 注册监听事件。
bt1.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
//检验用户名和密码。
checkLogin();
}
});
// 敲Enter键登录。
pwd.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
//检验用户名和密码。
checkLogin();
}
});
bt2.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
//检验用户名和密码。
reset();
}
});
}
public void checkLogin() {
String user = this.user.getText().trim();
String pwd = new String(this.pwd.getPassword()); //注意返回类型。
try {
mydb = DataBaseManager.getInstance();
rs = mydb.getResult("select * from admin where user = '" + user + "'");
if(rs.next()){
if(rs.getString("pwd").equals(pwd)) {
this.dispose();
new MainFrame();
}
else {
JOptionPane.showMessageDialog(null, "密码不正确");
this.user.setText("");
this.pwd.setText("");
}
}
else {
JOptionPane.showMessageDialog(null, "用户不存在");
this.user.setText("");
this.pwd.setText("");
}
}
catch(Exception e) {
System.out.println("可能数据库有问题!");
}
finally {
mydb.closeConnection();
}
/* if(user.equals("admin")) {
if(pwd.equals("123")) {
this.dispose(); //关掉前一个窗口,并释放内存。
// this.setVisible(false); //关掉前一个窗口,但不释放内存。
new MainFrame();
}
else {
JOptionPane.showMessageDialog(null, "密码不正确");
}
}
else {
JOptionPane.showMessageDialog(null, "用户名不正确");
}*/
}
private void reset() {
this.user.setText("");
this.pwd.setText("");
}
public static void main(String[] args) {
new Login();
}
}