package com.neusoft.ums;
import java.awt.BorderLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.Writer;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class login extends JFrame implements ActionListener{
JButton jb1,jb2;
JPanel jp1,jp2,jp3,jp4,jp5,jp6;
JLabel jl1,jl2,jl3;
JTextField jtf;
JPasswordField jpf;
JCheckBox jcb1,jcb2;
private int LCD_width = Toolkit.getDefaultToolkit().getScreenSize().width;
private int LCD_height = Toolkit.getDefaultToolkit().getScreenSize().height;
private String oldName = null;
private String oldPassword = null;
private String autoLogin = "";
private int passwordinputtime = 0;
public login(){
jl1 = new JLabel(new ImageIcon("image/loginhead.png"));
jp1 = new JPanel();
jp2 = new JPanel();
jp3 = new JPanel();
jp4 = new JPanel();
jp5 = new JPanel();
jp6 = new JPanel();
jl2 = new JLabel("用户名 ");
jl3 = new JLabel("密 码 ");
jtf = new JTextField(10);
jpf = new JPasswordField(10);
jb1 = new JButton("登陆");
jb1.addActionListener(this);
jb2 = new JButton("注册");
jb2.addActionListener(this);
jcb1 = new JCheckBox("记住密码");
jcb2 = new JCheckBox("自动登陆");
jcb2.addActionListener(this);
jp1.add(jl2);
jp1.add(jtf);
jp2.add(jl3);
jp2.add(jpf);
jp3.add(jcb1);
jp3.add(jcb2);
jp4.add(jp1);
jp4.add(jp2);
jp4.add(jp3);
jp5.add(jb1);
jp5.add(jb2);
this.add(jl1,BorderLayout.NORTH);
this.add(jp4,BorderLayout.CENTER);
this.add(jp5,BorderLayout.SOUTH);
//设置窗体的标题、位置、大小、可见性及关闭符号
this.setTitle("用户登陆");
//设置窗口位置在屏幕中央
this.setLocation(LCD_width/2-135,LCD_height/2-120);
//设置窗口大小
this.setSize(270,240);
this.setVisible(true);
//用户不可调整面板大小
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//读取本地保存的登陆信息
loadMsg();
//自动登陆
autoLogin();
}
public static void main(String[] args){
login l = new login();
}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==jb1){
checkLogin();
}
if(jcb2.isSelected()){
jcb1.setSelected(true);//选择自动登陆,就必须保存密码
}
if(e.getSource()==jb2){
new userReg(null,"建立用户",true,10);
}
}
//自动登陆
private void autoLogin(){
if(autoLogin.equals("auto")){
checkLogin();
}
}
//读取本地保存的登陆信息
private void loadMsg(){
try {
FileInputStream fis = new FileInputStream("config.dat");
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader br = new BufferedReader(isr);
oldName = br.readLine();
oldPassword = br.readLine();
autoLogin = br.readLine();
jtf.setText(oldName);
jpf.setText(oldPassword);
if(autoLogin==null)autoLogin="";
br.close();
} catch (Exception e) {
e.printStackTrace();
}
}
//保存正确的登陆信息到本地文件
private void saveMsg(){
try {
FileOutputStream fos = new FileOutputStream("config.dat");
fos.write((jtf.getText()+"\r\n").getBytes());
fos.write((jpf.getText()+"\r\n").getBytes());
fos.write(autoLogin.getBytes());
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
//清空本地文件中保存的登陆信息
private void clearMsg(){
try {
FileOutputStream fos = new FileOutputStream("config.dat");
fos.write("\r\n".getBytes());
fos.write("\r\n".getBytes());
fos.write(autoLogin.getBytes());
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
//验证登陆
private void checkLogin(){
boolean isLogin;
int myRight;
loginModel lm = new loginModel();
isLogin = lm.checkLogin(jtf.getText(),jpf.getText());
if(isLogin)//验证成功
{
boolean isSelect_jcb1 = false;
boolean isSelect_jcb2 = false;
isSelect_jcb1 = jcb1.isSelected();//获取选项状态
isSelect_jcb2 = jcb2.isSelected();
if(isSelect_jcb2){
autoLogin = "auto";
}else {
autoLogin = "";
}
if(isSelect_jcb1){
saveMsg();
}else {
clearMsg();
}
myRight = lm.getRight();
// System.out.println("Login OK"+" right"+myRight);
new userWindow(jtf.getText(),myRight);
this.dispose();
}else
{
if(passwordinputtime>=2)
{
JOptionPane.showMessageDialog(this,"密码输入错误3次,程序即将退出。","警告",JOptionPane.WARNING_MESSAGE);
clearMsg();
System.exit(0);
}
JOptionPane.showMessageDialog(this,"密码错误!请重新输入...","警告",JOptionPane.WARNING_MESSAGE);
jtf.setText("");
jpf.setText("");
passwordinputtime++;
}
}
}