Skip to main content
correções ortograficas , remoção de desculpas desnecessarias ao código e a pergunta
Fonte Link

Sou iniciante nessa área, e estou com um pequeno probleminha, se poderes me ajudar agradeço.. Tenho dois Frames um é principal (P) e o outro (B) eu chamo através de um botão. na tela principal tenho dois botões cadastrar e acessar, quando eu executo a tela principal aparece o botão cadastrar não aparece (isso é intencional) ficando visível apenas o botão acessar, quando clicar no botão acessar ele chama o frame (B) que por fim tem dois botões confirmar e cancelar, ao clicar no botão confirmar ele retorna ao frame principal (P), dessa vez com o botão cadastrar visível, mas não tenho ideia de como posso usar o botão cadastrar do frame (P) no frame (B).

Tenho dois Frames um é principal (P) e o outro (B) eu chamo através de um botão. Na tela principal tenho dois botões cadastrar e acessar, quando eu executo a tela principal aparece, o botão cadastrar não aparece (isso é intencional) ficando visível apenas o botão acessar, quando clicar no botão acessar ele chama o frame (B) que por fim tem dois botões, confirmar e cancelar, ao clicar no botão confirmar ele retorna ao frame principal (P), dessa vez com o botão cadastrar visível, mas não tenho ideia de como posso usar o botão cadastrar do frame (P) no frame (B).

Vou enviar o código completo como estou fazendo no netbeans vai ficar um pouco longo.. Essa é a classe Principal qque quando clicar no botão Acessar vai abrir a tela do frame B.

Então, observa q no primeiro frame eu deixei o botão cadastrar invisivel e quando eu clicar em acessar vai chamar a tela do frameB1 e quando eu acionar o botão confirmar vai fechar a janela do frameB1 chamando de volta o frame principal ja com o botão cadastrar visível. mas eu não estou conseguindo fazer isso...

Desculpe está te alugandoEntão, observa que no primeiro frame eu deixei o botão cadastrar invisível e quando eu clicar em acessar vai chamar a tela do frameB1,e quando eu acionar o botão confirmar vai fechar a janela do frameB1 chamando de volta o frame principal já com o botão cadastrar visível. mas eu queria aprender essa partenão estou conseguindo fazer isso...

Sou iniciante nessa área, e estou com um pequeno probleminha, se poderes me ajudar agradeço.. Tenho dois Frames um é principal (P) e o outro (B) eu chamo através de um botão. na tela principal tenho dois botões cadastrar e acessar, quando eu executo a tela principal aparece o botão cadastrar não aparece (isso é intencional) ficando visível apenas o botão acessar, quando clicar no botão acessar ele chama o frame (B) que por fim tem dois botões confirmar e cancelar, ao clicar no botão confirmar ele retorna ao frame principal (P), dessa vez com o botão cadastrar visível, mas não tenho ideia de como posso usar o botão cadastrar do frame (P) no frame (B).

Vou enviar o código completo como estou fazendo no netbeans vai ficar um pouco longo.. Essa é a classe Principal q quando clicar no botão Acessar vai abrir a tela do frame B.

Então, observa q no primeiro frame eu deixei o botão cadastrar invisivel e quando eu clicar em acessar vai chamar a tela do frameB1 e quando eu acionar o botão confirmar vai fechar a janela do frameB1 chamando de volta o frame principal ja com o botão cadastrar visível. mas eu não estou conseguindo fazer isso...

Desculpe está te alugando mas eu queria aprender essa parte..

Sou iniciante nessa área, e estou com um pequeno probleminha, se poderes me ajudar agradeço..

Tenho dois Frames um é principal (P) e o outro (B) eu chamo através de um botão. Na tela principal tenho dois botões cadastrar e acessar, quando eu executo a tela principal aparece, o botão cadastrar não aparece (isso é intencional) ficando visível apenas o botão acessar, quando clicar no botão acessar ele chama o frame (B) que por fim tem dois botões, confirmar e cancelar, ao clicar no botão confirmar ele retorna ao frame principal (P), dessa vez com o botão cadastrar visível, mas não tenho ideia de como posso usar o botão cadastrar do frame (P) no frame (B).

Vou enviar o código completo como estou fazendo no netbeans vai ficar um pouco longo.. Essa é a classe Principal que quando clicar no botão Acessar vai abrir a tela do frame B.

