Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.event.*;
- import javax.swing.*;
- import java.awt.*;
- import java.io.*;
- class CreateUser implements ActionListener
- {
- JFrame fr; //Frame
- JButton b1; //Create Button
- JLabel lb1, lb2, lb3; //Username and password
- JTextField tf; //Username and password input fields
- JPasswordField pf;
- JPanel p1;
- CreateUser()
- {
- //Setting the frame
- fr=new JFrame();
- fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- fr.setLayout(null);
- fr.setSize(400,400);
- //setting panel
- p1=new JPanel();
- p1.setBounds(0,0,400,400);
- p1.setLayout(null);
- //setting Username Label
- JLabel lb1=new JLabel("Username: ");
- lb1.setBounds(50,50,70,30);
- p1.add(lb1);
- //setting Username Text Field
- tf= new JTextField();
- tf.setBounds(150,50,150,30);
- p1.add(tf);
- //setting Password Label
- JLabel lb2=new JLabel("Password: ");
- lb2.setBounds(50,100,70,30);
- p1.add(lb2);
- //setting Password Text Field
- pf = new JPasswordField();
- pf.setBounds(150,100,150,30);
- p1.add(pf);
- //setting Button
- b1=new JButton("Create");
- b1.setBounds(100,200,100,40);
- p1.add(b1);
- fr.add(p1);
- fr.setVisible(true);
- b1.addActionListener(this);
- tf.addActionListener(this);
- pf.addActionListener(this);
- }
- public static void main(String[] s)
- {
- SwingUtilities.invokeLater(() -> new CreateUser());
- }
- public void actionPerformed(ActionEvent e)
- {
- if(e.getSource()==b1)
- {
- String uid = tf.getText();
- String pass = String.valueOf(pf.getPassword());
- //stores current file path to dir
- String dir = System.getProperty("user.dir");
- //Creating a new Users folder if doesn't exists
- File file = new File(dir=dir+"\\users");
- if (!file.exists())
- {
- if (file.mkdir())
- {
- System.out.println("Directory is created!");
- }
- else
- {
- System.out.println("Failed to create directory!");
- }
- }
- String dir1 = dir+"\\users";
- //Creating a folder named with username inside Users folder
- file = new File(dir=dir+"\\"+uid);
- if(file.exists())
- {
- JOptionPane.showMessageDialog(null, "User Already Exists!! \nPlease choose different Username!");
- }
- else
- //if (!file1.exists())
- {
- if (file.mkdir())
- {
- System.out.println("Directory is created!");
- }
- else
- {
- System.out.println("Failed to create directory!");
- }
- }
- //Storing Password.txt inside users/username folder
- try
- {
- FileOutputStream fout=new FileOutputStream(dir+"\\password.txt");
- byte b[]=pass.getBytes();
- fout.write(b);
- }
- catch(Exception ee)
- {
- ee.printStackTrace();
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment