Advertisement
Guest User

Untitled

a guest
Jul 1st, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.12 KB | None | 0 0
  1. package layout;
  2.  
  3. import java.awt.Color;
  4. import java.awt.HeadlessException;
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.ActionListener;
  7.  
  8. import javax.swing.GroupLayout;
  9. import javax.swing.JButton;
  10. import javax.swing.JFrame;
  11. import javax.swing.JLabel;
  12. import javax.swing.JPasswordField;
  13. import javax.swing.JTextField;
  14.  
  15. import core.ClientHandler;
  16. import core.Mensaje;
  17.  
  18. public class Login extends JFrame implements ActionListener {
  19.     private static final long serialVersionUID = 1L;
  20.     JButton btnEnviar;
  21.     JLabel lblUser, lblPass, lblIP, lblErrorLogin;
  22.     JTextField user, address;
  23.     JPasswordField password;
  24.     private ClientHandler cliente;
  25.  
  26.     public Login(String title, ClientHandler unCliente)
  27.             throws HeadlessException {
  28.         super(title);
  29.         cliente = unCliente;
  30.         init();
  31.     }
  32.  
  33.     public void init() {
  34.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  35.         setResizable(false);
  36.         GroupLayout layout = new GroupLayout(getContentPane());
  37.         getContentPane().setLayout(layout);
  38.  
  39.         // Etiquetas
  40.         lblUser = new JLabel("Usuario", JLabel.CENTER);
  41.         lblPass = new JLabel("Password", JLabel.CENTER);
  42.         lblIP = new JLabel("IP Address", JLabel.CENTER);
  43.         lblErrorLogin = new JLabel(" ", JLabel.CENTER);
  44.  
  45.         // Campos
  46.         user = new JTextField(20);
  47.         password = new JPasswordField(20);
  48.         address = new JTextField(10);
  49.  
  50.         // Boton
  51.         btnEnviar = new JButton("Enviar");
  52.  
  53.         // Listeners
  54.         btnEnviar.addActionListener(this);
  55.         user.addActionListener(this);
  56.         password.addActionListener(this);
  57.  
  58.         // Espacios
  59.         final int DEFAULT_GAP = 5;
  60.         layout.setAutoCreateGaps(true);
  61.  
  62.         layout.setHorizontalGroup(layout
  63.                 .createParallelGroup(GroupLayout.Alignment.CENTER)
  64.                 .addGap(DEFAULT_GAP)
  65.                 .addGroup(
  66.                         layout.createSequentialGroup()
  67.                                 .addComponent(lblUser, 75, 75, 75)
  68.                                 .addComponent(user, 125, 125, 125)
  69.                                 .addGap(DEFAULT_GAP))
  70.                 .addGroup(
  71.                         layout.createSequentialGroup()
  72.                                 .addComponent(lblPass, 75, 75, 75)
  73.                                 .addComponent(password, 125, 125, 125)
  74.                                 .addGap(DEFAULT_GAP))
  75.                 .addGroup(
  76.                         layout.createSequentialGroup()
  77.                                 .addComponent(lblIP, 75, 75, 75)
  78.                                 .addComponent(address, 125, 125, 125)
  79.                                 .addGap(DEFAULT_GAP))
  80.  
  81.                 .addGroup(
  82.                         layout.createParallelGroup(GroupLayout.Alignment.CENTER)
  83.                                 .addComponent(btnEnviar)
  84.                                 .addComponent(lblErrorLogin)
  85.                                 .addGap(DEFAULT_GAP))
  86.  
  87.         );
  88.  
  89.         layout.setVerticalGroup(layout
  90.                 .createSequentialGroup()
  91.                 .addGap(DEFAULT_GAP)
  92.                 .addGroup(
  93.                         layout.createParallelGroup().addComponent(lblUser)
  94.                                 .addComponent(user, 20, 20, 20))
  95.                 .addGroup(
  96.                         layout.createParallelGroup().addComponent(lblPass)
  97.                                 .addComponent(password, 20, 20, 20))
  98.                 .addGroup(
  99.                         layout.createParallelGroup().addComponent(lblIP)
  100.                                 .addComponent(address, 20, 20, 20))
  101.                 .addGroup(
  102.                         layout.createSequentialGroup().addComponent(btnEnviar)
  103.                                 .addComponent(lblErrorLogin)
  104.                                 .addGap(DEFAULT_GAP)));
  105.  
  106.         pack();
  107.         setVisible(true);
  108.         setLocationRelativeTo(null);
  109.         /*
  110.          * Luego de empacar todo, intenta colocar la ventana en el centro, sin
  111.          * importar la resolucion
  112.          */
  113.  
  114.     }
  115.  
  116.     @Override
  117.     public void actionPerformed(ActionEvent a) {
  118.  
  119.         // Boton Enviar
  120.         if (a.getSource() == btnEnviar) {
  121.             lblUser.setForeground(Color.BLACK);
  122.             lblPass.setForeground(Color.BLACK);
  123.             if (verificarCampos() == false) {
  124.                 // Do Nothing.
  125.             } else {
  126.                 cliente.user().userName(user.getText());
  127.                 Mensaje mensajeLogin = new Mensaje();
  128.                 mensajeLogin.handshake(cliente.user(), true, 0);
  129.                 cliente.server(address.getText());
  130.                 cliente.enviarMensaje(mensajeLogin);
  131.  
  132.                 if (cliente.redoLogin() == true) {
  133.                     lblErrorLogin
  134.                             .setText("El usuario " + user.getText() + " se encuentra en uso");
  135.                     user.setText(null);
  136.                     password.setText(null);
  137.                 } else {
  138.                     cliente.crearCanal();
  139.                     cliente.crearInvitar();
  140. //                  this.dispose();
  141.                 }
  142.  
  143.             }
  144.         }
  145.     }
  146.  
  147.     public boolean verificarCampos() {
  148.         int p = password.getPassword().length;
  149.         int u = user.getText().length();
  150.         boolean correcto = false;
  151.  
  152.         if (u == 0) {
  153.             lblUser.setForeground(Color.RED);
  154.         }
  155.         if (p == 0) {
  156.             lblPass.setForeground(Color.RED);
  157.         }
  158.  
  159.         if (u != 0 && p != 0) {
  160.             correcto = true;
  161.         }
  162.         return correcto;
  163.     }
  164.  
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement