Advertisement
Guest User

Untitled

a guest
Sep 24th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.76 KB | None | 0 0
  1.  
  2. import java.awt.BorderLayout;
  3. import java.awt.Container;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6. import java.io.BufferedReader;
  7. import java.io.BufferedWriter;
  8. import java.io.File;
  9. import java.io.FileNotFoundException;
  10. import java.io.FileReader;
  11. import java.io.FileWriter;
  12. import java.io.IOException;
  13.  
  14. import javax.swing.ImageIcon;
  15. import javax.swing.JButton;
  16. import javax.swing.JFrame;
  17. import javax.swing.JOptionPane;
  18. import javax.swing.JPanel;
  19. import javax.swing.JPasswordField;
  20. import javax.swing.JTextField;
  21.  
  22. public class Gui extends JFrame implements ActionListener {
  23.  
  24.     private static final long serialVersionUID = 1L;
  25.     public static boolean conected = false;
  26.     JTextField text = new JTextField("usuario");
  27.     JPasswordField pass = new JPasswordField("password");
  28.    
  29.     public String getUsername() {
  30.         return text.getText();
  31.     }
  32.    
  33.     @SuppressWarnings("deprecation")
  34.     public String getPassword() {
  35.         return pass.getText();
  36.     }
  37.    
  38.     public Gui() {
  39.         super("Iniciar Sesion");
  40.         JButton button = new JButton(new ImageIcon("data/images/connect.png"));
  41.         JPanel panel = new JPanel();
  42.         Container content = getContentPane();
  43.         panel.add(button);
  44.         content.add(text, BorderLayout.NORTH);
  45.         content.add(pass, BorderLayout.CENTER);
  46.         content.add(panel, BorderLayout.SOUTH);
  47.         button.addActionListener(this);
  48.         button.setText("Conectar");
  49.         setBounds(400, 300, 220, 125);
  50.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  51.         setVisible(true);
  52.     }
  53.    
  54.     public static void main(String[] args) {
  55.         new Gui();
  56.     }
  57.    
  58.     @Override
  59.     public void actionPerformed(ActionEvent arg0) {
  60.         try {
  61.             File user = new File("data/users/"+getUsername()+".txt");
  62.             if(!user.exists()) {
  63.                 msj("Usuario Incorrecto.", "Error", 1);
  64.                 return;
  65.             }
  66.             BufferedReader file = new BufferedReader(new FileReader("data/users/"+getUsername()+".txt"));
  67.             if(!file.readLine().equals(getPassword())) {
  68.                 msj("Contraseña Incorrecta.", "Error", 1);
  69.                 return;
  70.             }
  71.             conected = true;
  72.             msj(getUsername()+" ha iniciado sesion.", "Mensaje", 0);
  73.        
  74.         } catch (FileNotFoundException e) {
  75.             e.printStackTrace();
  76.         } catch (IOException e) {
  77.             e.printStackTrace();
  78.         }
  79.     }
  80.    
  81.     private void msj(String text, String title, int type) {
  82.         JPanel panel = new JPanel();
  83.         if(type == 0)
  84.             JOptionPane.showMessageDialog(panel, text, title, JOptionPane.WARNING_MESSAGE);
  85.         else
  86.             JOptionPane.showMessageDialog(panel, text, title, JOptionPane.ERROR_MESSAGE);
  87.     }
  88.    
  89.     public static void create(String user, String pass) {
  90.         try {
  91.             FileWriter fstream = new FileWriter("data/users/"+user+".txt");
  92.             BufferedWriter out = new BufferedWriter(fstream);
  93.             out.write(pass);
  94.             out.close();
  95.         } catch (Exception e) {
  96.             e.printStackTrace();
  97.         }            
  98.     }
  99.  
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement