import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.SwingUtilities;public class Principal extends JFrame { public Principal() { super("Principal"); // Sai do aplicativo ao clicar no botão Fechar setDefaultCloseOperation(EXIT_ON_CLOSE); JButton abrirSecundario = new JButton("Abrir Secundário"); abrirSecundario.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub setEnabled(false); new Secundario().setVisible(true); } }); getContentPane().add(abrirSecundario); pack(); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { // TODO Auto-generated method stub new Principal().setVisible(true); } }); }}/* A classe Secundario começa aqui */import java.awt.Frame;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.WindowEvent;import java.awt.event.WindowListener;import javax.swing.JButton;import javax.swing.JFrame;public class Secundario extends JFrame { public Secundario() { super("Secundário"); // Remove o frame da memória ao clicar no botão Fechar setDefaultCloseOperation(DO_NOTHING_ON_CLOSE); // Como não há ação padrão de fechar, deve-se implementar a ação addWindowListener(new WindowListener() { @Override public void windowActivated(WindowEvent arg0) { // TODO Auto-generated method stub } @Override public void windowClosed(WindowEvent arg0) { // TODO Auto-generated method stub } // Este é o método que importa para o botão Fechar @Override public void windowClosing(WindowEvent arg0) { // TODO Auto-generated method stub fecharSecundario(); } @Override public void windowDeactivated(WindowEvent arg0) { // TODO Auto-generated method stub } @Override public void windowDeiconified(WindowEvent arg0) { // TODO Auto-generated method stub } @Override public void windowIconified(WindowEvent arg0) { // TODO Auto-generated method stub } @Override public void windowOpened(WindowEvent arg0) { // TODO Auto-generated method stub } }); JButton fechar = new JButton("Fechar"); fechar.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub fecharSecundario(); } }); getContentPane().add(fechar); pack(); } private void fecharSecundario() { /* Obtém a lista de janelas abertas pelo aplicativo. * CUIDADO: Este método pode retornar janelas que não estão associadas * ao seu aplicativo. */ Frame[] frames = getFrames(); // Localiza a janela principal pelo título e habilita-a novamente. for (int count = 0; count < frames.length; count++) { if (frames[count].getTitle().equals("Principal")) { frames[count].setEnabled(true); } } dispose(); }}