📄 logindialog.java
字号:
package mysiloer;
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import java.sql.Connection;
import java.awt.event.*;
import java.sql.Statement;
import java.sql.SQLException;
import java.sql.ResultSet;
/**
* <p>Title: MySiloer</p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2005</p>
* <p>Company: yskey</p>
* @author 杨涛
* @version 1.0
*/
public class LoginDialog extends JDialog {
private JPanel panel1 = new JPanel();
private BorderLayout borderLayout1 = new BorderLayout();
private JPanel contentPanel = new JPanel();
private JPanel controlPanel = new JPanel();
private GridLayout gridLayout1 = new GridLayout();
private JLabel jLabel1 = new JLabel();
private JLabel jLabel2 = new JLabel();
private JTextField userNameField = new JTextField();
private JPasswordField pwdFiled = new JPasswordField();
private JButton cancelButt = new JButton();
private JButton loginButt = new JButton();
private Border border1;
private GridLayout gridLayout2 = new GridLayout();
private Border border2;
private Connection con = null;
private boolean isLogin = false;
public LoginDialog(MainFrame frame, String title, boolean modal) {
super(frame, title, modal);
try {
this.con = frame.dataBase.connection;
jbInit();
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
try{
con.close();
System.exit(0);
}
catch(SQLException ex){
System.err.println(ex.toString());
}
}});
this.setResizable(false);
pack();
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = this.getSize();
if (frameSize.height > screenSize.height) {
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width) {
frameSize.width = screenSize.width;
}
this.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
}
catch(Exception ex) {
ex.printStackTrace();
}
}
public LoginDialog() {
this(null, "", false);
}
private void jbInit() throws Exception {
border1 = BorderFactory.createEmptyBorder(15,15,15,15);
border2 = BorderFactory.createEmptyBorder(0,30,10,30);
panel1.setLayout(borderLayout1);
contentPanel.setLayout(gridLayout1);
gridLayout1.setColumns(2);
gridLayout1.setRows(2);
gridLayout1.setVgap(5);
jLabel1.setText(" 用户名:");
jLabel2.setText(" 密 码:");
pwdFiled.setEchoChar('●');
cancelButt.setText("取 消");
cancelButt.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
cancelButt_actionPerformed(e);
}
});
loginButt.setText("登 录");
loginButt.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
loginButt_actionPerformed(e);
}
});
userNameField.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
loginButt_actionPerformed(e);
}
});
pwdFiled.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
loginButt_actionPerformed(e);
}
});
contentPanel.setBorder(border1);
controlPanel.setLayout(gridLayout2);
gridLayout2.setHgap(40);
gridLayout2.setVgap(40);
controlPanel.setBorder(border2);
userNameField.setText("sa");
getContentPane().add(panel1);
panel1.add(contentPanel, BorderLayout.CENTER);
panel1.add(controlPanel, BorderLayout.SOUTH);
contentPanel.add(jLabel1, null);
contentPanel.add(userNameField, null);
contentPanel.add(jLabel2, null);
contentPanel.add(pwdFiled, null);
controlPanel.add(loginButt, null);
controlPanel.add(cancelButt, null);
}
void cancelButt_actionPerformed(ActionEvent e) {
try{
con.close();
System.exit(0);
}
catch(SQLException ex){
System.err.println(ex.toString());
}
}
void loginButt_actionPerformed(ActionEvent e) {
try{
String password = new String(pwdFiled.getPassword()).trim();
Statement sta = con.createStatement();
ResultSet set = sta.executeQuery("select password from userTable where userName like '"+userNameField.getText().trim()+"'");
if(set != null){
set.next();
String pwd = set.getString(1);
pwd = pwd.trim();
if(pwd.equals(password)){
System.out.println(password);
setLogin(true);
this.setVisible(false);
this.dispose();
}
else{
JOptionPane.showMessageDialog(this, "您输入的密码不正确,请重新输入!!");
}
}
}
catch(SQLException ex){
System.err.println(ex.toString());
JOptionPane.showMessageDialog(this, "您输入的用户名不存在,请重新输入!!");
}
}
private void setLogin(boolean isLogin){
this.isLogin = isLogin;
}
public boolean getLogin(){
return this.isLogin;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -