gpt4 book ai didi

Java 随 secret 码生成器

转载 作者:行者123 更新时间:2023-11-29 07:16:40 24 4
gpt4 key购买 nike

我做这个是为了帮助我练习用户界面。出于某种原因,生成时密码不会显示在屏幕上!被按下。也没有程序错误。如您所见,我有一个 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);
}
}

最佳答案

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

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

你需要:

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

然后您需要确保按照 bda​​res 的回答设置其文本。

关于Java 随 secret 码生成器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8951016/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com