mjau

PassGen

Nov 4th, 2011
12
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.69 KB | None | 0 0
  1. /**
  2.  Made by Classified; GUI by mjau.
  3.  */
  4. import java.awt.*;
  5. import java.awt.event.*;
  6. import javax.swing.*;
  7. public class GenerateOne extends JFrame implements ActionListener {  
  8.   public static int MIN_LENGTH;
  9.   protected static java.util.Random r = new java.util.Random();
  10.   protected static char[] goodChar = { 'a', 'b', 'c', 'd', 'e', 'f', 'g',
  11.       'h', 'j', 'k', 'm', 'n', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
  12.       'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K',
  13.       'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
  14.       '2', '3', '4', '5', '6', '7', '8', '9', };
  15.   JTextField amount = new JTextField(2);
  16.   JButton generate = new JButton("Generate");
  17.   JLabel length = new JLabel("Length: ");
  18.   String lengthString;
  19.   JTextField outField = new JTextField(15);
  20.   public GenerateOne() {
  21.       super("PassGen");
  22.       setSize(200, 200);
  23.       amount.addActionListener(this);
  24.       generate.addActionListener(this);
  25.           GridBagLayout gridbag = new GridBagLayout();
  26.           GridBagConstraints constraints = new GridBagConstraints();
  27.       setLayout(gridbag);
  28.       constraints.weighty = 3;
  29.       constraints.anchor = GridBagConstraints.EAST;
  30.       constraints.gridwidth = 1;
  31.       gridbag.setConstraints(length, constraints);
  32.       add(length);
  33.       constraints = new GridBagConstraints();
  34.       constraints.gridx = 1;
  35.       constraints.gridwidth = 2;
  36.       constraints.weighty = 3;
  37.       constraints.anchor = GridBagConstraints.CENTER;
  38.       gridbag.setConstraints(amount, constraints);
  39.       add(amount);
  40.       constraints = new GridBagConstraints();
  41.       constraints.gridx = 0;
  42.       constraints.gridy = 1;
  43.       constraints.gridwidth = 3;
  44.       constraints.weighty = 3;
  45.       gridbag.setConstraints(generate, constraints);
  46.       add(generate);
  47.       constraints.gridy = 2;
  48.       constraints.gridwidth = 3;
  49.       constraints.weighty = 3;
  50.       gridbag.setConstraints(outField, constraints);
  51.       add(outField);
  52.       outField.setEditable(false);
  53.      
  54.      
  55.   }
  56.   public static String getNext() {
  57.         StringBuffer sb = new StringBuffer();
  58.         for (int i = 0; i < MIN_LENGTH; i++) {
  59.           sb.append(goodChar[r.nextInt(goodChar.length)]);
  60.         }
  61.         return sb.toString();
  62.   }
  63.   public void actionPerformed(ActionEvent evt) {
  64.       Object source = evt.getSource();
  65.       if (source == generate) {
  66.           lengthString = amount.getText();
  67.           try {
  68.               MIN_LENGTH = Integer.parseInt(lengthString);
  69.           } catch (Exception e) {
  70.               JOptionPane.showMessageDialog(null, "Please enter a correct value!");
  71.               System.err.println(e);
  72.           }
  73.           outField.setText(GenerateOne.getNext());
  74.       }
  75.          
  76.          
  77. }
  78.  
  79.   public static void main(String[] args) {
  80.    
  81.     GenerateOne frame = new GenerateOne();
  82.     frame.setVisible(true);
  83.   }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment