Java随机密码生成器

时间:2012-01-21 05:51:44

标签: java swing

我这是为了帮助我练习用户界面。出于某种原因,生成时密码不会显示在屏幕上!被压了。也没有程序错误。如您所见,我有一个JLabel密码。

代码:

package components; 
import java.io.*;
import java.util.Scanner;
import java.util.Random;
import javax.swing.*;
import javax.*;
import java.awt.*;
import java.awt.event.*;

public class PassGenButton extends JPanel implements ActionListener{

protected JButton generate;
protected JLabel passLabel;
public String password = null;

public PassGenButton()
{
    JButton generate = new JButton("Generate!");
    JLabel passLabel = new JLabel(password, JLabel.CENTER);
    passLabel.setFont(new Font("Serif", Font.PLAIN, 36));
    passLabel.setBorder(BorderFactory.createTitledBorder("Password"));
    setLayout(new BorderLayout());
    generate.addActionListener(this);
    add(generate, BorderLayout.SOUTH);
    add(passLabel, BorderLayout.CENTER);
}

public void actionPerformed(ActionEvent e)
{
    GetPassword();
}

private static void createAndShowGUI()
{
    JFrame frame = new JFrame("Password Generator");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    PassGenButton contentPane = new PassGenButton();

    frame.setContentPane(contentPane);
    frame.setSize(400, 200);
    frame.setLocation(600, 300);
    frame.setVisible(true);
}   

public static void main(String[] args)
{
    javax.swing.SwingUtilities.invokeLater(new Runnable(){
        public void run(){
            createAndShowGUI();     
        }
    });
}

public void GetPassword()
{
    password = null;
    String[] nouns = new String[2432];
    File file = new File("C:\\Temp\\nounlist.txt");
    String chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    char[] characters = chars.toCharArray();

    try
    {
        nouns = ReadTextFile(file);
    }
    catch (FileNotFoundException f)
    {
        f.getMessage();
        System.exit(1);
    }

    ShowPassword(nouns, characters);
}

public final String[] ReadTextFile(File aFile) throws FileNotFoundException
{
    String[] strings = new String[2432];
    int counter = 0;
    Scanner scanner = new Scanner(new FileReader(aFile));

    try
    {
        while (scanner.hasNextLine())
        {
            strings[counter] = scanner.nextLine();
            counter++;
        }
    }

    finally
    {
        scanner.close();
    }

    return strings;
}

public void ShowPassword(String[] nouns, char[] characters)
{
    String password;
    Random generator = new Random();
    int chosenNoun = 0;
    int chosenChar = 0;
    int int1 = 0;
    int int2 = 0;

    chosenNoun = generator.nextInt(2432);
    chosenChar = generator.nextInt(26);
    int1 = generator.nextInt(10);
    int2 = generator.nextInt(10);

    password = nouns[chosenNoun] + characters[chosenChar] + Integer.toString(int1) + Integer.toString(int2);
}
}

4 个答案:

答案 0 :(得分:3)

您正在隐藏passLabel变量(以及generate按钮)。您已经将它们声明为实例变量,您不需要在构造函数中重新声明它们,只需为它们赋值即可。所以而不是:

JButton generate = new JButton("Generate!");
JLabel passLabel = new JLabel(password, JLabel.CENTER);

你需要:

generate = new JButton("Generate!");
passLabel = new JLabel(password, JLabel.CENTER);

然后你需要确保按照bdares的回答设置文本。

答案 1 :(得分:2)

已编辑的代码段

public void GetPassword()
{
    password = null;
    String[] nouns = new String[2432];
    File file = new File("C:\\Temp\\nounlist.txt");
    String chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    char[] characters = chars.toCharArray();

    try
    {
        nouns = ReadTextFile(file);
    }
    catch (FileNotFoundException f)
    {
        f.printStackTrace();
        //System.exit(1);
    }

    ShowPassword(nouns, characters);
}

输出

java.io.FileNotFoundException: C:\Temp\nounlist.txt (The system cannot find the path specified)
       at java.io.FileInputStream.open(Native Method)
       at java.io.FileInputStream.<init>(FileInputStream.java:120)
       at java.io.FileReader.<init>(FileReader.java:55)
       at PassGenButton.ReadTextFile(PassGenButton.java:78)
       at PassGenButton.GetPassword(PassGenButton.java:63)
       at PassGenButton.actionPerformed(PassGenButton.java:29)
       ...

输出可能与您的位置完全相同,但是按指定更改方法并复制/粘贴输出。

答案 2 :(得分:2)

你的passLabel标签就在那里。它的价值永远不变。您的showPassword()方法可能应该显示给定的密码,但事实并非如此。它只是创建一个带有密码值的字符串,然后结束,甚至没有查看标签。

您需要阅读最后一行,如下所示:

passLabel.setText(password);

答案 3 :(得分:0)

你可以使用ostermillers密码生成器。他的网站和javaapplet也有例子 http://ostermiller.org/utils/src/RandPass.java.html