前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >图书管理系统添加的新类 11AddUser…

图书管理系统添加的新类 11AddUser…

作者头像
明明如月学长
发布2021-08-27 11:02:45
6040
发布2021-08-27 11:02:45
举报
文章被收录于专栏:明明如月的技术专栏

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.BufferedWriter;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.FileWriter;

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 AddUserFrame extends InitFrame 

{

public AddUserFrame(){

super("图书管理系统—添加用户","Library.png",400,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_f.png"),SwingConstants.RIGHT);

label_result_f.setForeground(Color.red);//设置标签前景色

//   添加焦点事件 当失去焦点时   如果不可用给予提示

label_result_f.setVisible(false);

label_result_t = new JLabel("该用户名可用!!",new ImageIcon("label_result_t.png"),SwingConstants.RIGHT);

label_result_t.setForeground(Color.red);//设置标签前景色

//   添加焦点事件 当失去焦点时   如果不可用给予提示

label_result_t.setVisible(false);

label_password = new JLabel("请输入密码:");

label_new_password=new JLabel("请确认密码:");

// 初始化文本域

field_user = new JTextField(20);

field_user.addFocusListener(ufl);

field_password=new JPasswordField(20);

field_password_again=new JPasswordField(20);

// 添加中间面板元素

centerPanel.add(label_user,new GBC(0,1).setInsets(1));

centerPanel.add(label_password,new GBC(0,2).setInsets(1));

centerPanel.add(label_new_password,new GBC(0,3).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));

centerPanel.add(field_password_again,new GBC(1,3).setInsets(1));

//   设置下部边框

JButton button_ok = new JButton("确定",new ImageIcon("ok.png"));

button_ok.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e)

{

String getpassW =new String(field_password.getPassword());//获取密码

String getpassW_again =new String(field_password_again.getPassword());//获取确认密码

String getUser = new String(field_user.getText());

if(getpassW.equals("")||getpassW_again.equals("")||getUser.equals(""))

{

JLabel label_result = new JLabel("用户名和密码不可为空,请重新输入!!");

JOptionPane.showConfirmDialog(AddUserFrame.this, label_result,"图书管理系统-添加用户", JOptionPane.WARNING_MESSAGE,JOptionPane.OK_OPTION , new ImageIcon("result.png"));

}

else if(reiteration==true)//如果重复了 但是用户名域提示但是用户还是没有改 给予提示

{

JLabel label_result = new JLabel("该用户名重复,请重新输入!!");

JOptionPane.showConfirmDialog(AddUserFrame.this, label_result,"图书管理系统-添加用户", JOptionPane.WARNING_MESSAGE,JOptionPane.OK_OPTION , new ImageIcon("result.png"));

}

else{

//   如果两次输入的密码相同则将新用户写入 否则提醒用户重新输入

if(getpassW.equals(getpassW_again))

{

File f =new File("E:\\图书管理系统\\password.dat");

try {

FileWriter fr = new FileWriter(f,true);

BufferedWriter br = new BufferedWriter(fr);

br.write(getUser+","+getpassW);

br.flush();

fr.close();

br.close();

field_user.setText("");

field_password.setText("");

field_password_again.setText("");

JLabel label_result = new JLabel("新用户添加成功!!");

JOptionPane.showConfirmDialog(AddUserFrame.this, label_result,"图书管理系统-添加用户", JOptionPane.INFORMATION_MESSAGE,JOptionPane.OK_OPTION, new ImageIcon("succes.png"));

if(label_result_f.isVisible()==true)

{

label_result_f.setVisible(false);

}else if(label_result_t.isVisible()==true)

{

label_result_t.setVisible(false);

}

} catch (IOException e1) {

e1.printStackTrace();

}

}else 

{

JLabel label_result = new JLabel("两次输入的密码不一致,请重新输入!!");

JOptionPane.showConfirmDialog(AddUserFrame.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)

{

AddUserFrame.this.setVisible(false);//隐藏窗体

AddUserFrame.this.dispose();//释放窗体资源

}

}   );

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);

}

// 由于reiteration (重复)是全局变量,如果没有下面那句当第一次输入时重复的用户名时 ,如果可用再次使用将

// 不再提示可用的标签!!

reiteration=false;

}

}

public void focusLost(FocusEvent fe) 

{

if(fe.getSource().equals(field_user))

{

// 读取原始密码

file_pass_org = new File("E:\\图书管理系统\\password.dat");

FileReader fr;

try {

fr = new FileReader(file_pass_org);

BufferedReader br = new BufferedReader(fr);

do{

password_org= br.readLine();

if(password_org!=null){

int index =password_org.indexOf(',');//字符串中第一次出现逗号的索引

String get_user_name =password_org.substring(0,index);//逗号前的内容即用户名

String get_input_user = field_user.getText();//获取新的用户名

if(get_user_name.equals(get_input_user))

{

label_result_f.setVisible(true);

reiteration=true;

}

}

}while(password_org!=null);

fr.close();

br.close();

if(!reiteration)

{

System.out.println("here");

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_new_password;

private JLabel label_result_t;

private JLabel   label_result_f;

private JTextField field_user;

private JPasswordField field_password;

private JPasswordField field_password_again;

private String password_org;

private File file_pass_org;

private boolean reiteration=false;

}

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2015/06/20 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档