📄 login.java
字号:
package view.loginframe;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Properties;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import common.centerwindow.CenterWindow;
import common.checkinputvalue.Check;
import dao.operator.OperatorDao;
import view.mainframe.MainFrame;
import vo.Operator;
import javax.swing.JComboBox;
import javax.swing.ImageIcon;
/*******************************************************************************
* 登录界面
*
* @author 刘少 陈高鹏 卿茂月 卓彬
* @version 1.0 2008-3-21
* @since 中信软件
******************************************************************************/
public class Login {
// 定义一个本类对象
private static Login instance = null;
// 声明主面板
private JFrame jFrame = null;
private JPanel jContentPane = null;
// 用户标签
private JLabel adminLabel = null;
private JLabel passwordLabel = null;
// 用户名输入框
private JTextField adminTextField = null;
private JButton okButton = null;
private JButton escButton = null;
// 用户密码框
private JPasswordField passwordField = null;
private JLabel jLabelAdminLevel = null;
private JComboBox jComboBoxgetAdminLevel = null;
private JLabel jLabelBackground = null;
static Properties prop = new Properties();
// 写日志文件的流对象
static PrintWriter pw = null;
// 写日志文件的类的对象
static LogWriter writer = null;
/**
* 本类方法
*
* @return instance
*/
public static Login getInstance() {
if (instance == null) {
instance = new Login();
}
instance.getPasswordField().setText("");
instance.jFrame.setVisible(true);
return instance;
}
private Login() {
// 渲染主界面
try {
UIManager
.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
} catch (Exception el) {
System.out.println("Look and Feel Exception");
System.exit(0);
}
this.getJFrame();
}
// 主面板初始化
public JFrame getJFrame() {
if (jFrame == null) {
jFrame = new JFrame("中信KTV管理平台");
jFrame.setSize(new java.awt.Dimension(710, 440));
jFrame.setResizable(false);
jFrame.setContentPane(getJContentPane());
new CenterWindow().centerWindow(jFrame);
jFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
e.getWindow().setVisible(false);
e.getWindow().dispose();
}
});
getPasswordField().setText("");
// 在frame里面设置系统小图标
jFrame.setIconImage(Toolkit.getDefaultToolkit().getImage(
"icon/mainframe/r.png"));
jFrame.setVisible(true);
}
return jFrame;
}
/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
// 背景图片面板
private JPanel getJContentPane() {
if (jContentPane == null) {
jLabelBackground = new JLabel();
jLabelBackground
.setBounds(new java.awt.Rectangle(-10, -3, 720, 430));
jLabelBackground.setIcon(new ImageIcon("icon/mainframe/login.jpg"));
jLabelBackground.setText("");
jLabelAdminLevel = new JLabel();
jLabelAdminLevel
.setBounds(new java.awt.Rectangle(380, 270, 67, 25));
jLabelAdminLevel.setText("管理员级别");
passwordLabel = new JLabel();
passwordLabel.setBounds(new java.awt.Rectangle(380, 300, 67, 25));
passwordLabel.setText("密码");
adminLabel = new JLabel();
adminLabel.setBounds(new java.awt.Rectangle(380, 240, 67, 25));
adminLabel.setText("用户编号");
jContentPane = new JPanel();
jContentPane.setBackground(java.awt.Color.white);
jContentPane.setLayout(null);
jContentPane.setFont(new java.awt.Font("Dialog",
java.awt.Font.PLAIN, 12));
jContentPane.add(adminLabel, null);
jContentPane.add(passwordLabel, null);
jContentPane.add(getAdminTextField(), null);
jContentPane.add(getOkButton(), null);
jContentPane.add(getEscButton(), null);
jContentPane.add(getPasswordField(), null);
jContentPane.add(jLabelAdminLevel, null);
jContentPane.add(getJComboBoxgetAdminLevel(), null);
jContentPane.add(jLabelBackground, null);
jContentPane.getName();
}
return jContentPane;
}
// 初始化用户密码框
private JTextField getAdminTextField() {
if (adminTextField == null) {
adminTextField = new JTextField();
adminTextField.setBounds(new java.awt.Rectangle(460, 240, 116, 24));
}
return adminTextField;
}
// 初始化"确定"按扭
private JButton getOkButton() {
if (okButton == null) {
okButton = new JButton();
okButton.setBounds(new java.awt.Rectangle(400, 340, 60, 25));
okButton.setText("确定");
okButton.addActionListener(new ActionListener() {
// 确定按纽事件
public void actionPerformed(ActionEvent e) {
if (checkInputValue()) {
try {
int temp1 = Integer.parseInt(adminTextField
.getText());
String temp2 = new String(passwordField
.getPassword());
String temp3 = getJComboBoxgetAdminLevel()
.getSelectedItem().toString().trim();
if (isExistOperator(temp1)) {
if (getAOperator(temp1, temp2, temp3)) {
jFrame.setVisible(false);
jFrame.dispose();
MainFrame
.getInstance(getJComboBoxgetAdminLevel()
.getSelectedItem()
.toString().trim());
// 日志记录
writer.log("用户" + "成功登录", LogWriter.INFO);
} else {
// 异常日志记录
writer.log("用户" + "挂起", LogWriter.INFO);
JOptionPane.showMessageDialog(null,
"密码输入错误,请您重新输入", "提示信息",
JOptionPane.YES_OPTION);
passwordField.setText("");
passwordField.requestFocus();
}
} else {
JOptionPane.showMessageDialog(null,
"用户编号错误,请您重新输入", "提示信息",
JOptionPane.YES_OPTION);
adminTextField.requestFocus();
}
} catch (Exception e1) {
System.out.println(e1.getMessage());
}
}
}
});
}
return okButton;
}
// 初始化取消按钮设置
private JButton getEscButton() {
if (escButton == null) {
escButton = new JButton();
escButton.setBounds(new java.awt.Rectangle(480, 340, 60, 25));
escButton.setText("取消");
escButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jFrame.setVisible(false);
jFrame.dispose();
}
});
}
return escButton;
}
// 用户编号,密码框输入数值的约束条件
private boolean checkInputValue() {
// 新建一个check类对象
Check check = new Check();
// 判断用户编号不能够为空
if (check.checkInputIsNull(adminTextField.getText())) {
JOptionPane.showMessageDialog(null, "用户编号不能为空,请您重新输入", "提示信息",
JOptionPane.YES_OPTION);
adminTextField.requestFocus();
return false;
}
// 判断用户编号只能够是数字
if (check.checkIsContainCharactor(adminTextField.getText())) {
JOptionPane.showMessageDialog(null, "用户编号只能为数字,请您重新输入", "提示信息",
JOptionPane.YES_OPTION);
adminTextField.requestFocus();
return false;
}
// 判断密码框是否为空
if (check.checkInputIsNull(passwordField.getText())) {
JOptionPane.showMessageDialog(null, "密码不能为空,请您重新输入", "提示信息",
JOptionPane.YES_OPTION);
passwordField.requestFocus();
return false;
}
return true;
}
// 通过参数获得dao层数据
public boolean isExistOperator(int temp) {
boolean flag = false;
flag = new OperatorDao().isExistOperator(temp);
return flag;
}
public boolean getAOperator(int temp, String temp2, String temp3) {
boolean flag = false;
flag = new OperatorDao().getAOperator(temp, temp2, temp3);
return flag;
}
/**
* This method initializes passwordField
*
* @return javax.swing.JPasswordField
*/
private JPasswordField getPasswordField() {
if (passwordField == null) {
passwordField = new JPasswordField();
passwordField.setBounds(new java.awt.Rectangle(460, 300, 115, 22));
passwordField.addKeyListener(new KeyListener() {
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == e.VK_ENTER) {
if (checkInputValue()) {
try {
int temp1 = Integer.parseInt(adminTextField
.getText());
String temp2 = passwordField.getText();
String temp3 = getJComboBoxgetAdminLevel()
.getSelectedItem().toString().trim();
if (isExistOperator(temp1)) {
if (getAOperator(temp1, temp2, temp3)) {
jFrame.setVisible(false);
jFrame.dispose();
MainFrame
.getInstance(getJComboBoxgetAdminLevel()
.getSelectedItem()
.toString().trim());
} else {
JOptionPane.showMessageDialog(null,
"密码输入错误,请您重新输入", "提示信息",
JOptionPane.YES_OPTION);
passwordField.setText("");
passwordField.requestFocus();
}
} else {
JOptionPane.showMessageDialog(null,
"用户编号错误,请您重新输入", "提示信息",
JOptionPane.YES_OPTION);
adminTextField.requestFocus();
}
} catch (Exception e1) {
System.out.println(e1.getMessage());
}
}
}
}
public void keyReleased(KeyEvent e) {
}
public void keyTyped(KeyEvent e) {
}
});
}
return passwordField;
}
/**
* 初始化jComboBoxgetAdminLevel,可以让用户选择不同类型的权限登录
*/
private JComboBox getJComboBoxgetAdminLevel() {
if (jComboBoxgetAdminLevel == null) {
jComboBoxgetAdminLevel = new JComboBox();
jComboBoxgetAdminLevel.setBounds(new java.awt.Rectangle(460, 270,
116, 24));
jComboBoxgetAdminLevel.addItem("前台收费员");
jComboBoxgetAdminLevel.addItem("超级管理员");
jComboBoxgetAdminLevel.setSelectedItem("前台收费员");
}
return jComboBoxgetAdminLevel;
}
/**
* @param zhuobin
* @return void
* @throws
*/
public static void main(String arg[]) {
pw = new PrintWriter(System.err, true);
writer = new LogWriter("登录窗体", LogWriter.INFO, pw);
Calendar can = Calendar.getInstance();
SimpleDateFormat fileformate = new SimpleDateFormat("yyyy'年'MM'月'dd'日'");
String filename = fileformate.format(can.getTime());
File file = new File("日志文件");
if (!file.exists()) {
file.mkdir();
}
// 把日志写到工程目录下面
try {
pw = new PrintWriter(new FileWriter("日志文件\\" + filename + "日志.log",
true), true);
} catch (IOException e2) {
e2.printStackTrace();
}
writer.setPrintWriter(pw);
SimpleDateFormat formatter = new SimpleDateFormat(
"yyyy'年'MM'月'dd'日'HH:mm:ss");
String currenttime = formatter.format(can.getTime());
writer.log(currenttime + "开始记录日志", LogWriter.INFO);
Login.getInstance();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -