The function that I want the program to perform is that when the user and password is validated, the JFrame window will be displayed. But it does not work
package formularios;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import bean.Usuario;
import hilos.HiloBarra;
import mantenimientos.GestionUsuario;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JButton;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.ImageIcon;
import javax.swing.JProgressBar;
import javax.swing.event.ChangeListener;
import javax.swing.event.ChangeEvent;
import java.sql.SQLException;
public class frmLogin extends JFrame {
private JPanel contentPane;
private JTextField txtUsuario;
private JPasswordField txtContrasena;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
frmLogin frame = new frmLogin();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public frmLogin() {
setTitle("Pantalla de acceso");
setTitle("PANTALLA DE ACCESO");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 556, 432);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblUsuario = new JLabel("Usuario:");
lblUsuario.setFont(new Font("Tahoma", Font.PLAIN, 14));
lblUsuario.setIcon(new ImageIcon(frmLogin.class.getResource("/imagenes/user.png")));
lblUsuario.setBounds(137, 169, 121, 51);
contentPane.add(lblUsuario);
JLabel lblContrasea = new JLabel("Contrase\u00F1a:");
lblContrasea.setFont(new Font("Tahoma", Font.PLAIN, 14));
lblContrasea.setIcon(new ImageIcon("C:\Users\Neto\Documents\3ABM REYES\iconos programa\password.png"));
lblContrasea.setBounds(137, 215, 126, 51);
contentPane.add(lblContrasea);
txtUsuario = new JTextField();
txtUsuario.setBounds(257, 186, 139, 20);
contentPane.add(txtUsuario);
txtUsuario.setColumns(10);
txtContrasena = new JPasswordField();
txtContrasena.setBounds(257, 231, 139, 20);
contentPane.add(txtContrasena);
JButton btnIngresar = new JButton("INGRESAR");
btnIngresar.setIcon(new ImageIcon(frmLogin.class.getResource("/imagenes/login.png")));
btnIngresar.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ingresar();
}
});
btnIngresar.setFont(new Font("Tahoma", Font.PLAIN, 11));
btnIngresar.setBounds(264, 316, 144, 44);
contentPane.add(btnIngresar);
JButton btnNewButton = new JButton("SALIR");
btnNewButton.setIcon(new ImageIcon(frmLogin.class.getResource("/imagenes/error.png")));
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
salir();
}
});
btnNewButton.setBounds(123, 316, 110, 44);
contentPane.add(btnNewButton);
JLabel label = new JLabel(" ");
label.setIcon(new ImageIcon(frmLogin.class.getResource("/imagenes/logo.png")));
label.setBounds(178, -12, 379, 200);
contentPane.add(label);
}
protected void ingresar() {
String usuario = txtUsuario.getText();
String password = String.valueOf(txtContrasena.getPassword());
GestionUsuario gestionUsuario = new GestionUsuario();
Usuario usuario2 = new Usuario();
usuario2.setNombre(usuario);
usuario2.setPassword(password);
Usuario usu = gestionUsuario.obtenerUsuario(usuario2);
if(usu!=null){
JOptionPane.showMessageDialog(contentPane, "BIENVENIDA");
Ventana frame = new Ventana();
frame.setVisible(true);
this.dispose();
}
else{ JOptionPane.showMessageDialog(contentPane, "ERROR DATOS INVALIDOS","ERROR",JOptionPane.ERROR_MESSAGE);
}
}
protected void salir() {
System.exit(0);
}
}
package formularios;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
public class Ventana {
public static void main(String[] args){
// create JFrame and JTable
JFrame frame = new JFrame();
JTable table = new JTable();
// create a table model and set a Column Identifiers to this model
Object[] columns = {"Id","First Name","Last Name","Age"};
DefaultTableModel model = new DefaultTableModel();
model.setColumnIdentifiers(columns);
// set the model to the table
table.setModel(model);
// Change A JTable Background Color, Font Size, Font Color, Row Height
table.setBackground(Color.LIGHT_GRAY);
table.setForeground(Color.black);
Font font = new Font("",1,22);
table.setFont(font);
table.setRowHeight(30);
// create JTextFields
JTextField txtTitulo = new JTextField();
JTextField txtAutor = new JTextField();
JTextField txtAno = new JTextField();
JTextField txtGeneroMateria = new JTextField();
// create JButtons
JButton btnAdd = new JButton("Agregar");
JButton btnDelete = new JButton("Borrar");
JButton btnUpdate = new JButton("Actualizar");
txtTitulo.setBounds(20, 220, 100, 25);
txtAutor.setBounds(20, 250, 100, 25);
txtAno.setBounds(20, 280, 100, 25);
txtGeneroMateria.setBounds(20, 310, 100, 25);
btnAdd.setBounds(150, 220, 100, 25);
btnUpdate.setBounds(150, 265, 100, 25);
btnDelete.setBounds(150, 310, 100, 25);
// create JScrollPane
JScrollPane pane = new JScrollPane(table);
pane.setBounds(0, 0, 880, 200);
frame.getContentPane().setLayout(null);
frame.getContentPane().add(pane);
// add JTextFields to the jframe
frame.getContentPane().add(txtTitulo);
frame.getContentPane().add(txtAutor);
frame.getContentPane().add(txtAno);
frame.getContentPane().add(txtGeneroMateria);
// add JButtons to the jframe
frame.getContentPane().add(btnAdd);
frame.getContentPane().add(btnDelete);
frame.getContentPane().add(btnUpdate);
// create an array of objects to set the row data
Object[] row = new Object[4];
// button add row
btnAdd.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
row[0] = txtTitulo.getText();
row[1] = txtAutor.getText();
row[2] = txtAno.getText();
row[3] = txtGeneroMateria.getText();
// add row to the model
model.addRow(row);
}
});
// button remove row
btnDelete.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
// i = the index of the selected row
int i = table.getSelectedRow();
if(i >= 0){
// remove a row from jtable
model.removeRow(i);
}
else{
System.out.println("Error al eliminar");
}
}
});
// get selected row data From table to textfields
table.addMouseListener(new MouseAdapter(){
@Override
public void mouseClicked(MouseEvent e){
// i = the index of the selected row
int i = table.getSelectedRow();
txtTitulo.setText(model.getValueAt(i, 0).toString());
txtAutor.setText(model.getValueAt(i, 1).toString());
txtAno.setText(model.getValueAt(i, 2).toString());
txtGeneroMateria.setText(model.getValueAt(i, 3).toString());
}
});
// button update row
btnUpdate.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
// i = the index of the selected row
int i = table.getSelectedRow();
if(i >= 0)
{
model.setValueAt(txtTitulo.getText(), i, 0);
model.setValueAt(txtAutor.getText(), i, 1);
model.setValueAt(txtAno.getText(), i, 2);
model.setValueAt(txtGeneroMateria.getText(), i, 3);**texto en negrita**
}
else{
System.out.println("Error al actualizar");
}
}
});
frame.setSize(900,400);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void setVisible(boolean b) {
}
}