I made a random password generator in Java using a GUI. In the program, the user can choose the length, and whether to include lowercase letters, uppercase letters, symbols or numbers in the password. I believe that my code could be much better and I want to know how I can make it so.
Full code:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JSlider;
import javax.swing.JTextField;
import javax.swing.JCheckBox;
import java.awt.Color;
@SuppressWarnings("serial")
public class PasswordGenerator extends JFrame implements ActionListener {
JSlider lengthChooser;
JButton genButton;
JTextField passField;
JCheckBox lowercaseButton;
JCheckBox uppercaseButton;
JCheckBox symbolButton;
JCheckBox numberButton;
String lower = "abcdefghijklmnopqrstuvwxyz";
String upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
String symbols = "~`!@#$%^&*()-_=+[{]}|;:'\",<>./?//";
String numbers = "1234567890";
private String password = "";
public PasswordGenerator() {
setTitle("Password Generator");
getContentPane().setLayout(null);
setSize(241, 262);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
setResizable(false);
lengthChooser = new JSlider();
getContentPane().add(lengthChooser);
lengthChooser.setSnapToTicks(true);
lengthChooser.setMaximum(30);
lengthChooser.setToolTipText("Length of your password\r\n");
lengthChooser.setBounds(10, 26, 200, 38);
lengthChooser.setMajorTickSpacing(10);
lengthChooser.setMinorTickSpacing(1);
lengthChooser.setPaintTicks(true);
lengthChooser.setPaintLabels(true);
JLabel lblLength = new JLabel("Length:");
getContentPane().add(lblLength);
lblLength.setBounds(10, 11, 46, 14);
genButton = new JButton("Generate");
getContentPane().add(genButton);
genButton.addActionListener(this);
genButton.setBounds(71, 75, 89, 23);
passField = new JTextField();
getContentPane().add(passField);
passField.setBounds(10, 184, 215, 38);
JLabel lblPassword = new JLabel("Password:");
getContentPane().add(lblPassword);
lblPassword.setBounds(10, 159, 71, 14);
lowercaseButton = new JCheckBox("Lowercase");
getContentPane().add(lowercaseButton);
lowercaseButton.setBounds(10, 105, 97, 23);
uppercaseButton = new JCheckBox("Uppercase");
getContentPane().add(uppercaseButton);
uppercaseButton.setBounds(121, 105, 97, 23);
symbolButton = new JCheckBox("Symbols");
getContentPane().add(symbolButton);
symbolButton.setBounds(10, 129, 97, 23);
numberButton = new JCheckBox("Numbers");
getContentPane().add(numberButton);
numberButton.setBounds(121, 131, 97, 23);
}
String charString = "";
@Override
public void actionPerformed(ActionEvent event) {
Random random = new Random();
if (event.getSource() == genButton) {
boolean boxesArentChecked = !numberButton.isSelected() && !symbolButton.isSelected() && !uppercaseButton.isSelected()
&& !lowercaseButton.isSelected();
boolean numIsZero = lengthChooser.getValue() == 0;
if (boxesArentChecked && numIsZero) {
passField.setForeground(Color.RED);
passField.setText("Select a box and a size!");
} else if(numIsZero) {
passField.setForeground(Color.RED);
passField.setText("Your password length is 0!");
} else if(boxesArentChecked) {
passField.setForeground(Color.RED);
passField.setText("Select a box!");
} else {
passField.setForeground(Color.BLACK);
if (lowercaseButton.isSelected()) {
charString += lower;
}
if (uppercaseButton.isSelected()) {
charString += upper;
}
if (symbolButton.isSelected()) {
charString += symbols;
}
if (numberButton.isSelected()) {
charString += numbers;
}
char[] characters = charString.toCharArray();
password = "";
for (int i = 1; i <= lengthChooser.getValue(); i++) {
int choice = random.nextInt(characters.length);
password += characters[choice];
}
charString = "";
passField.setText(password);
}
}
}
}
Constructor:
public PasswordGenerator() {
setTitle("Password Generator");
getContentPane().setLayout(null);
setSize(241, 262);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
setResizable(false);
lengthChooser = new JSlider();
getContentPane().add(lengthChooser);
lengthChooser.setSnapToTicks(true);
lengthChooser.setMaximum(30);
lengthChooser.setToolTipText("Length of your password\r\n");
lengthChooser.setBounds(10, 26, 200, 38);
lengthChooser.setMajorTickSpacing(10);
lengthChooser.setMinorTickSpacing(1);
lengthChooser.setPaintTicks(true);
lengthChooser.setPaintLabels(true);
JLabel lblLength = new JLabel("Length:");
getContentPane().add(lblLength);
lblLength.setBounds(10, 11, 46, 14);
genButton = new JButton("Generate");
getContentPane().add(genButton);
genButton.addActionListener(this);
genButton.setBounds(71, 75, 89, 23);
passField = new JTextField();
getContentPane().add(passField);
passField.setBounds(10, 184, 215, 38);
JLabel lblPassword = new JLabel("Password:");
getContentPane().add(lblPassword);
lblPassword.setBounds(10, 159, 71, 14);
lowercaseButton = new JCheckBox("Lowercase");
getContentPane().add(lowercaseButton);
lowercaseButton.setBounds(10, 105, 97, 23);
uppercaseButton = new JCheckBox("Uppercase");
getContentPane().add(uppercaseButton);
uppercaseButton.setBounds(121, 105, 97, 23);
symbolButton = new JCheckBox("Symbols");
getContentPane().add(symbolButton);
symbolButton.setBounds(10, 129, 97, 23);
numberButton = new JCheckBox("Numbers");
getContentPane().add(numberButton);
numberButton.setBounds(121, 131, 97, 23);
}
ActionListener
method:
@Override
public void actionPerformed(ActionEvent event) {
Random random = new Random();
if (event.getSource() == genButton) {
boolean boxesArentChecked = !numberButton.isSelected() && !symbolButton.isSelected() && !uppercaseButton.isSelected()
&& !lowercaseButton.isSelected();
boolean numIsZero = lengthChooser.getValue() == 0;
if (boxesArentChecked && numIsZero) {
passField.setForeground(Color.RED);
passField.setText("Select a box and a size!");
} else if(numIsZero) {
passField.setForeground(Color.RED);
passField.setText("Your password length is 0!");
} else if(boxesArentChecked) {
passField.setForeground(Color.RED);
passField.setText("Select a box!");
} else {
passField.setForeground(Color.BLACK);
if (lowercaseButton.isSelected()) {
charString += lower;
}
if (uppercaseButton.isSelected()) {
charString += upper;
}
if (symbolButton.isSelected()) {
charString += symbols;
}
if (numberButton.isSelected()) {
charString += numbers;
}
char[] characters = charString.toCharArray();
password = "";
for (int i = 1; i <= lengthChooser.getValue(); i++) {
int choice = random.nextInt(characters.length);
password += characters[choice];
}
charString = "";
passField.setText(password);
}
}
}