Então, observa que no primeiro frame eu deixei o botão cadastrar invisível e quando eu clicar em acessar vai chamar a tela do frameB1,e quando eu acionar o botão confirmar vai fechar a janela do frameB1 chamando de volta o frame principal já com o botão cadastrar visível. mas eu não estou conseguindo fazer isso...

Formatação; tags editadas
Fonte Link
Victor Stafusa
  • 64,9mil
  • 9
  • 120
  • 189

package javaapplication2;

public class Principal extends javax.swing.JFrame {

package javaapplication2;

public class Principal extends javax.swing.JFrame {
    
    /**
     * Creates new form Principal
     */
    public Principal() {
        initComponents();
        JBCadastrar.setVisible(false);
       
        
    }

    /**
     * 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.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        JBCadastrar = new javax.swing.JButton();
        JBAcessar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        JBCadastrar.setText("Cadastrar");

        JBAcessar.setText("Acessar");
        JBAcessar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                JBAcessarActionPerformed(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()
                .addGap(147, 147, 147)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(JBCadastrar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(JBAcessar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(172, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(106, 106, 106)
                .addComponent(JBCadastrar)
                .addGap(38, 38, 38)
                .addComponent(JBAcessar)
                .addContainerGap(110, Short.MAX_VALUE))
        );

        setSize(new java.awt.Dimension(416, 338));
        setLocationRelativeTo(null);
    }// </editor-fold>                        

    private void JBAcessarActionPerformed(java.awt.event.ActionEvent evt) {                                          
        String args [] = new String [1];
        FrameB1.main(args);
        
    }                                         

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Principal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Principal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Principal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Principal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Principal().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JButton JBAcessar;
    private javax.swing.JButton JBCadastrar;
    // End of variables declaration     
} 

}

package javaapplication2;

public class FrameB1 extends javax.swing.JFrame {

/**
 * Creates new form FrameB1
 */
public FrameB1() {
   package initComponents();
}javaapplication2;

/**
 * This method is called from within the constructorpublic toclass initializeFrameB1 theextends formjavax.
 * WARNING: Do NOT modify this codeswing. The contentJFrame of{
 this method is always
 * regenerated by the Form Editor.
 */
@SuppressWarnings("unchecked")**
// <editor-fold defaultstate="collapsed" desc="Generated Code">     * Creates new form FrameB1
     */
    public FrameB1() {
      
private void initComponents();
 {   }

    JBConfirmar/**
 = new javax  * This method is called from within the constructor to initialize the form.swing
     * WARNING: Do NOT modify this code.JButton The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked");
    JBCancelar// =<editor-fold newdefaultstate="collapsed" javax.swing.JButtondesc="Generated Code">                          
    private void initComponents(); {

    setDefaultCloseOperation(    JBConfirmar = new javax.swing.WindowConstantsJButton();
        JBCancelar = new javax.EXIT_ON_CLOSEswing.JButton();

    JBConfirmar.setText("Confirmar");
    JBConfirmar.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformedsetDefaultCloseOperation(javajavax.awtswing.eventWindowConstants.ActionEvent evt) {
            JBConfirmarActionPerformed(evt);
        }
    }EXIT_ON_CLOSE);

    JBCancelar    JBConfirmar.setText("Cancelar""Confirmar");
    JBCancelar    JBConfirmar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
            JBCancelarActionPerformed    JBConfirmarActionPerformed(evt);
            }
        });

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane()JBCancelar.setLayoutsetText(layout"Cancelar");
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            JBCancelar.addGapaddActionListener(70, 70, 70)
           new java.addComponent(JBConfirmar)
            awt.addGap(76, 76, 76)
            event.addComponentActionListener(JBCancelar)
            .addContainerGap(100, Short.MAX_VALUE)){
    );
    layout.setVerticalGroup(
      public void layout.createParallelGroupactionPerformed(javax.swing.GroupLayout.Alignment.LEADING)
        java.addGroup(layoutawt.createSequentialGroup()
            event.addGap(124, 124,ActionEvent 124evt)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE){
                .addComponentJBCancelarActionPerformed(JBConfirmarevt);
                .addComponent(JBCancelar))}
            .addContainerGap(153, Short.MAX_VALUE))
    });

    setSize(    javax.swing.GroupLayout layout = new javajavax.awtswing.DimensionGroupLayout(416,getContentPane());
 338       getContentPane().setLayout(layout);
    setLocationRelativeTo    layout.setHorizontalGroup(null
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(70, 70, 70)
                .addComponent(JBConfirmar)
                .addGap(76, 76, 76)
                .addComponent(JBCancelar)
                .addContainerGap(100, Short.MAX_VALUE))
        );
}// </editor-fold>       layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(124, 124, 124)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(JBConfirmar)
                    .addComponent(JBCancelar))
                .addContainerGap(153, Short.MAX_VALUE))
        );

private void JBConfirmarActionPerformed(java.awt.event.ActionEvent evt) {                                            
     String args [] = setSize(new String [1];
    Principaljava.mainawt.Dimension(args);
   416, dispose(338));
}              setLocationRelativeTo(null);
    }// </editor-fold>                        

    private void JBCancelarActionPerformedJBConfirmarActionPerformed(java.awt.event.ActionEvent evt) {                                            
        String args [] = new String [1];
        Principal.main(args);
        dispose();
    }                                           

/**
 * @param args theprivate commandvoid lineJBCancelarActionPerformed(java.awt.event.ActionEvent arguments
evt) */
public{ static void main(String args[]) {
    /* Set the Nimbus look and feel */
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">              
    /* If Nimbus  dispose(introduced);
 in Java SE 6)} is not available, stay with the default look and feel.                                
   
    /**
     * For@param detailsargs seethe http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.htmlcommand line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(FrameB1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(FrameB1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(FrameB1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(FrameB1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
    } catch (ClassNotFoundException ex) {//</editor-fold>
   
      java.util.logging.Logger.getLogger(FrameB1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  /* Create }and catchdisplay (InstantiationExceptionthe ex)form {*/
        java.util.loggingawt.LoggerEventQueue.getLoggerinvokeLater(FrameB1.class.getNamenew Runnable()).log(java.util.logging.Level.SEVERE, null, ex);{
    } catch (IllegalAccessException ex     public void run() {
        java.util.logging.Logger.getLogger(        new FrameB1.class.getName()).logsetVisible(java.util.logging.Level.SEVERE, null, extrue);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
    }
    java.util.logging.Logger.getLogger(FrameB1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex  });
    }
    //</editor-fold>

    /*/ CreateVariables anddeclaration display- thedo formnot */
modify    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
    
    private javax.swing.JButton JBCancelar;
    newprivate FrameB1()javax.setVisible(true);swing.JButton JBConfirmar;
    // End of variables }
declaration    });               
}

// Variables declaration - do not modify                     
private javax.swing.JButton JBCancelar;
private javax.swing.JButton JBConfirmar;
// End of variables declaration                   

}

package javaapplication2;

public class Principal extends javax.swing.JFrame {

/**
 * Creates new form Principal
 */
public Principal() {
    initComponents();
    JBCadastrar.setVisible(false);
   
    
}

/**
 * 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.
 */
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {

    JBCadastrar = new javax.swing.JButton();
    JBAcessar = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    JBCadastrar.setText("Cadastrar");

    JBAcessar.setText("Acessar");
    JBAcessar.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            JBAcessarActionPerformed(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()
            .addGap(147, 147, 147)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                .addComponent(JBCadastrar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(JBAcessar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addContainerGap(172, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGap(106, 106, 106)
            .addComponent(JBCadastrar)
            .addGap(38, 38, 38)
            .addComponent(JBAcessar)
            .addContainerGap(110, Short.MAX_VALUE))
    );

    setSize(new java.awt.Dimension(416, 338));
    setLocationRelativeTo(null);
}// </editor-fold>                        

private void JBAcessarActionPerformed(java.awt.event.ActionEvent evt) {                                          
    String args [] = new String [1];
    FrameB1.main(args);
    
}                                         

/**
 * @param args the command line arguments
 */
public static void main(String args[]) {
    /* Set the Nimbus look and feel */
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */
    try {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(Principal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(Principal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(Principal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(Principal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new Principal().setVisible(true);
        }
    });
}

// Variables declaration - do not modify                     
private javax.swing.JButton JBAcessar;
private javax.swing.JButton JBCadastrar;
// End of variables declaration     

}

package javaapplication2;

public class FrameB1 extends javax.swing.JFrame {

/**
 * Creates new form FrameB1
 */
public FrameB1() {
    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.
 */
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {

    JBConfirmar = new javax.swing.JButton();
    JBCancelar = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    JBConfirmar.setText("Confirmar");
    JBConfirmar.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            JBConfirmarActionPerformed(evt);
        }
    });

    JBCancelar.setText("Cancelar");
    JBCancelar.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            JBCancelarActionPerformed(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()
            .addGap(70, 70, 70)
            .addComponent(JBConfirmar)
            .addGap(76, 76, 76)
            .addComponent(JBCancelar)
            .addContainerGap(100, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGap(124, 124, 124)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(JBConfirmar)
                .addComponent(JBCancelar))
            .addContainerGap(153, Short.MAX_VALUE))
    );

    setSize(new java.awt.Dimension(416, 338));
    setLocationRelativeTo(null);
}// </editor-fold>                        

private void JBConfirmarActionPerformed(java.awt.event.ActionEvent evt) {                                            
     String args [] = new String [1];
    Principal.main(args);
    dispose();
}                                           

private void JBCancelarActionPerformed(java.awt.event.ActionEvent evt) {                                           
    dispose();
}                                          

/**
 * @param args the command line arguments
 */
public static void main(String args[]) {
    /* Set the Nimbus look and feel */
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */
    try {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(FrameB1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(FrameB1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(FrameB1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(FrameB1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new FrameB1().setVisible(true);
        }
    });
}

// Variables declaration - do not modify                     
private javax.swing.JButton JBCancelar;
private javax.swing.JButton JBConfirmar;
// End of variables declaration                   

}

package javaapplication2;

public class Principal extends javax.swing.JFrame {
    
    /**
     * Creates new form Principal
     */
    public Principal() {
        initComponents();
        JBCadastrar.setVisible(false);
       
        
    }

    /**
     * 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.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        JBCadastrar = new javax.swing.JButton();
        JBAcessar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        JBCadastrar.setText("Cadastrar");

        JBAcessar.setText("Acessar");
        JBAcessar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                JBAcessarActionPerformed(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()
                .addGap(147, 147, 147)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(JBCadastrar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(JBAcessar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(172, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(106, 106, 106)
                .addComponent(JBCadastrar)
                .addGap(38, 38, 38)
                .addComponent(JBAcessar)
                .addContainerGap(110, Short.MAX_VALUE))
        );

        setSize(new java.awt.Dimension(416, 338));
        setLocationRelativeTo(null);
    }// </editor-fold>                        

    private void JBAcessarActionPerformed(java.awt.event.ActionEvent evt) {                                          
        String args [] = new String [1];
        FrameB1.main(args);
        
    }                                         

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Principal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Principal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Principal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Principal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Principal().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JButton JBAcessar;
    private javax.swing.JButton JBCadastrar;
    // End of variables declaration     
} 
package javaapplication2;

public class FrameB1 extends javax.swing.JFrame {
    
    /**
     * Creates new form FrameB1
     */
    public FrameB1() {
        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.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        JBConfirmar = new javax.swing.JButton();
        JBCancelar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        JBConfirmar.setText("Confirmar");
        JBConfirmar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                JBConfirmarActionPerformed(evt);
            }
        });

        JBCancelar.setText("Cancelar");
        JBCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                JBCancelarActionPerformed(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()
                .addGap(70, 70, 70)
                .addComponent(JBConfirmar)
                .addGap(76, 76, 76)
                .addComponent(JBCancelar)
                .addContainerGap(100, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(124, 124, 124)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(JBConfirmar)
                    .addComponent(JBCancelar))
                .addContainerGap(153, Short.MAX_VALUE))
        );

