import javax.swing.*; import java.awt.*; import java.awt.datatransfer.Clipboard; import java.awt.datatransfer.StringSelection; import java.awt.datatransfer.Transferable; import java.util.Random; import java.io.FileWriter; import java.io.IOException; class main1 { public static void clipboard(String text) { Clipboard clipboard1 = Toolkit.getDefaultToolkit().getSystemClipboard(); Transferable txt = new StringSelection(text); clipboard1.setContents(txt, null); } } public class Main { public static void main(String[] args) { Random random = new Random();//生成随机数 JOptionPane.showMessageDialog(null, new JLabel("<html><h2><font color='blue',size=10>你好," + "欢迎来到密码生成器" + "<br>" + "</font><font " + "color='pink',size=3> " + "我是充满抛瓦制作的哦~</font></h2></html>")); String st0 = JOptionPane.showInputDialog(null, "输入密码名称"); String[] options = {"生成低强度密码", "生成中等强度密码", "生成高强度密码", "生成超高强度密码", "生成密码提示"}; Object ob1 = JOptionPane.showInputDialog(null, "请选择你下面需要的步骤:", "欢迎来到充满抛瓦的密码生成器", JOptionPane.QUESTION_MESSAGE, null, options, options[0]); if (ob1 != "生成密码提示") { String num = JOptionPane.showInputDialog(null, "请输入你需要几位数的密码:"); int num1 = Integer.parseInt(num); char[] chars = new char[num1]; String[] option2 = {"小写字母+大写字母", "小写字母+数字", "大写字母+数字"}; String[] option5 = {"纯数字", "纯小写字母", "纯大写字母"}; if (ob1 == "生成高强度密码") { for (int i = 0; i < num1; i++) { int i1 = random.nextInt(48, 122); if (i1 >= 48 && i1 <= 57 || i1 >= 65 && i1 <= 90 || i1 >= 97 && i1 <= 122) chars[i] = (char) i1; } } else if (ob1 == "生成超高强度密码") { for (int i = 0; i < num1; i++) { int i1 = random.nextInt(33, 126); chars[i] = (char) i1; } } else if (ob1 == "生成低强度密码") { int ob3 = JOptionPane.showOptionDialog(null, "请选择你的选项:", "提示", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, option5, option5[0]); if (ob3 == 0) { for (int i = 0; i < num1; i++) { int i1 = random.nextInt(48, 57); chars[i] = (char) i1; } } else if (ob3 == 1) { for (int i = 0; i < num1; i++) { int i1 = random.nextInt(97, 122); chars[i] = (char) i1; } } else if (ob3 == 2) { for (int i = 0; i < num1; i++) { int i1 = random.nextInt(65, 90); chars[i] = (char) i1; } } } else if (ob1 == "生成中等强度密码") { int ob6 = JOptionPane.showOptionDialog(null, "请选择你的选项:", "提示", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, option2, option2[0]); for (int i = 0; i < num1; i++) { int i1 = random.nextInt(48, 122); if (ob6 == 0) { if (i1 >= 65 && i1 <= 90 || i1 >= 97 && i1 <= 122) chars[i] = (char) i1; } else if (ob6 == 1) { if (i1 >= 48 && i1 <= 57 || i1 >= 97 && i1 <= 122) chars[i] = (char) i1; } else if (ob6 == 2) { if (i1 >= 65 && i1 <= 90 || i1 >= 97 && i1 <= 122) chars[i] = (char) i1; } } } String st2 = String.valueOf(chars); JOptionPane.showMessageDialog(null, st0 + "的密码是:" + st2); 按钮选项加入数组 String[] option3 = {"复制到剪贴板", "保存为"+st0+".txt"}; int ob2 = JOptionPane.showOptionDialog(null, new JLabel("<html><h2" + "><font " + "color='blue'," + "size=5" + ">请选择你的下个步骤</font" + "></h2></html>"), "询问", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, option3, option3[0]); if (ob2 == 0) { main1.clipboard(st0 + "的密码是:" + st2); //生成剪贴板输入流 JOptionPane.showMessageDialog(null, "已保存到剪贴板"); } if (ob2 == 1) { fileWriterMethod("C:\\Users" + "\\林钦声\\Desktop" + "\\弹出窗口\\" + st0 + ".txt", st0 + "的密码是:" + st2); JOptionPane.showMessageDialog(null, "已保存到" + st0 + ".txt文件中"); } } else if (ob1 == "生成密码提示") { JOptionPane.showMessageDialog(null, """ 低强度密码:由纯数字或纯小写字母或纯大写字母组成; 中等强度密码:由数字、小写字母、大写字母中的其中两种组成; 高强度密码:由数字、小写字母、大写字母共同组成; 超高强度密码:由数字、小写字母、大写字母、符号共同组成 """); } } public static void fileWriterMethod(String filepath, String content) { try (FileWriter fileWriter = new FileWriter(filepath)) { fileWriter.append(content); } catch (IOException e) { throw new RuntimeException(e); } } }