Metodo dispose() no JFrame não faz nada!

2 respostas
gregorioarthur
import javax.swing.*;
import java.awt.event.*;

public class TelaPrincipal extends JFrame {

    private static TelaPrincipal TPr = new TelaPrincipal();
    private static TelaCadastrar TCad = new TelaCadastrar();
    private static TelaExclusao TEx = new TelaExclusao();
    private static TelaAlteracao TAlt = new TelaAlteracao();
    private static TelaInformacoes TInf = new TelaInformacoes();

    public TelaPrincipal() {
        initComponents();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        Logo = new javax.swing.JLabel();
        BarradeMenu = new javax.swing.JMenuBar();
        MenuPrincipal = new javax.swing.JMenu();
        MCadastros = new javax.swing.JMenuItem();
        MAlteraCad = new javax.swing.JMenuItem();
        MExcluiCad = new javax.swing.JMenuItem();
        MSair = new javax.swing.JMenuItem();
        MenuRelatorios = new javax.swing.JMenu();
        MRelTp1 = new javax.swing.JMenuItem();
        MRelTp2 = new javax.swing.JMenuItem();
        MenuAjuda = new javax.swing.JMenu();
        MComoUsar = new javax.swing.JMenuItem();
        MSobre = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle(".:: SCC - Sistema de Controle de Clientes ::. ");
        setMinimumSize(new java.awt.Dimension(800, 600));
        setName("TelaPrincipal"); // NOI18N
        setResizable(false);

        Logo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/IconesImagens/SCC - Logo.png"))); // NOI18N

        MenuPrincipal.setIcon(new javax.swing.ImageIcon(getClass().getResource("/IconesImagens/MPri_ico.png"))); // NOI18N
        MenuPrincipal.setText("Principal");

        MCadastros.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_1, java.awt.event.InputEvent.ALT_MASK));
        MCadastros.setIcon(new javax.swing.ImageIcon(getClass().getResource("/IconesImagens/Add_Cad_ico.png"))); // NOI18N
        MCadastros.setText("Cadastrar");
        MCadastros.setToolTipText("Cadastra um Novo Cliente");
        MCadastros.addActionListener(new ActionListener() {   
            public void actionPerformed(ActionEvent e) {  
                TCad.setVisible(true);
            }
        });
        MenuPrincipal.add(MCadastros);

        MAlteraCad.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_2, java.awt.event.InputEvent.ALT_MASK));
        MAlteraCad.setIcon(new javax.swing.ImageIcon(getClass().getResource("/IconesImagens/Edit_Cad_ico.png"))); // NOI18N
        MAlteraCad.setText("Alterar / Consultar");
        MAlteraCad.setToolTipText("Altera ou Consulta um Cadastro");
        MAlteraCad.addActionListener(new ActionListener() {   
            public void actionPerformed(ActionEvent e) {  
                TAlt.setVisible(true);
            }
        });
        MenuPrincipal.add(MAlteraCad);

        MExcluiCad.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_3, java.awt.event.InputEvent.ALT_MASK));
        MExcluiCad.setIcon(new javax.swing.ImageIcon(getClass().getResource("/IconesImagens/Del_Cad_ico.png"))); // NOI18N
        MExcluiCad.setText("Excluir Existente");
        MExcluiCad.setToolTipText("Exclui um Cadastro Existente");
        MExcluiCad.addActionListener(new ActionListener() {   
            public void actionPerformed(ActionEvent e) {  
                TEx.setVisible(true);
            }
        });
        MenuPrincipal.add(MExcluiCad);

        MSair.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F12, 0));
        MSair.setIcon(new javax.swing.ImageIcon(getClass().getResource("/IconesImagens/Out_ico.png"))); // NOI18N
        MSair.setText("Sair");
        MSair.setToolTipText("Sai do Programa");
        MSair.addActionListener(new ActionListener() {       // Aqui começa o AcitonListener do botao "Sair"
            public void actionPerformed(ActionEvent e) {      // Mas qundo clica no Botão ele não sai a menos que eu coloque assim:
                TPr.dispose();                                              // TelaPrincipal.this.dispose(); ai funfa normal, so q a aplicação nao termina!!! 
            }                                                                    // Ela continua rodando e gastando memoria....
        });
        MenuPrincipal.add(MSair);

        BarradeMenu.add(MenuPrincipal);

        MenuRelatorios.setIcon(new javax.swing.ImageIcon(getClass().getResource("/IconesImagens/MRel_ico.png"))); // NOI18N
        MenuRelatorios.setText("Relatórios");

        MRelTp1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/IconesImagens/Rel_ico.png"))); // NOI18N
        MRelTp1.setText("Imprimir Todo os Cadastros");
        MenuRelatorios.add(MRelTp1);

        MRelTp2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/IconesImagens/Rel_ico.png"))); // NOI18N
        MRelTp2.setText("Imprimir um Cadastro em específico");
        MenuRelatorios.add(MRelTp2);

        BarradeMenu.add(MenuRelatorios);

        MenuAjuda.setIcon(new javax.swing.ImageIcon(getClass().getResource("/IconesImagens/Help_ico.png"))); // NOI18N
        MenuAjuda.setText("Ajuda");

        MComoUsar.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F1, 0));
        MComoUsar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/IconesImagens/Waht_ico.png"))); // NOI18N
        MComoUsar.setText("Como Usar");
        MComoUsar.setToolTipText("Dá Informações de como usar o Programa");
        MenuAjuda.add(MComoUsar);

        MSobre.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F2, 0));
        MSobre.setIcon(new javax.swing.ImageIcon(getClass().getResource("/IconesImagens/Sob_ico.png"))); // NOI18N
        MSobre.setText("Sobre");
        MSobre.setToolTipText("Dá Informações sonbre o program, Versão e BD..");
        MSobre.addActionListener(new ActionListener() {   
            public void actionPerformed(ActionEvent e) {    
                TInf.setVisible(true);
            }  
        });
        MenuAjuda.add(MSobre);

        BarradeMenu.add(MenuAjuda);

        setJMenuBar(BarradeMenu);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(147, 147, 147)
                .addComponent(Logo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(137, 137, 137))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(136, 136, 136)
                .addComponent(Logo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(157, 157, 157))
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-800)/2, (screenSize.height-600)/2, 800, 600);
    }// </editor-fold>                        

    // Variables declaration - do not modify                     
    private javax.swing.JMenuBar BarradeMenu;
    private javax.swing.JLabel Logo;
    private javax.swing.JMenuItem MAlteraCad;
    private javax.swing.JMenuItem MCadastros;
    private javax.swing.JMenuItem MComoUsar;
    private javax.swing.JMenuItem MExcluiCad;
    private javax.swing.JMenuItem MRelTp1;
    private javax.swing.JMenuItem MRelTp2;
    private javax.swing.JMenuItem MSair;
    private javax.swing.JMenuItem MSobre;
    private javax.swing.JMenu MenuAjuda;
    private javax.swing.JMenu MenuPrincipal;
    private javax.swing.JMenu MenuRelatorios;
    // End of variables declaration                   
}

Acima tem uma breve descrição do problema… mas oq fazer!? se eu por TelaPrincipal.this.dispose(); ela fehca mas a aplicação fica travada e não finaliza, apenas fecha a tela atual, se eu por instanciando um obejeto da própria classe tbm ai ela nem fecha!

oq fazer? alguem pode ajudar? :shock:

2 Respostas

R

Para a propriedade defaultCloseOperation do JFrame, use DO_NOTHING_ON_CLOSE em vez de EXIT_ON_CLOSE, e, no evento windowClosing() do JFrame, use System.exit() para encerrar a JVM.

http://java.sun.com/javase/6/docs/api/java/lang/System.html#exit(int)

gregorioarthur

é, na verdade… se eu fizer isso funfa… mas queria mesmo é com o dispose() lá no meu Jmenu “Sair” pq em todos os outros jframes funciona, so nesse que nao…

por isso achei estranho…

mas vou fazer assim, vou por no evento do WindowClosing, esse SystemExit, pra ele fecha a JVM… ai axo que vai funcionar, vou testar… valeu!

Se alguem tiver outra sugestão… so dizer ai!

Criado 12 de junho de 2009
Ultima resposta 12 de jun. de 2009
Respostas 2
Participantes 2