        setSize(new java.awt.Dimension(416, 338));
        setLocationRelativeTo(null);
    }// </editor-fold>                        

    private void JBConfirmarActionPerformed(java.awt.event.ActionEvent evt) {                                            
        String args [] = new String [1];
        Principal.main(args);
        dispose();
    }                                           

    private void JBCancelarActionPerformed(java.awt.event.ActionEvent evt) {                                           
        dispose();
    }                                          
   
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(FrameB1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(FrameB1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(FrameB1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(FrameB1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>
 
        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new FrameB1().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JButton JBCancelar;
    private javax.swing.JButton JBConfirmar;
    // End of variables declaration                   
}
Fonte Link

Chamar JButon de um JFrame para outro

Sou iniciante nessa área, e estou com um pequeno probleminha, se poderes me ajudar agradeço.. Tenho dois Frames um é principal (P) e o outro (B) eu chamo através de um botão. na tela principal tenho dois botões cadastrar e acessar, quando eu executo a tela principal aparece o botão cadastrar não aparece (isso é intencional) ficando visível apenas o botão acessar, quando clicar no botão acessar ele chama o frame (B) que por fim tem dois botões confirmar e cancelar, ao clicar no botão confirmar ele retorna ao frame principal (P), dessa vez com o botão cadastrar visível, mas não tenho ideia de como posso usar o botão cadastrar do frame (P) no frame (B).

Vou enviar o código completo como estou fazendo no netbeans vai ficar um pouco longo.. Essa é a classe Principal q quando clicar no botão Acessar vai abrir a tela do frame B.

package javaapplication2;

public class Principal extends javax.swing.JFrame {

/**
 * Creates new form Principal
 */
public Principal() {
    initComponents();
    JBCadastrar.setVisible(false);
   
    
}

/**
 * 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.
 */
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {

    JBCadastrar = new javax.swing.JButton();
    JBAcessar = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    JBCadastrar.setText("Cadastrar");

    JBAcessar.setText("Acessar");
    JBAcessar.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            JBAcessarActionPerformed(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()
            .addGap(147, 147, 147)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                .addComponent(JBCadastrar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(JBAcessar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addContainerGap(172, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGap(106, 106, 106)
            .addComponent(JBCadastrar)
            .addGap(38, 38, 38)
            .addComponent(JBAcessar)
            .addContainerGap(110, Short.MAX_VALUE))
    );

    setSize(new java.awt.Dimension(416, 338));
    setLocationRelativeTo(null);
}// </editor-fold>                        

private void JBAcessarActionPerformed(java.awt.event.ActionEvent evt) {                                          
    String args [] = new String [1];
    FrameB1.main(args);
    
}                                         

/**
 * @param args the command line arguments
 */
public static void main(String args[]) {
    /* Set the Nimbus look and feel */
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */
    try {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(Principal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(Principal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(Principal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(Principal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new Principal().setVisible(true);
        }
    });
}

// Variables declaration - do not modify                     
private javax.swing.JButton JBAcessar;
private javax.swing.JButton JBCadastrar;
// End of variables declaration     

}

Essa outra é o FrameB1

package javaapplication2;

public class FrameB1 extends javax.swing.JFrame {

/**
 * Creates new form FrameB1
 */
public FrameB1() {
    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.
 */
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {

    JBConfirmar = new javax.swing.JButton();
    JBCancelar = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    JBConfirmar.setText("Confirmar");
    JBConfirmar.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            JBConfirmarActionPerformed(evt);
        }
    });

    JBCancelar.setText("Cancelar");
    JBCancelar.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            JBCancelarActionPerformed(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()
            .addGap(70, 70, 70)
            .addComponent(JBConfirmar)
            .addGap(76, 76, 76)
            .addComponent(JBCancelar)
            .addContainerGap(100, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGap(124, 124, 124)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(JBConfirmar)
                .addComponent(JBCancelar))
            .addContainerGap(153, Short.MAX_VALUE))
    );

    setSize(new java.awt.Dimension(416, 338));
    setLocationRelativeTo(null);
}// </editor-fold>                        

private void JBConfirmarActionPerformed(java.awt.event.ActionEvent evt) {                                            
    String args [] = new String [1];
    Principal.main(args);
    dispose();
}                                           

private void JBCancelarActionPerformed(java.awt.event.ActionEvent evt) {                                           
    dispose();
}                                          

/**
 * @param args the command line arguments
 */
public static void main(String args[]) {
    /* Set the Nimbus look and feel */
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */
    try {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(FrameB1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(FrameB1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(FrameB1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(FrameB1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new FrameB1().setVisible(true);
        }
    });
}

// Variables declaration - do not modify                     
private javax.swing.JButton JBCancelar;
private javax.swing.JButton JBConfirmar;
// End of variables declaration                   

}

Então, observa q no primeiro frame eu deixei o botão cadastrar invisivel e quando eu clicar em acessar vai chamar a tela do frameB1 e quando eu acionar o botão confirmar vai fechar a janela do frameB1 chamando de volta o frame principal ja com o botão cadastrar visível. mas eu não estou conseguindo fazer isso...

Desculpe está te alugando mas eu queria aprender essa parte..