logindialog.java
字号:
package asset;
import java.awt.*;
import javax.swing.*;
import javax.swing.BorderFactory;
import java.awt.Color;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.BorderLayout;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyAdapter;
import java.util.Calendar;
public class LoginDialog
extends JDialog {
JPanel LoginPanel = new JPanel();
JLabel LoginICO = new JLabel();
JLabel jLabel2 = new JLabel();
JLabel jLabel3 = new JLabel();
JPasswordField LoginPassword = new JPasswordField();
JButton btnLogin = new JButton();
JButton btnCZ = new JButton();
JButton btnExit = new JButton();
JComboBox jCombLoginName = new JComboBox();
UserManager um = new UserManager();
private Connection con = ConnectionManager.getConnection();
private Statement stmt;
private ResultSet rs;
private int error = 0; // 错误计数,大于3次,系统自动退出
protected static String name;
protected String pwd;
Calendar objCalendar = Calendar.getInstance();
//用户登入日期
String logindate = "" + objCalendar.get(Calendar.YEAR) //年
+ "-" + (objCalendar.get(Calendar.MONTH) + 1) //月
+ "-" + objCalendar.get(Calendar.DATE) //日
+ " " + objCalendar.get(Calendar.HOUR) //时
+ ":" + objCalendar.get(Calendar.MINUTE) //分
+ ":" + objCalendar.get(Calendar.SECOND);//秒
public LoginDialog(Frame owner, String title, boolean modal) {
super(owner, title, modal);
try {
setDefaultCloseOperation(DISPOSE_ON_CLOSE);// 设置"X"无任何功能
jbInit();
// 开始时读入登入时间最晚的3名用户名
stmt = con.createStatement();
rs = stmt.executeQuery("select top 3 name from operator where deleteflag = '1' order by LoginTime DESC");
while(rs.next()){
jCombLoginName.addItem(rs.getString(1));
}
ConnectionManager.closeResultSet(rs);
ConnectionManager.closeStatement(stmt);
pack();
} catch (Exception exception) {
exception.printStackTrace();
}
}
public LoginDialog() {
this(new Frame(), "LoginDialog", false);
}
private void jbInit() throws Exception {
this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); // 设置"X"无任何功能
LoginPanel.setLayout(null);
LoginPanel.setBackground(MainFrame.maincolor);
LoginPanel.setBorder(BorderFactory.createLineBorder(Color.black));
jCombLoginName.setBounds(new Rectangle(216, 51, 106, 26));
jCombLoginName.setToolTipText("请选择或输入操作用户");
jCombLoginName.setEditable(true);
this.getContentPane().add(LoginPanel, java.awt.BorderLayout.CENTER);
LoginICO.setIcon(new ImageIcon(getClass().getResource("img/loginico.gif")));
LoginICO.setBounds(new Rectangle(30, 33, 80, 80));
jLabel2.setText("用户名:");
jLabel2.setBounds(new Rectangle(154, 56, 62, 15));
jLabel3.setText("密 码:");
jLabel3.setBounds(new Rectangle(155, 105, 62, 15));
LoginPassword.setForeground(Color.red);
LoginPassword.setBounds(new Rectangle(216, 98, 106, 25));
LoginPassword.addKeyListener(new LoginDialog_LoginPassword_keyAdapter(this));
btnLogin.setBackground(MainFrame.maincolor);
btnLogin.setBounds(new Rectangle(49, 163, 71, 25));
btnLogin.setBorder(null);
btnLogin.setHorizontalTextPosition(SwingConstants.CENTER);
btnLogin.setIcon(new ImageIcon(getClass().getResource("img/qdbtnico.gif")));
btnLogin.addActionListener(new LoginDialog_btnLogin_actionAdapter(this));
btnCZ.setBackground(MainFrame.maincolor);
btnCZ.setBounds(new Rectangle(154, 163, 71, 25));
btnCZ.setBorder(null);
btnCZ.setIcon(new ImageIcon(getClass().getResource("img/czbtnico.gif")));
btnCZ.addActionListener(new LoginDialog_btnCZ_actionAdapter(this));
btnExit.setBackground(MainFrame.maincolor);
btnExit.setBounds(new Rectangle(256, 163, 71, 25));
btnExit.setBorder(null);
btnExit.setIcon(new ImageIcon(getClass().getResource("img/qxbtnico.gif")));
btnExit.addActionListener(new LoginDialog_btnExit_actionAdapter(this));
LoginPanel.add(jLabel2);
LoginPanel.add(LoginPassword);
LoginPanel.add(jLabel3);
LoginPanel.add(LoginICO);
LoginPanel.add(btnExit);
LoginPanel.add(btnLogin);
LoginPanel.add(btnCZ);
LoginPanel.add(jCombLoginName);
LoginPassword.requestFocus(true);
}
public void btnExit_actionPerformed(ActionEvent e) {
System.exit(0);
}
public void btnLogin_actionPerformed(ActionEvent e) {
pwd = new String(LoginPassword.getPassword()); // 得到密码
name = jCombLoginName.getSelectedItem().toString();// 得到用户名
// 开始时读入用户名
if (name.length() == 0) {
JOptionPane.showMessageDialog(this, "请输入用户名!");
return;
}
try {
stmt = con.createStatement();
rs = stmt.executeQuery(
"SELECT name FROM operator where deleteflag = '1' and name=" +
"'" + name + "'");
//System.out.println(rs.getString(1));
if (! (rs.next())) {
JOptionPane.showMessageDialog(this, "无此用户!!");
return;
}
ConnectionManager.closeResultSet(rs);
ConnectionManager.closeStatement(stmt);
}
catch (SQLException ex) {
}
try {
boolean flag = um.canLoad(name, pwd);
if (flag) {
//JOptionPane.showMessageDialog(this,"登录成功!");
//----------------------更新当前用户登入时间 开始-------------------------
try {
stmt = con.createStatement();
String sql="update operator Set LoginTime='"+logindate+"' where name=" +
"'" + name + "'";
stmt.executeUpdate(sql);
//System.out.println(sql);
ConnectionManager.closeResultSet(rs);
ConnectionManager.closeStatement(stmt);
}
catch (SQLException ex) {
}
//----------------------更新当前用户登入时间 结束-------------------------
this.dispose(); // 进入主界面
}
else {
JOptionPane.showMessageDialog(this,
"对不起! 用户名与密码不匹配!\n请重新输入!\n还有" + (3 - error) +
"次重试机会");
error++;
if (error > 3) {
System.exit(1);
}
LoginPassword.setFocusable(true);
return;
}
}
catch (SQLException ex) {
}
}
public void btnCZ_actionPerformed(ActionEvent e) {
LoginPassword.setText("");
LoginPassword.requestFocus();
}
public void LoginPassword_keyPressed(KeyEvent e) {
int c =e.getKeyCode();//获得键盘按下是的键值
if (c==10) {//判断按下的是否是回车
pwd = new String(LoginPassword.getPassword()); // 得到密码
name = jCombLoginName.getSelectedItem().toString(); // 得到用户名
// 开始时读入用户名
if (name.length() == 0) {
JOptionPane.showMessageDialog(this, "请输入用户名!");
return;
}
try {
stmt = con.createStatement();
rs = stmt.executeQuery(
"SELECT name FROM operator where deleteflag = '1' and name=" +
"'" + name + "'");
//System.out.println(rs.getString(1));
if (! (rs.next())) {
JOptionPane.showMessageDialog(this, "无此用户!!");
return;
}
ConnectionManager.closeResultSet(rs);
ConnectionManager.closeStatement(stmt);
}
catch (Exception ex) {
//如输入的不是数字,程序异常处理
JOptionPane.showMessageDialog(this, "无此用户!");
return;
}
try {
boolean flag = um.canLoad(name,pwd);
if (flag) {
//JOptionPane.showMessageDialog(this,"登录成功!");
//----------------------更新当前用户登入时间 开始-------------------------
try {
stmt = con.createStatement();
String sql = "update operator Set LoginTime='" + logindate +
"' where name=" + "'" + name + "'";
stmt.executeUpdate(sql);
//System.out.println(sql);
ConnectionManager.closeResultSet(rs);
ConnectionManager.closeStatement(stmt);
}
catch (SQLException ex) {
}
//----------------------更新当前用户登入时间 结束-------------------------
this.dispose(); // 进入主界面
}
else {
JOptionPane.showMessageDialog(this,"对不起! 用户名与密码不匹配!\n请重新输入!\n还有"+(3-error)+"次重试机会");
error++;
if(error > 3){
System.exit(1);
}
LoginPassword.setFocusable(true);
return;
}
}
catch (SQLException ex) {
}
}
}
}
class LoginDialog_LoginPassword_keyAdapter
extends KeyAdapter {
private LoginDialog adaptee;
LoginDialog_LoginPassword_keyAdapter(LoginDialog adaptee) {
this.adaptee = adaptee;
}
public void keyPressed(KeyEvent e) {
adaptee.LoginPassword_keyPressed(e);
}
}
class LoginDialog_btnCZ_actionAdapter
implements ActionListener {
private LoginDialog adaptee;
LoginDialog_btnCZ_actionAdapter(LoginDialog adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.btnCZ_actionPerformed(e);
}
}
class LoginDialog_btnExit_actionAdapter
implements ActionListener {
private LoginDialog adaptee;
LoginDialog_btnExit_actionAdapter(LoginDialog adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.btnExit_actionPerformed(e);
}
}
class LoginDialog_btnLogin_actionAdapter
implements ActionListener {
private LoginDialog adaptee;
LoginDialog_btnLogin_actionAdapter(LoginDialog adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.btnLogin_actionPerformed(e);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -