Olá,
Gostaria que, após efetuar o login, a página de login fechasse e uma nova janela abrisse. Usei o .setVisible(true), mas não funciona. Simplesmente não abre a janela.
segue o código:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package sistemagastos;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Driver;
import com.mysql.jdbc.PreparedStatement;
import java.awt.Frame;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author André Ravazzi
*/
public class Login extends javax.swing.JFrame {
/**
* Creates new form Login
*/
public Login() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
usuarioInput = new javax.swing.JTextField();
pwInput = new javax.swing.JPasswordField();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
acessarLogin = new javax.swing.JButton();
jLabel5 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
usuarioInput.setFont(new java.awt.Font("Century Gothic", 0, 15)); // NOI18N
usuarioInput.setHorizontalAlignment(javax.swing.JTextField.CENTER);
usuarioInput.setToolTipText("");
usuarioInput.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
usuarioInputActionPerformed(evt);
}
});
pwInput.setHorizontalAlignment(javax.swing.JTextField.CENTER);
pwInput.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
pwInputActionPerformed(evt);
}
});
jLabel3.setFont(new java.awt.Font("Century Gothic", 0, 15)); // NOI18N
jLabel3.setText("Usuário");
jLabel4.setFont(new java.awt.Font("Century Gothic", 0, 15)); // NOI18N
jLabel4.setText("Senha");
jLabel1.setFont(new java.awt.Font("Century Gothic", 0, 24)); // NOI18N
jLabel1.setForeground(new java.awt.Color(255, 53, 14));
jLabel1.setText("Gerenciador de Gastos Internos");
jLabel2.setFont(new java.awt.Font("Century Gothic", 0, 15)); // NOI18N
jLabel2.setForeground(new java.awt.Color(255, 53, 14));
jLabel2.setText("Entre com as credenciais");
acessarLogin.setFont(new java.awt.Font("Century Gothic", 0, 18)); // NOI18N
acessarLogin.setForeground(new java.awt.Color(222, 13, 40));
acessarLogin.setText("Acessar");
acessarLogin.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
acessarLoginActionPerformed(evt);
}
});
jLabel5.setIcon(new javax.swing.ImageIcon(getClass().getResource("/sistemagastos/images/senoweb.png"))); // NOI18N
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(144, 144, 144)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(acessarLogin, javax.swing.GroupLayout.PREFERRED_SIZE, 112, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jLabel4)
.addComponent(jLabel3))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(pwInput, javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(usuarioInput, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 149, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addComponent(jLabel2)))
.addGroup(layout.createSequentialGroup()
.addGap(86, 86, 86)
.addComponent(jLabel1))
.addGroup(layout.createSequentialGroup()
.addGap(52, 52, 52)
.addComponent(jLabel5)))
.addContainerGap(58, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(31, 31, 31)
.addComponent(jLabel5)
.addGap(32, 32, 32)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jLabel2)
.addGap(32, 32, 32)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(usuarioInput, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(21, 21, 21)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(pwInput, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(34, 34, 34)
.addComponent(acessarLogin, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(67, Short.MAX_VALUE))
);
jLabel3.getAccessibleContext().setAccessibleDescription("");
pack();
}// </editor-fold>
private void pwInputActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void usuarioInputActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void acessarLoginActionPerformed(java.awt.event.ActionEvent evt) {
//procura pela classe de conexão
try {
Class.forName("com.mysql.jdbc.Driver");
java.sql.Connection con = DriverManager.getConnection("jdbc:mysql:localhost", "root", "aaa");
String usuario = usuarioInput.getText();
String senha = new String(pwInput.getPassword());
String query = "SELECT * FROM usuarios WHERE usuario = '"+usuario+"' AND senha = '"+senha+"'";
try (java.sql.PreparedStatement login1 = con.prepareStatement(query)) {
java.sql.ResultSet login = login1.executeQuery();
if(login.next()) {
System.out.println("Bem Vindo!");
//fecha janela
this.dispose();
dashboard ds = new dashboard();
ds.setVisible(true); //ISSO NÃO FUNCIONA. MAS NÃO RETORNA ERRO .-.
} else {
System.out.println("usuário ou senha incorretos");
}
}
} catch (SQLException | ClassNotFoundException ex1) {
System.out.println(ex1);
}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/*
* Set the Nimbus look and feel
*/
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/*
* If Nimbus (introduced in Java SE 6) is not available, stay with the
* default look and feel. For details see
* http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/*
* Create and display the form
*/
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Login().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton acessarLogin;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JPasswordField pwInput;
private javax.swing.JTextField usuarioInput;
// End of variables declaration
}
Obrigado =D