[java]代码库
package com.course.UI;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import com.course.BLL.UserDao;
import com.course.DAL.DbUtil;
import com.course.DAL.User;
import com.course.UTIL.Alert;
import com.course.UTIL.Tools;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.awt.event.ActionEvent;
import javax.swing.JPasswordField;
public class LoginWindow extends JFrame {
private JPanel contentPane;
private JTextField txt_usn;
private JPasswordField txt_pwd;
DbUtil dbUtil=new DbUtil();
UserDao ud=new UserDao();
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
LoginWindow frame = new LoginWindow();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public LoginWindow() {
setTitle("\u7528\u6237\u767B\u5F55");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblNewLabe = new JLabel("课程管理登录系统");
lblNewLabe.setBounds(105, 30, 126, 35);
contentPane.add(lblNewLabe);
JLabel lblNewLabel_1 = new JLabel("用户");
lblNewLabel_1.setBounds(60, 97, 35, 24);
contentPane.add(lblNewLabel_1);
JLabel lblNewLabel_2 = new JLabel("密码");
lblNewLabel_2.setBounds(60, 151, 35, 24);
contentPane.add(lblNewLabel_2);
txt_usn = new JTextField();
txt_usn.setBounds(105, 99, 152, 22);
contentPane.add(txt_usn);
txt_usn.setColumns(10);
JButton btn_login = new JButton(" \u767B\u5F55");
btn_login.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String usn = txt_usn.getText().trim();
String pwd = new String(txt_pwd.getPassword()).trim();
if (Tools.isEmpty(usn)) {
Alert.show("用户名不能为空");
return;
}
if(Tools.isEmpty(pwd)) {
Alert.show("密码不能为空");
return;
}
User user = new User(usn,pwd);
Connection conn = null;
try {
conn = dbUtil.getCon();
if (ud.login(conn, user)!= null)
{
LoginWindow.this.dispose();
new MainWindow().setVisible(true);
}
else
Alert.show("用户名或密码错误!");
}catch (Exception e) {
e.printStackTrace();
Alert.show("登陆失败");
}finally {
try {
dbUtil.getClose(conn);
}catch (Exception e) {
e.printStackTrace();;
}
}
}
});
btn_login.setBounds(66, 213, 74, 23);
contentPane.add(btn_login);
JButton btn_reset = new JButton(" \u91CD\u7F6E");
btn_reset.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
txt_usn.setText("");
txt_pwd.setText("");
}
});
btn_reset.setBounds(205, 213, 74, 23);
contentPane.add(btn_reset);
txt_pwd = new JPasswordField();
txt_pwd.setBounds(105, 153, 152, 22);
contentPane.add(txt_pwd);
}
}