Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.BorderLayout;
- import java.awt.Container;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.io.IOException;
- import javax.swing.ImageIcon;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JOptionPane;
- import javax.swing.JPanel;
- import javax.swing.JPasswordField;
- import javax.swing.JTextField;
- public class Gui extends JFrame implements ActionListener {
- private static final long serialVersionUID = 1L;
- public static boolean conected = false;
- JTextField text = new JTextField("usuario");
- JPasswordField pass = new JPasswordField("password");
- public String getUsername() {
- return text.getText();
- }
- @SuppressWarnings("deprecation")
- public String getPassword() {
- return pass.getText();
- }
- public Gui() {
- super("Iniciar Sesion");
- JButton button = new JButton(new ImageIcon("data/images/connect.png"));
- JPanel panel = new JPanel();
- Container content = getContentPane();
- panel.add(button);
- content.add(text, BorderLayout.NORTH);
- content.add(pass, BorderLayout.CENTER);
- content.add(panel, BorderLayout.SOUTH);
- button.addActionListener(this);
- button.setText("Conectar");
- setBounds(400, 300, 220, 125);
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setVisible(true);
- }
- public static void main(String[] args) {
- new Gui();
- }
- @Override
- public void actionPerformed(ActionEvent arg0) {
- try {
- File user = new File("data/users/"+getUsername()+".txt");
- if(!user.exists()) {
- msj("Usuario Incorrecto.", "Error", 1);
- return;
- }
- BufferedReader file = new BufferedReader(new FileReader("data/users/"+getUsername()+".txt"));
- if(!file.readLine().equals(getPassword())) {
- msj("Contraseña Incorrecta.", "Error", 1);
- return;
- }
- conected = true;
- msj(getUsername()+" ha iniciado sesion.", "Mensaje", 0);
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- private void msj(String text, String title, int type) {
- JPanel panel = new JPanel();
- if(type == 0)
- JOptionPane.showMessageDialog(panel, text, title, JOptionPane.WARNING_MESSAGE);
- else
- JOptionPane.showMessageDialog(panel, text, title, JOptionPane.ERROR_MESSAGE);
- }
- public static void create(String user, String pass) {
- try {
- FileWriter fstream = new FileWriter("data/users/"+user+".txt");
- BufferedWriter out = new BufferedWriter(fstream);
- out.write(pass);
- out.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement