-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPasswordGenerator.java
168 lines (114 loc) · 4.79 KB
/
PasswordGenerator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import java.awt.Font;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.util.Random;
public class PasswordGenerator extends JFrame {
private Toolkit toolkit = getToolkit();
private Dimension size = toolkit.getScreenSize();
public PasswordGenerator() {
new JFrame();
this.setTitle("Simple Password Generator v1.0");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(500, 200);
this.setLayout(null);
this.setResizable(false);
this.setVisible(true);
this.setLocation(size.width/2 - getWidth()/2, size.height/2 - getHeight()/2);
ContainerArea ca = new ContainerArea();
ca.setBounds(0, 0, 500, 200);
this.add(ca);
}
public class ContainerArea extends JPanel {
Font font = new Font("SansSerif", Font.BOLD, 20);
String lengthlist[] = {"8", "12", "14", "16", "18", "20"};
private JTextField generatedPassword = new JTextField();
private JButton generateButton = new JButton("Generate");
private JLabel passLengthLabel = new JLabel("Length");
private JCheckBox specialCharsCheckBox = new JCheckBox("Special Characters");
private JComboBox<String> lengthDropDown = new JComboBox<String>(lengthlist);
private int passwordLen = 8; // default is 8
private int SpecialChars = 0;
public ContainerArea() {
this.setLayout(null);
// specialChars.setBounds(55, 45, 250, 30);
specialCharsCheckBox.setBounds(170, 45, 250, 30);
specialCharsCheckBox.setFocusable(false);
specialCharsCheckBox.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
// TODO Auto-generated method stub
SpecialChars = (e.getStateChange() == 1) ? 1 : 0;
}
});
lengthDropDown.setBounds(55, 50, 50, 20);
passLengthLabel.setBounds(110, 50, 50, 20);
lengthDropDown.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String ss = "" + lengthDropDown.getItemAt(lengthDropDown.getSelectedIndex());
passwordLen = Integer.parseInt(ss);
}
});
generatedPassword.setBounds(55, 85, 250, 30);
generatedPassword.setEditable(false);
generatedPassword.setFont(font);
generateButton.setBounds(315, 85, 100, 30);
generateButton.setFocusable(false);
generateButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (e.getSource() == generateButton) {
generatePass();
}
}
});
this.add(specialCharsCheckBox);
this.add(lengthDropDown);
this.add(passLengthLabel);
this.add(generatedPassword);
this.add(generateButton);
}
public void generatePass() {
Random random = new Random();
int chartype;
String resultString = "";
for (int i = 0; i < passwordLen; i++) {
if (SpecialChars == 1) {
chartype = random.nextInt(4);
} else {
chartype = random.nextInt(3) + 1;
}
char resultChar = '0';
switch (chartype) {
// special chars
case 0 : resultChar = (char) (random.nextInt(6) + 33); break;
// lowercase letters
case 1 : resultChar = (char) (random.nextInt(26) + 97); break;
// uppsercase letters
case 2 : resultChar = (char) (random.nextInt(26) + 65); break;
// numbers
case 3 : resultChar = (char) (random.nextInt(10) + 48); break;
default : break;
}
resultString += Character.toString(resultChar);
}
generatedPassword.setText(resultString);
}
}
public static void main(String[] args) {
new PasswordGenerator();
}
}