olá pessoal é a primeira vez que posto aqui…
eu quero que quando o usuário efetuar o login ele irá para uma nova parte do programa…esse conteúdo dessa janela será trocado por um outro, como por exemplo uma tela com o nome dele e informações para ele realizar a sua consulta a biblioteca…
mas eu não sei como faço para fazer isso
qualquer sugestão sobre algo que eu tenha feito mas que seria melhor de outra forma tb é bem vindo…
alguém poderia me ajudar?
sou iniciante em GUI
[code]package bibiloteca;
import javax.swing.SwingUtilities;
import java.awt.BorderLayout;
import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.Dimension;
import javax.swing.JLabel;
import java.awt.Rectangle;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JPasswordField;
import java.sql.SQLException;
import javax.swing.JOptionPane;
public class Login extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel jContentPane = null;
private JLabel label1 = null;
private JLabel label2 = null;
private JTextField TEXT1 = null;
private JButton BOTAO1 = null;
private JPasswordField password1 = null;
/**
* This method initializes TEXT1
*
* @return javax.swing.JTextField
*/
private JTextField getTEXT1() {
if (TEXT1 == null) {
TEXT1 = new JTextField();
TEXT1.setBounds(new Rectangle(180, 47, 122, 26));
}
return TEXT1;
}
/**
* This method initializes BOTAO1
*
* @return javax.swing.JButton
*/
private JButton getBOTAO1() {
if (BOTAO1 == null) {
BOTAO1 = new JButton();
BOTAO1.setBounds(new Rectangle(147, 116, 112, 31));
BOTAO1.setFont(new Font("Dialog", Font.BOLD, 18));
BOTAO1.setText("LOGAR");
BOTAO1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e){
try
{
Usuario usuario;
usuario = Consulta.efetuarLogin(TEXT1.getText(),new String(password1.getPassword()));
if(usuario == null)
{
JOptionPane.showMessageDialog(null,"LOGIN OU SENHA INVÁLIDOS",
"ERRO",JOptionPane.ERROR_MESSAGE);
}
else
{
JOptionPane.showMessageDialog(null,"SEJA BEM VINDO\n" + usuario.getNome());
}
}
catch(SQLException e1)
{
JOptionPane.showMessageDialog(null,"ERRO NO BANCO DE DADOS",
"ERRO",JOptionPane.ERROR_MESSAGE);
}
catch (Exception e3) {
e3.printStackTrace();
}
}
});
}
return BOTAO1;
}
/**
* This method initializes password1
*
* @return javax.swing.JPasswordField
*/
private JPasswordField getPassword1() {
if (password1 == null) {
password1 = new JPasswordField();
password1.setBounds(new Rectangle(183, 82, 117, 23));
}
return password1;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
SwingUtilities.invokeLater(new Runnable() {
public void run() {
Login thisClass = new Login();
thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
thisClass.setVisible(true);
}
});
}
/**
* This is the default constructor
*/
public Login() {
super();
initialize();
}
/**
* This method initializes this
*
* @return void
*/
private void initialize() {
this.setSize(417, 258);
this.setContentPane(getJContentPane());
this.setTitle("JFrame");
}
/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane() {
if (jContentPane == null) {
label2 = new JLabel();
label2.setBounds(new Rectangle(97, 78, 63, 24));
label2.setFont(new Font("Dialog", Font.BOLD, 18));
label2.setText("SENHA");
label1 = new JLabel();
label1.setBounds(new Rectangle(104, 47, 57, 24));
label1.setFont(new Font("Dialog", Font.BOLD, 18));
label1.setText("LOGIN");
jContentPane = new JPanel();
jContentPane.setLayout(null);
jContentPane.add(label1, null);
jContentPane.add(label2, null);
jContentPane.add(getTEXT1(), null);
jContentPane.add(getBOTAO1(), null);
jContentPane.add(getPassword1(), null);
}
return jContentPane;
}
} // @jve:decl-index=0:visual-constraint=“89,28”
[/code]
[code]package bibiloteca;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class Consulta
{
public static Usuario efetuarLogin(String login,String senha) throws Exception
{
Connection con = null;
//1. Driver a ser utilizado
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//2. Obtendo a conexão com o banco
con = DriverManager.getConnection(
"jdbc:odbc:biblioteca","","");
//3. statement para executar SQLs
Statement stmt = con.createStatement();
//4. Objeto com o resultado da consulta
ResultSet rs = stmt.executeQuery("SELECT usuarios.usu_nome " +
" FROM usuarios " +
" WHERE usuarios.usu_email='" + login + "'" +
"and usuarios.usu_senha='" + senha + "'");
Usuario usu = null;
while (rs.next()) {
usu = new Usuario();
usu.setNome(rs.getString("usu_nome"));
}
return usu;
}
}
[/code]
package bibiloteca;
public class Usuario
{
private String nome;
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
}