一个密码生成器,为了应付各种网站的注册问题。
生成的密码位数默认为12位,字母、数字、符号
废话不多说,直接上图,上源码
打开密码记录可以看到保存过的账号和密码,文件保存在相对路径下。
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.util.Random;
public class Frame extends JFrame implements ActionListener {
JFrame jf = new JFrame("密码生成器");
JLabel jl1 = new JLabel("密码:");
JTextField t1 = new JTextField(20);
JButton jb1 = new JButton("保存");
JButton jb2 = new JButton("生成");
JButton jb3 = new JButton("打开密码记录");
JPanel jp1 = new JPanel();
JPanel jp2 = new JPanel();
public void init(){
jf.setLayout(new GridLayout(2,1));
jf.setVisible(true);
jf.setSize(350,150);
jf.setLocationRelativeTo(this);
jf.setResizable(false);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jp1.add(jl1);
jp1.add(t1);
jp2.add(jb1);
jp2.add(jb2);
jp2.add(jb3);
jf.add(jp1);
jf.add(jp2);
jb1.addActionListener(this);
jb2.addActionListener(this);
jb3.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
String text = e.getActionCommand();
if(text.equals("保存")){
if(t1.getText().equals("未生成符合要求的密码,请重新生成")){
JOptionPane.showMessageDialog(jf,"请重新生成符合要求的密码!");
}else if(t1.getText().equals("")){
JOptionPane.showMessageDialog(jf,"密码不能为空,请生成密码!");
} else {
Save save = new Save();
save.init(t1.getText());
}
}
if(text.equals("生成")){
Password();
}
if(text.equals("打开密码记录")){
File file = new File("Password.txt");
try {
Desktop.getDesktop().open(file);
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
public void Password(){
int length = 12;
String result = null;
result = makepwd(length);
if (result.matches(".*[a-z]{1,}.*") && result.matches(".*[A-Z]{1,}.*") && result.matches(".*\\d{1,}.*") && result.matches(".*[~!@#$%^&*\\.?]{1,}.*")) {
t1.setText(result);
}else{
t1.setText("未生成符合要求的密码,请重新生成");
}
}
public static String makepwd(int len){
char charr[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890~!@#$%^&*.?".toCharArray();
StringBuilder stringBuilder = new StringBuilder();
Random r = new Random();
for(int i=0;i<len;i++){
stringBuilder.append(charr[r.nextInt(charr.length)]);
}
return stringBuilder.toString();
}
public static void main(String[] args) {
Frame frame = new Frame();
frame.init();
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
public class Save extends JFrame implements ActionListener {
JFrame f = new JFrame("保存密码");
JLabel jl1 = new JLabel("网站名称:");
JLabel jl2 = new JLabel("账 号:");
JLabel jl3 = new JLabel("密 码:");
JTextField jt1 = new JTextField(16);
JTextField jt2 = new JTextField(16);
JTextField jt3= new JTextField(16);
JButton jb1 = new JButton("确定");
JButton jb2 = new JButton("重置");
JPanel jp1 = new JPanel();
JPanel jp2 = new JPanel();
JPanel jp3 = new JPanel();
JPanel jp4 = new JPanel();
public void init(String pwd){
if(pwd!=null){
jt3.setText(pwd);
}
if(pwd==null){
jt3.setText("");
}
f.setSize(300,200);
f.setLayout(new GridLayout(4,2));
f.setVisible(true);
f.setResizable(false);
f.setLocationRelativeTo(null);
jp1.add(jl1);
jp1.add(jt1);
jp2.add(jl2);
jp2.add(jt2);
jp3.add(jl3);
jp3.add(jt3);
jp4.add(jb1);
jp4.add(jb2);
f.add(jp1);
f.add(jp2);
f.add(jp3);
f.add(jp4);
jb1.addActionListener(this);
jb2.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
String cmd = e.getActionCommand();
if(cmd.equals("确定")){
String webname = jt1.getText();
String username = jt2.getText();
String password = jt3.getText();
if((webname.equals("")&&username.equals(""))||(webname.equals("")||username.equals(""))){
JOptionPane.showMessageDialog(f,"请输入网站名称和账号!");
}else {
if(File(webname,username,password)){
JOptionPane.showMessageDialog(f,"保存成功!");
f.dispose();
}else {
JOptionPane.showMessageDialog(f,"保存失败!");
f.dispose();
}
}
}
if(cmd.equals("重置")){
jt1.setText("");
jt2.setText("");
}
}
public boolean File(String webname,String username,String password){
boolean flag = false;
OutputStreamWriter out = null;
if(flag!=true){
try{
out = new OutputStreamWriter(new FileOutputStream("Password.txt",true),"utf-8");
out.append("网站名称:"+webname);
out.append("\r\n");
out.append("账号:"+username);
out.append("\r\n");
out.append("密码:"+password);
out.append("\r\n");
out.append("\r\n");
out.flush();
flag = true;
}catch (Exception ex){
ex.printStackTrace();
}finally {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return flag;
}
}