package interfaz;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.LayoutManager;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class VentanaPrincipal extends JFrame {
//VARIABLES
private JButton btAceptar, btSalir;
private JLabel lUsuario, lPass, lImagen;
private JTextField tUsuario;
private JPasswordField tPass;
private JPanel pUsuarios, pImagen;
private VentanaPrincipal vP;
public VentanaPrincipal(){
this.setTitle("Conexión con la Aplicación");
this.setLocationRelativeTo(null);
vP = this;
this.setResizable(false);
this.setLocationRelativeTo(null);
FlowLayout fl = new FlowLayout();
this.setLayout(fl);
// FUNCIONES
CrearInterfaz();
Escuchadores();
}
private void CrearInterfaz(){
//PANEL ACCESO USUARIOS
pUsuarios = new JPanel();
// GESTOR DE LA VENTANA EN PARRILLA
GridLayout gridLayout1 = new GridLayout();
gridLayout1.setRows(3);
gridLayout1.setHgap(20);
gridLayout1.setColumns(2);
gridLayout1.setVgap(10);
//ESTABLECER EL GESTOR
pUsuarios.setLayout(gridLayout1);
this.add(pUsuarios, null);
lUsuario = new JLabel("Usuario: ");
lPass = new JLabel("Contraseña: ");
tUsuario = new JTextField();
tPass = new JPasswordField();
btSalir = new JButton("Salir");
btAceptar = new JButton("Aceptar");
pUsuarios.add(lUsuario);
pUsuarios.add(tUsuario);
pUsuarios.add(lPass);
pUsuarios.add(tPass);
pUsuarios.add(btAceptar);
pUsuarios.add(btSalir);
//PANEL IMAGEN
pImagen = new JPanel();
this.add(pImagen, null);
lImagen = new JLabel(new ImageIcon(getClass().getResource("/images/candado.png")));
pImagen.add(lImagen);
}
private void Escuchadores(){
btSalir.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
System.exit(0);
}
});
btAceptar.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//ACCEDEMOS A LA BASE DE DATOS
String user="pakitoelchocolatero";
String password="666";
String url = "jdbc:mysql://10.25.4.10/AccesosRep";
try{
//Instanciación del conextor jdbc
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection(url, user, password);
String consulta="SELECT usuario, password FROM USUARIOS WHERE password = '" + tPass.getPassword().toString() + "'" ;
stmt = conn.createStatement();
rs = stmt.executeQuery(consulta);
if (rs.next()){
// SI ENTRA AQUI ES QUE SE HA INDICADO UN USUARIO Y CONTRASEÑA CORRECTOS Y ABRIMOS UNA NUEVA VENTANA
AccesoSistema AS = new AccesoSistema();
AS.pack();
AS.setResizable(false);
AS.setLocationRelativeTo(null);
vP.setVisible(false);
AS.setVisible(true);
AS.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
else {
JOptionPane.showMessageDialog(null, "El usuario o la contraseña no coinciden. Vuelva a Intentarlo de nuevo.");
}
}
finally{
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
}
}