3
I’m starting in Java and I want to make a screen where the person register the login and password, the system stores the data and then, in a new window is asked to enter the user and password, I did the validation and registration part of the data on the same page, because I do not master the JFrames.
How do I separate things, create, for example, a page and then close and open a new one? I don’t know where I can close the code and open a new one, for example.
Follow the current code, I’m working with Eclipse.
import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
public class Exemplo1 extends JFrame implements ActionListener{ 
JButton entrar; 
JTextField cxnome; 
JTextField cxsenha; 
JLabel rotulo; 
JTextField cxvarnome; //variavel de senha a ser inserida
JTextField cxvarsenha; //variavel de senha a ser inserida
public void actionPerformed(ActionEvent evento){ 
String nome, senha, suasenha, seunome; 
nome = cxnome.getText(); 
senha = cxsenha.getText(); 
seunome = cxvarnome.getText(); 
suasenha = cxvarsenha.getText(); 
//metodo da interface ActionListener 
//como o tipo String não é um tipo primitivo, e sim 
//um tipo referencial, sua comparação não pode ser == 
if (evento.getSource()== entrar && nome.equals(seunome)&& senha.equals(suasenha)){
    rotulo.setText("CORRETO");
    dispose();
}
else{
    rotulo.setText("FALHO");
}
}
public static void main(String[] args){ 
    //instanciando objeto 
    Exemplo1 janela = new Exemplo1(); 
    janela.setVisible(true); 
    janela.setTitle("login"); 
    janela.setSize(200,200); 
    janela.setLocation(400,300); 
    } 
//construtor 
public Exemplo1(){ 
//gride para os objetos 
getContentPane().setLayout(new GridLayout(4,1)); 
cxvarnome = new JTextField();//instanciando 
getContentPane().add(cxvarnome);//coloca... no grid 
cxvarsenha = new JTextField();//instanciando 
getContentPane().add(cxvarsenha);//coloca... no grid 
cxnome = new JTextField();//instanciando 
getContentPane().add(cxnome);//coloca... no grid 
cxsenha = new JTextField();//instanciando 
getContentPane().add(cxsenha);//coloc... no grid 
entrar = new JButton("OK");//instanciando 
getContentPane().add(entrar);//coloca... no grid 
entrar.addActionListener(this);//add evento ao clicar 
rotulo = new JLabel();//instanciando 
getContentPane().add(rotulo);//coloca... no grid 
rotulo.setOpaque(true);//tornando opaco 
rotulo.setBackground(Color.orange); 
} 
}
 

I don’t quite understand your question, but I think you could take a look at this question here: Toggle Jpanels within a single Jframe, maybe it helps (or maybe it doesn’t).
– Math