图书管理系统代码  1 Libraryload …_java

图书管理系统代码  1 Libraryload …_失去焦点_02
// 新增了 焦点监听器 当用户名文本域失去焦点 时 进行提示!!!!
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.io.BufferedReader;
import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.IOException;

import javax.swing.ImageIcon;
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.SwingConstants;




public class Libraryload extends InitFrame
{
public Libraryload(){

super("图书管理系统—登录","Library.png",360,200);
this.setDefaultCloseOperation (JFrame.DISPOSE_ON_CLOSE);
//设置布局管理器
this.setLayout(new BorderLayout());
// 创建面板
centerPanel = new JPanel();
southPanel = new JPanel();
// 将面板添加到窗体
this.add(centerPanel,BorderLayout.CENTER);
this.add(southPanel,BorderLayout.SOUTH);
// 设置面板布局
centerPanel.setLayout(new GridBagLayout());
southPanel.setLayout(new FlowLayout());
// 实例化焦点监听器
addUserFocusListener ufl = new addUserFocusListener();
// 初始化标签
      label_user= new JLabel("请输入用户名:");
     
     
      label_result_f = new JLabel("该用户名正确!!!",new ImageIcon("label_result_t.png"),SwingConstants.RIGHT);
      label_result_f.setForeground(Color.red);//设置标签前景色
//       添加焦点事件 当失去焦点时   如果不可用给予提示
      label_result_f.setVisible(false);
     
      label_result_t = new JLabel("该用户名不存在!!",new ImageIcon("label_result_f.png"),SwingConstants.RIGHT);
      label_result_t.setForeground(Color.red);//设置标签前景色
//       添加焦点事件 当失去焦点时   如果不可用给予提示
      label_result_t.setVisible(false);
     
     
      label_password = new JLabel("请输入密码:");
   
// 初始化文本域
  field_user = new JTextField(20);
  field_user.addFocusListener(ufl);
  field_password=new JPasswordField(20);
  field_password.addFocusListener(ufl);
 
// 添加中间面板元素
      centerPanel.add(label_user,new GBC(0,1).setInsets(1));
      centerPanel.add(label_password,new GBC(0,2).setInsets(1));
 
      centerPanel.add(label_result_f,new GBC(1,1).setInsets(1));
      centerPanel.add(label_result_t,new GBC(1,1).setInsets(1));
     
     
      centerPanel.add(field_user,new GBC(1,1).setInsets(1));
      centerPanel.add(field_password,new GBC(1,2).setInsets(1));
     
     
     
//   设置下部边框
JButton button_ok = new JButton("确定",new ImageIcon("ok.png"));
button_ok.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
boolean has=false;
String getpassW =new String(field_password.getPassword());//获取密码
    String getUser = new String(field_user.getText());
    if(getpassW.equals("")||getUser.equals(""))
{
   
JLabel label_result = new JLabel("用户名和密码不可为空,请重新输入!!");
JOptionPane.showConfirmDialog(Libraryload.this, label_result,"图书管理系统-登录", JOptionPane.WARNING_MESSAGE,JOptionPane.OK_OPTION , new ImageIcon("result.png"));
}else {
File file_org= new File("E:\\图书管理系统\\password.dat");
    FileReader fr;
    try {
    fr = new FileReader(file_org);
   
    BufferedReader br = new BufferedReader(fr);
    do{
   
        password_org2 = br.readLine();
       
        if(password_org2!=null)
        {
       
        password_input = field_user.getText()+","+new String(field_password.getPassword());
               
        if(password_org2.equals(password_input))
        {
        has=true;        
        }
               
        }
       
    }while(password_org2!=null);
    fr.close();
        br.close();
   
    } catch (FileNotFoundException e3) 
    {
    e3.printStackTrace();
    }catch(IOException e1)
    {
            e1.printStackTrace();
    }
   
    if(has==true){
   
    try {
Thread.sleep(2000);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
   
   
    try {
Thread.sleep(1000);
} catch (InterruptedException e1)
{
e1.printStackTrace();
}
    Libraryload.this.setVisible(false);
    Libraryload.this.dispose();
   
    LibraryMainF lm = new LibraryMainF();
    lm.setVisible(true);
   
   
    }else
    {
    JLabel label_result = new JLabel("用户名或密码错误,请重新输入!!");
JOptionPane.showConfirmDialog(Libraryload.this, label_result,"图书管理系统-登录", JOptionPane.WARNING_MESSAGE,JOptionPane.OK_OPTION , new ImageIcon("result.png"));
    }
   
}
}
});
      JButton   button_cancel = new JButton("重置",new ImageIcon("cancel.png"));
button_cancel.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
field_user.setText("");
field_password.setText("");
   
}
}                               );
      southPanel.add(button_ok);
      southPanel.add(button_cancel);
}
//此焦点类的目的是判断当用户输完用户名时 是否与已有用户相同 并给予提示!
private class addUserFocusListener implements FocusListener
{
// 当用户名区获取焦点 则将给出的提示取消
public void focusGained(FocusEvent fe) 
{
if(fe.getSource().equals(field_user))
{
if(label_result_f.isVisible()==true)
{
label_result_f.setVisible(false);
}else if(label_result_t.isVisible()==true)
{
label_result_t.setVisible(false);
    }
// 由于has (重复)是全局变量,如果没有下面那句当第一次输入时重复的用户名时 ,如果可用再次使用将
// 不再提示可用的标签!!
xianshi=false;
}
}

public void focusLost(FocusEvent fe) 
{
 
if(fe.getSource().equals(field_user))
{
System.out.println("7");
// 读取原始密码
file_pass_org = new File("E:\\图书管理系统\\password.dat");
FileReader fr;
try {
fr = new FileReader(file_pass_org);
BufferedReader br = new BufferedReader(fr);
do{
password_org1= br.readLine();
if(password_org1!=null){
int index =password_org1.indexOf(',');//字符串中第一次出现逗号的索引
String get_user_name =password_org1.substring(0,index);//逗号前的内容即用户名
String get_input_user = field_user.getText();//获取新的用户名
if(get_user_name.equals(get_input_user))
{
label_result_f.setVisible(true);
xianshi=true;
}
}
}while(password_org1!=null);
fr.close();
br.close();
if(!xianshi)
{
label_result_t.setVisible(true);
}
} catch (FileNotFoundException e1) 
{
e1.printStackTrace();
}catch(IOException e2)
{
        e2.printStackTrace();
}
}
}
}
     
     

private JPanel centerPanel;
private JPanel southPanel;
private   JLabel label_user;
private JLabel label_password ;
private JLabel label_result_t;
private JLabel   label_result_f;

private JTextField field_user;
private JPasswordField field_password;
private String password_input;
private String password_org1;
private String password_org2;
    private File file_pass_org;
   
    private boolean xianshi=false;
    }