Need to be done in Java using swing and awt. This program needs to create a GUI
ID: 3857950 • Letter: N
Question
Need to be done in Java using swing and awt. This program needs to create a GUI that generates random passwords for the user. The program should consist of the following swing components:
1 JButton to create a password,
4 JRadio buttons to allow the user to select the size of the password in characters (8 characters, 16, 24, and 32 characters.)
3 JCheckBoxes to allow the user to select the level of complexity for the password ("Use Caps", "Use Special Characters", "Use Numbers")
1 JTextField to display the created password.
When the Create Password button is clicked by the user, the program should generate a password of random characters with a length of equal to the value chosen by the radio buttons. The password should consist only of lower case characters(a-z) unless the user has checked the various checkboxes to make the password more complex by include capital letters, numbers, or special characters($!@#%&).
Finally if the password has been generated and the user attempts to close the window, prevent the window from closing and display a dialog box to remind the user to securely record their new password somewhere.
Explanation / Answer
package chegg;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.Random;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JRadioButton;
import javax.swing.JCheckBox;
import javax.swing.JTextField;
public class Sample {
private JFrame frame;
private JTextField textField;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Sample window = new Sample();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Sample() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JLabel lblSelectSize = new JLabel("Select Size");
lblSelectSize.setBounds(23, 34, 80, 23);
frame.getContentPane().add(lblSelectSize);
JRadioButton rdbtnNewRadioButton = new JRadioButton("8");
rdbtnNewRadioButton.setBounds(109, 34, 36, 23);
frame.getContentPane().add(rdbtnNewRadioButton);
JRadioButton radioButton = new JRadioButton("16");
radioButton.setBounds(148, 34, 44, 23);
frame.getContentPane().add(radioButton);
JRadioButton radioButton_1 = new JRadioButton("24");
radioButton_1.setBounds(192, 34, 49, 23);
frame.getContentPane().add(radioButton_1);
JRadioButton radioButton_2 = new JRadioButton("32");
radioButton_2.setBounds(244, 34, 109, 23);
frame.getContentPane().add(radioButton_2);
JLabel lblLevelOfComplexity = new JLabel("Level of Complexity");
lblLevelOfComplexity.setBounds(23, 80, 122, 14);
frame.getContentPane().add(lblLevelOfComplexity);
JCheckBox chckbxUppercase = new JCheckBox("Use Case");
chckbxUppercase.setBounds(148, 76, 95, 23);
frame.getContentPane().add(chckbxUppercase);
JCheckBox chckbxUseSpecialCharacters = new JCheckBox("Use Special Characters");
chckbxUseSpecialCharacters.setBounds(256, 76, 172, 23);
frame.getContentPane().add(chckbxUseSpecialCharacters);
JCheckBox chckbxUseNumbers = new JCheckBox("Use Numbers");
chckbxUseNumbers.setBounds(148, 109, 97, 23);
frame.getContentPane().add(chckbxUseNumbers);
textField = new JTextField();
textField.setBounds(71, 151, 249, 20);
frame.getContentPane().add(textField);
textField.setColumns(10);
JButton btnNewButton = new JButton("Create Password");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
int size = 0;
if(rdbtnNewRadioButton.isSelected())
size = 8;
else if(radioButton.isSelected())
size = 16;
else if(radioButton_1.isSelected())
size = 24;
else if(radioButton_2.isSelected())
size = 32;
String small_case = "abcdefghijklmnopqrstuvwxyz";
String upper_case = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
String numbers = "0123456789";
String symbols = "$!@#%&";
StringBuilder sb = new StringBuilder(64);
sb.append(small_case.toCharArray());
if(chckbxUppercase.isSelected())
sb.append(upper_case.toCharArray());
if(chckbxUseSpecialCharacters.isSelected())
sb.append(symbols.toCharArray());
if(chckbxUseNumbers.isSelected())
sb.append(numbers.toCharArray());
char result[] = sb.toString().toCharArray();
String password = "";
for(int i = 0; i < size;i++)
{
int rnd = new Random().nextInt(result.length);
password = password + result[rnd];
}
textField.setText(password);
}
});
btnNewButton.setBounds(127, 195, 136, 23);
frame.getContentPane().add(btnNewButton);
frame.addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent windowEvent) {
if (JOptionPane.showConfirmDialog(frame,
"Please save your password", "Really Closing?",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE) == JOptionPane.YES_OPTION){
System.exit(0);
}
}
});
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.