Codice:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class JPasswordGen {
char alfanum[] = {'1','2','3','4','5','6','7','8','9','0','q','w',' e','r','t','y','u','i','o','p','a','s','d','f','g' ,'h','j','k','l','z','x','c','v','b','n','m','Q',' W','E','R','T','Y','U','I','O','P','A','S','D','F' ,'G','H','J','K','L','Z','X','C','V','B','N','M'};
char num[] = {'1','2','3','4','5','6','7','8','9','0'};
char alfa[] = {'q','w','e','r','t','y','u','i','o','p','a','s',' d','f','g','h','j','k','l','z','x','c','v','b','n' ,'m','Q','W','E','R','T','Y','U','I','O','P','A',' S','D','F','G','H','J','K','L','Z','X','C','V','B' ,'N','M'};
char special[] = {'è','+','ò','à','ù',',','.','-',';',':','_','ç','°','§','é','*','^','?','=','(', ')','/','&','%','$','£','!','|','@','#','[',']'};
public static void main(String[] args) {
new JPasswordGen();
}
JPasswordGen(){
JFrame JFrm = new JFrame("JPasswordGen");
JPanel JPnl = new JPanel(new BorderLayout());
JButton JGen = new JButton("Genera Password!");
JButton JEsc = new JButton("Esci");
JLabel JLbl = new JLabel("Password Generata:");
final JTextField JPwd = new JTextField(15);
JPnl.setBorder(BorderFactory.createEmptyBorder(20, 20,20, 20));
JFrm.setContentPane(JPnl);
JFrm.setSize(350,250);
JPnl.add(JLbl,BorderLayout.NORTH);
JPnl.add(JPwd,BorderLayout.WEST);
JPnl.add(JEsc,BorderLayout.SOUTH);
JPnl.add(JGen,BorderLayout.EAST);
Toolkit Tk = Toolkit.getDefaultToolkit();
Dimension sS = Tk.getScreenSize();
int x = (sS.width - JFrm.getWidth()) / 2;
int y = (sS.height - JFrm.getHeight()) / 2;
JFrm.setLocation(x,y);
JFrm.setResizable(false);
JFrm.pack();
JFrm.setDefaultCloseOperation(WindowConstants.DISP OSE_ON_CLOSE);
final String Case = JOptionPane.showInputDialog(JFrm, "Tipo di password da generare:\n" + "1: Numerica\n" + "2: Letterale\n" + "3: Alfanumerica\n" + "4: Caratteri speciali", "3");
final String Lenght = JOptionPane.showInputDialog(JFrm, "Inserire la lunghezza della password da generare:","9");
JEsc.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
JGen.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
GenPwd(Case,Lenght,JPwd);
}
});
JFrm.setVisible(true);
GenPwd(Case,Lenght,JPwd);
}
public void GenPwd(String Tipo,String Lunghezza,JTextField JPwd){
if(Tipo.equals("1")){
String Str = new String();
for(int i = 0;i<Integer.parseInt(Lunghezza);i++) {
int c = (int) (Math.random() * num.length);
Str += num[c];
}
JPwd.setText(Str);
}else if (Tipo.equals("2")){
String Str = new String();
for(int i = 0;i<Integer.parseInt(Lunghezza);i++) {
int c = (int) (Math.random() * alfa.length);
Str += alfa[c];
}
JPwd.setText(Str);
}else if (Tipo.equals("3")){
String Str = new String();
for(int i = 0;i<Integer.parseInt(Lunghezza);i++) {
int c = (int) (Math.random() * alfanum.length);
Str += alfanum[c];
}
JPwd.setText(Str);
}else if (Tipo.equals("4")){
String Str = new String();
for(int i = 0;i<Integer.parseInt(Lunghezza);i++) {
int c = (int) (Math.random() * special.length);
Str += special[c];
}
JPwd.setText(Str);
}
}
}