Myth0147

Swing - CreateUser

Jul 8th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.70 KB | None | 0 0
  1. import java.awt.event.*;
  2. import javax.swing.*;
  3. import java.awt.*;
  4. import java.io.*;
  5.  
  6. class CreateUser implements ActionListener
  7. {  
  8.     JFrame fr;  //Frame
  9.     JButton b1;  //Create Button
  10.     JLabel lb1, lb2, lb3;   //Username and password
  11.     JTextField tf;  //Username and password input fields
  12.     JPasswordField pf;
  13.     JPanel p1;
  14.    
  15.     CreateUser()
  16.     {
  17.         //Setting the frame
  18.         fr=new JFrame();
  19.         fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  20.         fr.setLayout(null);
  21.         fr.setSize(400,400);
  22.        
  23.         //setting panel
  24.         p1=new JPanel();
  25.         p1.setBounds(0,0,400,400);
  26.         p1.setLayout(null);
  27.        
  28.         //setting Username Label
  29.         JLabel lb1=new JLabel("Username: ");
  30.         lb1.setBounds(50,50,70,30);
  31.         p1.add(lb1);
  32.        
  33.         //setting Username Text Field
  34.         tf= new JTextField();
  35.         tf.setBounds(150,50,150,30);
  36.         p1.add(tf);
  37.    
  38.        
  39.         //setting Password Label
  40.         JLabel lb2=new JLabel("Password: ");
  41.         lb2.setBounds(50,100,70,30);
  42.         p1.add(lb2);
  43.        
  44.         //setting Password Text Field
  45.         pf = new JPasswordField();
  46.         pf.setBounds(150,100,150,30);
  47.         p1.add(pf);
  48.        
  49.         //setting Button
  50.         b1=new JButton("Create");
  51.         b1.setBounds(100,200,100,40);  
  52.         p1.add(b1);
  53.        
  54.         fr.add(p1);
  55.         fr.setVisible(true);   
  56.         b1.addActionListener(this);
  57.         tf.addActionListener(this);
  58.         pf.addActionListener(this);
  59.  
  60.     }
  61.    
  62.     public static void main(String[] s)
  63.     {
  64.         SwingUtilities.invokeLater(() -> new CreateUser());
  65.     }
  66.  
  67.     public void actionPerformed(ActionEvent e)
  68.     {
  69.         if(e.getSource()==b1)
  70.         {  
  71.             String uid = tf.getText();
  72.             String pass = String.valueOf(pf.getPassword());
  73.            
  74.        
  75.             //stores current file path to dir
  76.             String dir = System.getProperty("user.dir");
  77.            
  78.             //Creating a new Users folder if doesn't exists
  79.            
  80.             File file = new File(dir=dir+"\\users");
  81.             if (!file.exists())
  82.             {
  83.                 if (file.mkdir())
  84.                 {
  85.                     System.out.println("Directory is created!");
  86.                 }
  87.                 else
  88.                 {
  89.                     System.out.println("Failed to create directory!");
  90.                 }
  91.             }
  92.             String dir1 = dir+"\\users";
  93.            
  94.             //Creating a folder named with username inside Users folder
  95.            
  96.             file = new File(dir=dir+"\\"+uid);
  97.             if(file.exists())
  98.             {
  99.                 JOptionPane.showMessageDialog(null, "User Already Exists!! \nPlease choose different Username!");
  100.             }
  101.             else
  102.             //if (!file1.exists())
  103.             {
  104.                 if (file.mkdir())
  105.                 {
  106.                     System.out.println("Directory is created!");
  107.                 }
  108.                 else
  109.                 {
  110.                     System.out.println("Failed to create directory!");
  111.                 }
  112.             }
  113.            
  114.             //Storing Password.txt inside users/username folder
  115.             try
  116.             {
  117.                 FileOutputStream fout=new FileOutputStream(dir+"\\password.txt");
  118.                 byte b[]=pass.getBytes();
  119.                 fout.write(b);
  120.             }
  121.             catch(Exception ee)
  122.             {
  123.                 ee.printStackTrace();
  124.             }
  125.         }
  126.     }          
  127. }
Add Comment
Please, Sign In to add comment