Olá pessoal,
Estou apanhando pra fazer um formulário abrir outro formulário no Java.
Veja, não consigo fazer os formulários se enchergarem:
Estou tentando assim:
frame.setVisible(false);
frame2.setVisible(true);
E segue os códigos (Formulário 1):
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JButton;
import java.awt.FlowLayout;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.Window;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.awt.event.ActionEvent;
import javax.swing.JTextField;
public class Tela extends JFrame {
private JPanel contentPane;
private JTextField textField;
private JTextField textField_1;
static Tela frame;
/*try {
Tela frame = new Tela();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}*/
/**
* Launch the application.
*/
public static void main(String[] args) throws SQLException{
{
//Class.forName("com.mysql.jdbc.Driver");
//System.out.println("Conectado.");
//conexao.close();
EventQueue.invokeLater(new Runnable() {
public void run() {
frame = new Tela();
frame.setVisible(true);
}
});
} }
/**
* Create the frame.
*/
public Tela()
{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
GridBagLayout gbl_contentPane = new GridBagLayout();
gbl_contentPane.columnWidths = new int[]{171, 0, 0, 25, 61, 0};
gbl_contentPane.rowHeights = new int[]{23, 0, 0, 0, 0, 0, 0, 0, 0};
gbl_contentPane.columnWeights = new double[]{0.0, 1.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
gbl_contentPane.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
contentPane.setLayout(gbl_contentPane);
JButton btnNewButton = new JButton("Login");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(e.getSource() == btnNewButton)
{
try {
// Se clicou no botão "Login"
String loginUsuario = textField.getText();
String passwordUsuario = textField_1.getText();
//lblNewLabel.setText("Correto");
Connection conexao = DriverManager.getConnection("jdbc:mysql://localhost:3306/jogo?useTimezone=true&serverTimezone=UTC","root","");
Statement estado = conexao.createStatement();
String querySelecionaUsuario = "SELECT * FROM usuario WHERE usuarioLogin = '"+ loginUsuario +"'"
+ "AND usuarioSenha = '" + passwordUsuario +"';";
ResultSet rs = estado.executeQuery(querySelecionaUsuario);
String queryCompleta = "SELECT * FROM usuario WHERE usuarioLogin = '"+ loginUsuario +"'"
+ "AND usuarioSenha = '" + passwordUsuario +"';";
if(rs.next())
{
String id = rs.getString("usuarioId");
String nome = rs.getString("usuarioNome");
String login = rs.getString("usuarioLogin");
String senha = rs.getString("usuarioSenha");
String email = rs.getString("usuarioEmail");
/*System.out.println(id);
System.out.println(nome);
System.out.println(login);
System.out.println(senha);
System.out.println(email);*/
JOptionPane.showMessageDialog(null,queryCompleta,"Query",JOptionPane.INFORMATION_MESSAGE);
frame.setVisible(false);
frame2.setVisible(true);
}}catch (Exception exception) {
exception.printStackTrace();
}
} }});
JLabel lblNewLabel = new JLabel("Login");
GridBagConstraints gbc_lblNewLabel = new GridBagConstraints();
gbc_lblNewLabel.anchor = GridBagConstraints.EAST;
gbc_lblNewLabel.insets = new Insets(0, 0, 5, 5);
gbc_lblNewLabel.gridx = 0;
gbc_lblNewLabel.gridy = 2;
contentPane.add(lblNewLabel, gbc_lblNewLabel);
textField = new JTextField();
GridBagConstraints gbc_textField = new GridBagConstraints();
gbc_textField.insets = new Insets(0, 0, 5, 5);
gbc_textField.fill = GridBagConstraints.HORIZONTAL;
gbc_textField.gridx = 1;
gbc_textField.gridy = 2;
contentPane.add(textField, gbc_textField);
textField.setColumns(10);
JLabel lblNewLabel_1 = new JLabel("Password");
GridBagConstraints gbc_lblNewLabel_1 = new GridBagConstraints();
gbc_lblNewLabel_1.anchor = GridBagConstraints.EAST;
gbc_lblNewLabel_1.insets = new Insets(0, 0, 5, 5);
gbc_lblNewLabel_1.gridx = 0;
gbc_lblNewLabel_1.gridy = 3;
contentPane.add(lblNewLabel_1, gbc_lblNewLabel_1);
textField_1 = new JTextField();
GridBagConstraints gbc_textField_1 = new GridBagConstraints();
gbc_textField_1.insets = new Insets(0, 0, 5, 5);
gbc_textField_1.fill = GridBagConstraints.HORIZONTAL;
gbc_textField_1.gridx = 1;
gbc_textField_1.gridy = 3;
contentPane.add(textField_1, gbc_textField_1);
textField_1.setColumns(10);
GridBagConstraints gbc_btnNewButton = new GridBagConstraints();
gbc_btnNewButton.insets = new Insets(0, 0, 5, 5);
gbc_btnNewButton.anchor = GridBagConstraints.NORTH;
gbc_btnNewButton.gridx = 1;
gbc_btnNewButton.gridy = 5;
contentPane.add(btnNewButton, gbc_btnNewButton);
}
}
(Formulário 2):
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
public class telaInicial extends JFrame {
private JPanel contentPane;
static telaInicial frame2;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
frame2 = new telaInicial();
frame2.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public telaInicial() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
}
}
Alguem pode dar uma força?
Obrigado.