smislogin.java
来自「用java2核心类库写的一个学生课程管理系统」· Java 代码 · 共 155 行
JAVA
155 行
/**
* @(#)smisLogin.java
*
*
* @author
* @version 1.00 2008/3/5
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class smisLogin
{
private int checkCount;
private String user = null;
private String password = null;
private JFrame f;
private JPanel content;
private JLabel userLabel;
private JLabel passwordLabel;
private JTextField userText;
private JPasswordField passwordText;
private JButton confirmBtn;
private JButton concleBtn;
private final String superUser = "superuser";
private final String superPassword = "super123";
private static final smisLogin login = new smisLogin();
public static smisLogin singleSmisLogin()
{
return login;
}
private smisLogin()
{
user = null;
password = null;
//初始化窗口
initMainFrame();
//为button注册监听器
ClickActionListener cal = new ClickActionListener();
confirmBtn.addActionListener(cal);
concleBtn.addActionListener(cal);
//为窗口注册监听器
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
e.getWindow().setVisible(false);
((Window)e.getComponent()).dispose();
System.exit(0);
}
});
}
private void initMainFrame()
{
checkCount = 0;
f = new JFrame("登录");
content = new JPanel();
userLabel = new JLabel("用户名: ",JLabel.LEFT);
passwordLabel = new JLabel("密码: ",JLabel.LEFT);
userText = new JTextField(10);
passwordText = new JPasswordField(10);
passwordText.setEchoChar('*');
confirmBtn = new JButton("确定");
concleBtn = new JButton("取消");
content.add(userLabel);
content.add(userText);
content.add(passwordLabel);
content.add(passwordText);
content.add(confirmBtn);
content.add(concleBtn);
content.setLayout(new FlowLayout());
f.add(content);
f.setSize(300,300);
f.setVisible(true);
}
//内部类,用来处理按钮的action event
class ClickActionListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
System.out.println("insert button is click3");
if (e.getActionCommand() == "确定")
{
System.out.println("insert button is 确定");
user = userText.getText();
password = passwordText.getText();
System.out.println("user = " + user + ", password = " + password);
if ( (user.equals(superUser)) && (password.equals(superPassword)) )
{
// super user login success
f.setVisible(false);
smisMainFrame.singleMainFrame().setMenuManageUser(user,password);
}
else
{
String sqlSelectUser = "select 用户名, 密码 from 用户信息";
SMISDB userDB = new SMISDB();
int confirmSuccess = 0;
confirmSuccess = userDB.login(sqlSelectUser,user, password);
System.out.println("-----------------confirmSuccess == " + confirmSuccess);
if ( confirmSuccess == 1)
{
f.setVisible(false);
smisMainFrame.singleMainFrame().setMenuManageUser(user,password);
}
else if ( confirmSuccess == -1)
{
checkCount++;
if ( checkCount <3)
{
JOptionPane.showMessageDialog(f,"用户名或密码有误,请重新输入!");
}
else
{
JOptionPane.showMessageDialog(f,"已输入3次,系统将退出!");
System.exit(0);
}
}
else
{
//SQLException
}
}
}
else if (e.getActionCommand() == "取消")
{
System.out.println("insert button is 取消");
System.exit(0);
}
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?