setVisible

7 respostas
O

Hey Pessoal:

Tenho um Jbutton numa JFrame que chama outra

AdicionarCd frm = new AdicionarCd();
            frm.setVisible(true);

Depois ao fazer Run da primeira JFrame ele abre-me o outro, até aqui tudo bem.

Para porque é que quando fecho a JFrame que ele me abriu, ele fechame todo o projecto?

Pensei que podia fazer:

AdicionarCd frm = new AdicionarCd();
frm.dispose();

Num botao deste segundo JFrame, mas ele nao fecha.

7 Respostas

maruero

Cara certamente quando vc fechar uma jframe e ela fecha toda a aplicação é pq ela está setada pra funcionar dessa maneira… não acho outro jeito de isso acontecer, mas por via das duvidas poste o codigo inteiro da jframe que vc criou (AdicionarCd).

O
package fichapratica9;

/**
 *
 * @author  Administrador
 */
public class AdicionarCd extends javax.swing.JFrame {
    Biblioteca bib = new Biblioteca(); 
    Cd cdx1 = new Cd();
    /** Creates new form AdicionarCd */
    public AdicionarCd() {
        initComponents();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">                          
    private void initComponents() {
        jLabel1 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jTextField2 = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        jLabel1.setText("Adicionar CD");

        jLabel2.setText("Titulo:");

        jLabel3.setText("Artista:");

        jButton1.setText("Inserir");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jTextField2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 240, Short.MAX_VALUE)
                            .addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 240, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(113, 113, 113)
                        .addComponent(jLabel1))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(112, 112, 112)
                        .addComponent(jButton1)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(23, 23, 23)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(20, 20, 20)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 29, Short.MAX_VALUE)
                .addComponent(jButton1)
                .addGap(26, 26, 26))
        );
        pack();
    }// </editor-fold>                        

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
       cdx1.setArtista(jTextField1.getText());
       cdx1.setTitulo(jTextField2.getText());
       bib.enterCd(cdx1);
    }                                        
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new AdicionarCd().setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify                     
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    // End of variables declaration                   
    
}
O

nao percebi muito bem a forma de implementar isso

O

Obrigado pessoal está a funcionar Abraço

Joao

Luiz_Rocha

Opa, companheiro!

Essa é simples: existe um método que eu uso na JFrame chamado

setDefaultCloseOperation(int i)

Use esse método no construtor da sua JFrame para mudar a configuração default para o que ela faz quando vc clica no X. Para isso envie como argumento a constante que vc quiser, dentre estas:

javax.swing.WindowConstants
DISPOSE_ON_CLOSE ou 2 // ?
DO_NOTHING_ON_CLOSE ou 0 // vc já sabe
EXIT_ON_CLOSE ou 3 // fecha todo o projeto
HIDE_ON_CLOSE ou 1 //esconde, mas conserva os atributos

Testa aí e vê se dá certo.

Boa sorte!
:smiley: :smiley: :smiley:

Luiz_Rocha

Por exemplo:

//import java. “as coisa aí”.*;

public class Janela extends JFrame
{

//atributos…

public Janela()//método construtor

{

setTitle(Janela Sem Futuro);

setSize(1,1);

setLocation(50,50);

//(…)

//esconder janela ao clicar no X:

setDefaultCloseOperation(HIDE_ON_CLOSE);

//ou: fazer alguma coisa q eu n lembro:

setDefaultCloseOperation(DISPOSE_ON_CLOSE);

//ou: fechar tudo ao clicar no x:

setDefaultCloseOperation(EXIT_ON_CLOSE);

//ou: fazer nada ao clicar no x:

setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);

(…)

}//fim do construtor

//(…)

}//fim da classe

ou então

//import …

public class ClasseQualquerComObjetoDeJFrame
{

JFrame janela;

public ClasseQualquerComObjetoDeJFrame()

{

janela=new JFrame(Janela com menos futuro ainda);

janela.setSize(9999999,99999999);

janela.setLocation(la na China);

janela.setDefaultCloseOperation(HIDE_ON_CLOSE);

//(…)

}//fim do construtor

//(…)

}//fim final

Espero ter ajudado melhor agora.

Continue com boa sorte.

Luiz_Rocha

Pronto, melhor: no teu caso, eu botava o seguinte:

(isso abaixo foi retirado do teu código)

public class AdicionarCd extends javax.swing.JFrame {

Biblioteca bib = new Biblioteca();

Cd cdx1 = new Cd();

/** Creates new form AdicionarCd */

public AdicionarCd() {

initComponents();

this.setDefaultCloseOperation(HIDE_ON_CLOSE);

// n.b.: acho que fica melhor com DISPOSE_ON_CLOSE, melhor tentar com oos dois.Oo “this.” é opcional.

}
Criado 12 de dezembro de 2006
Ultima resposta 13 de dez. de 2006
Respostas 7
Participantes 3