import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
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.JTabbedPane;
import javax.swing.JTextField;
public class EditpassW extends InitFrame
{
public EditpassW()
{
super("图书管理系统—修改密码","Library.png",480,300);
//设置布局
this.setLayout(new BorderLayout());
this.setResizable(false);
centerPanel1 = new JPanel();//放置暗文密码的面板
centerPanel2 = new JPanel();//放置明文密码的面板
southPanel = new JPanel();//放置确定和重置按钮
this.setDefaultCloseOperation (JFrame.DISPOSE_ON_CLOSE);
// 为各个面板设置布局管理器
centerPanel1.setLayout(new GridBagLayout());
centerPanel2.setLayout(new GridBagLayout());
southPanel.setLayout(new FlowLayout());
this.add(southPanel,BorderLayout.SOUTH);
tabbedPane =new JTabbedPane();
this.add(tabbedPane,BorderLayout.CENTER);
ImageIcon image1 = new ImageIcon("edit_pass1.png");
ImageIcon image2 = new ImageIcon("edit_pass2.png");
tabbedPane.addTab("暗文密码(H)", image1, centerPanel1);
// 为选项卡面板添加快捷键
int index = tabbedPane.indexOfTab("暗文密码(H)");
tabbedPane.setMnemonicAt(index, KeyEvent.VK_H);
// ALT+H即可选中
tabbedPane.addTab("明文密码(L)", image2, centerPanel2);
// 为选项卡面板添加快捷键
int index2 = tabbedPane.indexOfTab("明文密码(L)");
tabbedPane.setMnemonicAt(index2, KeyEvent.VK_L);
// ALT+L即可选中
tabbedPane.setSelectedIndex(0);
//设置中间边框
label_super_user = new JLabel(" 请输入原用户名:");
label_pass = new JLabel("请输入原密码:");
label_pass_new = new JLabel("请输入新密码:");
label_pass_again = new JLabel("请重新输入新密码:");
field_super_user =new JTextField(20);
field_pass = new JPasswordField(20);
field_pass_new = new JPasswordField(20);
field_pass_again = new JPasswordField(20);
centerPanel1.add(label_super_user,new GBC(0,0));
centerPanel1.add(field_super_user,new GBC(1,0));
centerPanel1.add(label_pass,new GBC(0,1));
centerPanel1.add(field_pass,new GBC(1,1));
centerPanel1.add(label_pass_new,new GBC(0,2));
centerPanel1.add(field_pass_new,new GBC(1,2));
centerPanel1.add(label_pass_again,new GBC(0,3));
centerPanel1.add(field_pass_again,new GBC(1,3));
label_super_user2 = new JLabel(" 请输入原用户名:");
label_pass2 = new JLabel("请输入原密码:");
label_pass_new2 = new JLabel("请输入新密码:");
label_pass_again2 = new JLabel("请重新输入新密码:");
field_super_user2 = new JTextField(20);
field_pass2 = new JTextField(20);
field_pass_new2 = new JTextField(20);
field_pass_again2 = new JTextField(20);
centerPanel2.add(label_super_user2,new GBC(0,0));
centerPanel2.add(field_super_user2,new GBC(1,0));
centerPanel2.add(label_pass2,new GBC(0,1));
centerPanel2.add(field_pass2,new GBC(1,1));
centerPanel2.add(label_pass_new2,new GBC(0,2));
centerPanel2.add(field_pass_new2,new GBC(1,2));
centerPanel2.add(label_pass_again2,new GBC(0,3));
centerPanel2.add(field_pass_again2,new GBC(1,3));
// 设置下部边框
JButton button_ok = new JButton("确定",new ImageIcon("ok.png"));
button_ok.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
// 1
//2 如果选择的是"暗文密码"执行如下操作
if(tabbedPane.getSelectedIndex()==0)
{
ArrayList password1=new ArrayList(10);
initArray(password1);
password_old =field_super_user.getText()+","+ new String(field_pass.getPassword());//存放输入的原密码
password_new1 =new String(field_pass_new.getPassword());//存放新密码
passWord_new2 = new String(field_pass_again.getPassword());//存放重新输入的新密码
user_new= field_super_user.getText();
// 遍历存放密码的数组
int index =password1.indexOf(password_old);
if(index>0)// 如果输入的原密码和原始密码相等
{
//将原先位置密码换成新密码
password1.set(index, new String(user_new+","+password_new1));
Object [] password_array1=password1.toArray();
if(password_new1.equals(passWord_new2))//如果两次输入的新密码相同
{
try {
FileWriter fr_new = new FileWriter(file_pass_org ,false);//不附加写入的数据(覆盖原密码)
for(int m=0;m
fr_new.write((String)password_array1[m]+"\r\n");//将新密码写入存放密码的文件
fr_new.flush();
}
fr_new.close();
System.out.println("新用户名是"+user_new);
System.out.println("新密码是"+password_new1);
} catch (IOException e1) {
JLabel label_result = new JLabel("对不起,密码写入失败!!");
JOptionPane.showConfirmDialog(tabbedPane, label_result,"图书管理系统-密码修改", JOptionPane.INFORMATION_MESSAGE,JOptionPane.OK_OPTION, new ImageIcon("null.png"));
e1.printStackTrace();
}
JLabel label_result = new JLabel("密码修改成功!!");
JOptionPane.showConfirmDialog(tabbedPane, label_result,"图书管理系统-密码修改", JOptionPane.INFORMATION_MESSAGE,JOptionPane.OK_OPTION, new ImageIcon("succes.png"));
}else
{
JLabel label_result = new JLabel("两次输入的新密码不一致,请重新输入!!");
JOptionPane.showConfirmDialog(tabbedPane, label_result,"图书管理系统-密码修改", JOptionPane.WARNING_MESSAGE,JOptionPane.OK_OPTION , new ImageIcon("result.png"));
}
}else
{
JLabel label_result = new JLabel("您输入的密码有误,请重新输入!!");
JOptionPane.showConfirmDialog(tabbedPane, label_result,"图书管理系统-密码修改", JOptionPane.INFORMATION_MESSAGE,JOptionPane.OK_OPTION, new ImageIcon("null.png"));
}
}
//3 如果选择的是"明文密码"执行如下操作
if(tabbedPane.getSelectedIndex()==1)
{
ArrayList password2=new ArrayList(10);
initArray(password2);
password_old = field_super_user2.getText()+","+field_pass2.getText();//存放输入的原密码
password_new1 =field_pass_new2.getText();//存放新密码
passWord_new2 = field_pass_again2.getText();//存放重新输入的新密码
user_new =field_super_user2.getText();
int index4 =password2.indexOf(password_old);
if(index4>0)// 如果输入的原密码和原始密码相等
{
//将原先位置密码换成新密码
password2.set(index4, new String(user_new+","+password_new1));
System.out.println("11"+password2);
Object [] password_array2 =password2.toArray();
if(password_new1.equals(passWord_new2))//如果两次输入的新密码相同
{
try {
FileWriter fr_new2 = new FileWriter(file_pass_org ,false);//不附加写入的数据(覆盖原密码)
for(int m=0;m
fr_new2.write((String)password_array2[m]+"\r\n");//将新密码写入存放密码的文件
fr_new2.flush();
}
fr_new2.close();
System.out.println("新用户名是"+user_new);
System.out.println("新密码是"+password_new1);
} catch (IOException e1) {
JLabel label_result = new JLabel("对不起,密码写入失败!!");
JOptionPane.showConfirmDialog(tabbedPane, label_result,"图书管理系统-密码修改", JOptionPane.INFORMATION_MESSAGE,JOptionPane.OK_OPTION, new ImageIcon("null.png"));
e1.printStackTrace();
}
JLabel label_result = new JLabel("密码修改成功!!");
JOptionPane.showConfirmDialog(tabbedPane, label_result,"图书管理系统-密码修改", JOptionPane.INFORMATION_MESSAGE,JOptionPane.OK_OPTION, new ImageIcon("succes.png"));
}else
{
JLabel label_result = new JLabel("两次输入的新密码不一致,请重新输入!!");
JOptionPane.showConfirmDialog(tabbedPane, label_result,"图书管理系统-密码修改", JOptionPane.WARNING_MESSAGE,JOptionPane.OK_OPTION , new ImageIcon("result.png"));
}
}else
{
JLabel label_result = new JLabel("您输入的密码有误,请重新输入!!");
JOptionPane.showConfirmDialog(tabbedPane, label_result,"图书管理系统-密码修改", JOptionPane.INFORMATION_MESSAGE,JOptionPane.OK_OPTION, new ImageIcon("null.png"));
}
}
}
});
JButton button_cancel = new JButton("取消",new ImageIcon("cancel.png"));
button_cancel.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
EditpassW.this.setVisible(false);//隐藏窗体
EditpassW.this.dispose();//释放窗体资源
}
});
southPanel.add(button_ok);
southPanel.add(button_cancel);
}
public ArrayList initArray(ArrayList password){
// 读取原始密码
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();
System.out.println(password_org);
if(password_org!=null){
// 初始化密码数组
password.add(password_org);
i++;
System.out.println("i="+i);
}
}while(password_org!=null);
password.trimToSize();
br.close();
} catch (FileNotFoundException e1)
{
e1.printStackTrace();
}catch(IOException e2)
{
e2.printStackTrace();
}
return password;
}
private JPanel centerPanel1 ;
private JPanel centerPanel2 ;
private JPanel southPanel ;
private JTabbedPane tabbedPane;
JLabel label_super_user;
JLabel label_pass ;
JLabel label_pass_new ;
JLabel label_pass_again ;
private JTextField field_super_user;
private JPasswordField field_pass ;
private JPasswordField field_pass_new;
private JPasswordField field_pass_again;
private JLabel label_super_user2;
private JLabel label_pass2 ;
private JLabel label_pass_new2;
private JLabel label_pass_again2 ;
private JTextField field_super_user2;
private JTextField field_pass2 ;
private JTextField field_pass_new2 ;
private JTextField field_pass_again2;
private String password_org;
private String password_old;
private String password_new1;
private String passWord_new2;
private String user_new;
private File file_pass_org ;//存放原始密码的文件路径
int i=0;
}