Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. package interfaz;
  2.  
  3. import java.awt.BorderLayout;
  4. import java.awt.Color;
  5. import java.awt.FlowLayout;
  6. import java.awt.GridLayout;
  7. import java.awt.LayoutManager;
  8. import java.awt.Panel;
  9. import java.awt.event.ActionEvent;
  10. import java.awt.event.ActionListener;
  11.  
  12. import javax.swing.BorderFactory;
  13. import javax.swing.ImageIcon;
  14. import javax.swing.JButton;
  15. import javax.swing.JFrame;
  16. import javax.swing.JLabel;
  17. import javax.swing.JOptionPane;
  18. import javax.swing.JPanel;
  19. import javax.swing.JPasswordField;
  20. import javax.swing.JTextField;
  21.  
  22. public class VentanaPrincipal extends JFrame {
  23.    
  24.     //VARIABLES
  25.     private JButton btAceptar, btSalir;
  26.     private JLabel lUsuario, lPass, lImagen;
  27.     private JTextField tUsuario;
  28.     private JPasswordField tPass;
  29.     private JPanel pUsuarios, pImagen;
  30.     private VentanaPrincipal vP;
  31.    
  32.     public VentanaPrincipal(){
  33.        
  34.         this.setTitle("Conexión con la Aplicación");
  35.         this.setLocationRelativeTo(null);
  36.        
  37.         vP = this;
  38.        
  39.         this.setResizable(false);
  40.         this.setLocationRelativeTo(null);
  41.        
  42.         FlowLayout fl = new FlowLayout();
  43.         this.setLayout(fl);
  44.        
  45.         // FUNCIONES
  46.                
  47.         CrearInterfaz();
  48.        
  49.         Escuchadores();
  50.     }
  51.    
  52.     private void CrearInterfaz(){
  53.        
  54.         //PANEL ACCESO USUARIOS
  55.        
  56.         pUsuarios = new JPanel();
  57.    
  58.        
  59.         // GESTOR DE LA VENTANA EN PARRILLA
  60.                 GridLayout gridLayout1 = new GridLayout();
  61.                 gridLayout1.setRows(3);
  62.                 gridLayout1.setHgap(20);
  63.                 gridLayout1.setColumns(2);
  64.                 gridLayout1.setVgap(10);
  65.                
  66.                 //ESTABLECER EL GESTOR
  67.                 pUsuarios.setLayout(gridLayout1);
  68.                
  69.         this.add(pUsuarios, null);
  70.        
  71.        
  72.         lUsuario = new JLabel("Usuario: ");
  73.         lPass = new JLabel("Contraseña: ");
  74.        
  75.         tUsuario = new JTextField();
  76.         tPass = new JPasswordField();
  77.        
  78.         btSalir = new JButton("Salir");
  79.         btAceptar = new JButton("Aceptar");
  80.        
  81.         pUsuarios.add(lUsuario);
  82.         pUsuarios.add(tUsuario);
  83.         pUsuarios.add(lPass);
  84.         pUsuarios.add(tPass);
  85.         pUsuarios.add(btAceptar);
  86.         pUsuarios.add(btSalir);
  87.        
  88.        
  89.         //PANEL IMAGEN
  90.        
  91.         pImagen = new JPanel();
  92.        
  93.        
  94.         this.add(pImagen, null);
  95.        
  96.        
  97.         lImagen = new JLabel(new ImageIcon(getClass().getResource("/images/candado.png")));
  98.        
  99.         pImagen.add(lImagen);
  100.        
  101.        
  102.     }
  103.    
  104.     private void Escuchadores(){
  105.        
  106.         btSalir.addActionListener(new ActionListener() {
  107.            
  108.             @Override
  109.             public void actionPerformed(ActionEvent arg0) {
  110.                 System.exit(0);
  111.                
  112.             }
  113.         });
  114.        
  115.        
  116.         btAceptar.addActionListener(new ActionListener() {
  117.            
  118.             @Override
  119.             public void actionPerformed(ActionEvent e) {
  120.            
  121.                 //ACCEDEMOS A LA BASE DE DATOS
  122.                
  123.                 String user="pakitoelchocolatero";
  124.                 String password="666";     
  125.                 String url = "jdbc:mysql://10.25.4.10/AccesosRep";
  126.                
  127.                 try{
  128.                     //Instanciación del conextor jdbc
  129.                     Class.forName("com.mysql.jdbc.Driver").newInstance();          
  130.                     conn = DriverManager.getConnection(url, user, password);
  131.                     String consulta="SELECT usuario, password FROM USUARIOS WHERE password = '" + tPass.getPassword().toString() + "'" ;
  132.  
  133.                     stmt = conn.createStatement();
  134.                     rs = stmt.executeQuery(consulta);
  135.                    
  136.                     if (rs.next()){
  137.                        
  138.                         // SI ENTRA AQUI ES QUE SE HA INDICADO UN USUARIO Y CONTRASEÑA CORRECTOS Y ABRIMOS UNA NUEVA VENTANA
  139.                         AccesoSistema AS = new AccesoSistema();
  140.                         AS.pack();
  141.                         AS.setResizable(false);
  142.                         AS.setLocationRelativeTo(null);
  143.                         vP.setVisible(false);
  144.                         AS.setVisible(true);
  145.                         AS.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  146.                        
  147.                     }
  148.                     else {
  149.                        
  150.                         JOptionPane.showMessageDialog(null, "El usuario o la contraseña no coinciden. Vuelva a Intentarlo de nuevo.");
  151.                        
  152.                        
  153.                     }
  154.                        
  155.                 }
  156.                    
  157.                 finally{
  158.                    
  159.                     try {
  160.                         conn.close();
  161.                     } catch (SQLException e) {
  162.                         // TODO Auto-generated catch block
  163.                         e.printStackTrace();
  164.                     }
  165.                 }
  166.                    
  167.                    
  168.                
  169.                
  170.             }
  171.         });
  172.     }
  173.  
  174. }