criei 2 telas uma para realizar um cadastro e outra para fazer o login, consegui chamar uma atraves da outra mas quando eu dou fechar eu uma das 2 as 2 fecham.
Como posso fazer para quando uma tela for fechada a outra ser chamada sem ser abrindo uma nova???
ai vão os códigos
Tela Principal(Cadastro)import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintStream;
import java.text.ParseException;
import java.util.Scanner;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.text.MaskFormatter;
public class TelaPrincipal extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
// Criando elementos
JLabel nome;
JLabel endereco;
JLabel telefone;
JLabel cpf;
JFormattedTextField ip_nome;
JFormattedTextField ip_endereco;
JFormattedTextField ip_telefone;
JFormattedTextField ip_cpf;
JButton limpar;
JButton confirmar;
int codigo;
JMenuBar menubar;
JComboBox estados;
JList lista;
public TelaPrincipal() throws IOException, ParseException {
// ComboBox
Object[] estado = { "", "Alagoas", "Amapá", "Amazonas", "Bahia",
"Ceará", "Distrito Federal", "Espírito Santo", "Goiás",
"Maranhão", "Mato Grosso", "Mato Grosso do Sul",
"Minas Gerais", "Pará", "Paraíba", "Paraná", "Pernambuco",
"Piauí", "Rio de Janeiro", "Rio Grande do Norte",
"Rio Grande do Sul", "Rondônia", "Roraima", "Santa Catarina",
"São Paulo", "Sergipe", "Tocantins" };
estados = new JComboBox(estado);
// Criando e configurando os Menus
menubar = new JMenuBar();
// menu arquivo
JMenu arquivo = new JMenu("Arquivo");
arquivo.setMnemonic(KeyEvent.VK_ALT);
JMenuItem item2 = new JMenuItem("Sair");
item2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
arquivo.add(item2);
menubar.add(arquivo);
// menu ferramentas
JMenu ferramentas = new JMenu("Ferramentas");
JMenu cor = new JMenu("Alterar Cor");
JMenuItem azul = new JMenuItem("Azul");
azul.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
getContentPane().setBackground(Color.BLUE);
}
});
JMenuItem verde = new JMenuItem("Verde");
verde.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
getContentPane().setBackground(Color.green);
}
});
JMenuItem vermelho = new JMenuItem("Vermelho");
vermelho.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
getContentPane().setBackground(Color.red);
}
});
JMenuItem branco = new JMenuItem("Branco");
branco.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
getContentPane().setBackground(Color.white);
}
});
ferramentas.add(cor);
cor.add(branco);
cor.add(azul);
cor.add(verde);
cor.add(vermelho);
menubar.add(ferramentas);
super.setJMenuBar(menubar);
// Lista
lista = new JList(estado);
// Criando Mascaras das textfields
MaskFormatter formnome = new MaskFormatter(
"*****************************************");
MaskFormatter formend = new MaskFormatter(
"*****************************************");
MaskFormatter formtel = new MaskFormatter("(**)****-****");
MaskFormatter formcpf = new MaskFormatter("***.***.***-**");
// Editando ações e aparência do panel
getContentPane().setLayout(null);
setTitle("Cadastro");
setVisible(true);
setResizable(true);
setSize(700, 400);
setLocation(300, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Label
nome = new JLabel("Nome:");
endereco = new JLabel("Endereço:");
cpf = new JLabel("CPF:");
telefone = new JLabel("Telefone:");
// TextFields
ip_nome = new JFormattedTextField(formnome);
ip_endereco = new JFormattedTextField(formend);
ip_cpf = new JFormattedTextField(formcpf);
ip_telefone = new JFormattedTextField(formtel);
ip_nome.setCaretPosition(0);
ip_endereco.setCaretPosition(0);
ip_cpf.setCaretPosition(0);
ip_telefone.setCaretPosition(0);
ip_nome.setCaretColor(Color.blue);
// Botões
limpar = new JButton("Limpar");
limpar.addActionListener(new Clique());
confirmar = new JButton("OK");
confirmar.addActionListener(new Clique());
// Adicionando elementos ao panel
getContentPane().add(nome);
getContentPane().add(endereco);
getContentPane().add(cpf);
getContentPane().add(ip_nome);
getContentPane().add(ip_endereco);
getContentPane().add(ip_cpf);
getContentPane().add(limpar);
getContentPane().add(confirmar);
getContentPane().add(ip_telefone);
getContentPane().add(telefone);
getContentPane().add(estados);
// Posicionando elementos
nome.setBounds(5, 23, 50, 10);
ip_nome.setBounds(100, 20, 200, 20);
endereco.setBounds(5, 63, 70, 10);
ip_endereco.setBounds(100, 60, 300, 20);
cpf.setBounds(5, 103, 30, 20);
ip_cpf.setBounds(100, 100, 100, 20);
telefone.setBounds(5, 143, 70, 20);
ip_telefone.setBounds(100, 140, 200, 20);
limpar.setBounds(20, 200, 200, 40);
confirmar.setBounds(240, 200, 200, 40);
menubar.setBounds(1, 1, 500, 30);
estados.setBounds(420, 60, 150, 20);
// Validando Mascaras dos textfields
formnome
.setValidCharacters("qwertyuiopasdfghjklçzxcvbnmMNBVCXZASDFGHJKLÇPOIUYTREWQáàÀÁóÓÉÍíóéôÔâúÚãÃõÕ6üÜÖö ");
formend
.setValidCharacters("qwertyuiopasdfghjklçzxcvbnmMNBVCXZASDFGHJKLÇPOIUYTREWQáàÀÁóÓÉÍíóéôÔâúÚãÃõÕ6üÜÖö [telefone removido]/-_");
formcpf.setValidCharacters("[telefone removido]");
formtel.setValidCharacters("[telefone removido]");
}
public class Clique implements ActionListener {
public void actionPerformed(ActionEvent e) {
// Ações do botão Limpar
if (e.getSource() == limpar) {
ip_nome.setText(null);
ip_endereco.setText(null);
ip_cpf.setText(null);
ip_telefone.setText(null);
estados.setSelectedItem(null);
}
// Ações do botão confirmar
if (e.getSource() == confirmar) {
Scanner scan1 = new Scanner(ip_nome.getText());
Scanner scan2 = new Scanner(ip_endereco.getText());
Scanner scan3 = new Scanner(ip_cpf.getText());
Scanner scan4 = new Scanner(ip_telefone.getText());
Scanner scan5 = new Scanner(String.valueOf(estados
.getSelectedItem()));
PrintStream ps;
try {
ps = new PrintStream("Cadastro.txt");
while (scan1.hasNextLine()) {
ps.println(ps + (scan1.nextLine()));
}
while (scan2.hasNextLine()) {
ps.println(ps + (scan2.nextLine()));
}
while (scan3.hasNextLine()) {
ps.println(ps + (scan3.nextLine()));
}
while (scan4.hasNextLine()) {
ps.println(ps + (scan4.nextLine()));
}
while (scan5.hasNextLine()) {
ps.println(ps + (scan5.nextLine()));
}
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
codigo++;
JOptionPane.showMessageDialog(null, "Cadastro Nº: " + codigo
+ " efetuado com sucesso");
int resposta = JOptionPane.showConfirmDialog(null,
"Deseja Continuar?");
ip_nome.setText(null);
ip_endereco.setText(null);
ip_cpf.setText(null);
ip_telefone.setText(null);
estados.setSelectedItem("");
if ((resposta == JOptionPane.NO_OPTION)
|| (resposta == JOptionPane.CANCEL_OPTION)) {
System.exit(0);
}
}
}
}
public static void main(String[] args) throws IOException, ParseException {
new TelaPrincipal();
}
}
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.text.ParseException;
import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.text.MaskFormatter;
public class Login extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
JLabel login;
JLabel senha;
JTextField ip_login;
JPasswordField ip_senha;
JButton confirma;
JButton limpar;
JMenuBar barraMenu;
public Login() throws ParseException {
// Menu
barraMenu = new JMenuBar();
JMenu opcoes = new JMenu("Opções");
barraMenu.add(opcoes);
JMenuItem cadastrar = new JMenuItem("Cadastrar Novo Usuário");
cadastrar.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
new TelaPrincipal();
} catch (IOException e1) {
e1.printStackTrace();
} catch (ParseException e1) {
e1.printStackTrace();
}
}
});
JMenuItem sair = new JMenuItem("Sair");
sair.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
opcoes.add(cadastrar);
opcoes.add(sair);
// Definindo Elementos
login = new JLabel("Usuário:");
senha = new JLabel("Senha:");
MaskFormatter formLogin = new MaskFormatter("***************");
ip_login = new JFormattedTextField(formLogin);
ip_senha = new JPasswordField();
ip_login.setCaretPosition(0);
ip_senha.setCaretPosition(0);
formLogin
.setValidCharacters("qwertyuiopasdfghjklçzxcvbnmMNBVCXZASDFGHJKLÇPOIUYTREWQ0123456789_*&!@#");
// Botões
confirma = new JButton("OK");
limpar = new JButton("Limpar");
limpar.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ip_login.setText(null);
ip_senha.setText(null);
}
});
// Definindo Panel
getContentPane().setLayout(null);
setTitle("Login");
setVisible(true);
setLocation(500, 300);
setResizable(false);
setSize(300, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Adicionando ao panel
getContentPane().add(login);
getContentPane().add(ip_login);
getContentPane().add(senha);
getContentPane().add(ip_senha);
getContentPane().add(limpar);
getContentPane().add(confirma);
getContentPane().add(barraMenu);
// Posição (X , Y ,Largura,Altura)
login.setBounds(5, 50, 50, 40);
ip_login.setBounds(70, 57, 150, 20);
senha.setBounds(5, 80, 50, 40);
ip_senha.setBounds(70, 87, 150, 20);
limpar.setBounds(20, 150, 80, 50);
confirma.setBounds(150, 150, 80, 50);
barraMenu.setBounds(1, 1, 300, 20);
}
public static void main(String[] args) throws ParseException {
new Login();
}
}
OBS:minha unica duvida é essa, eu sei que tem outros erros no código, mas se n forem relacionados a este problema por favor não citem, esse programa é só um teste meu

