-1

I have a JFrame and 5 different JPanel and a NEXT button. I want to use the NEXT button to move to each JPanel but it does not work. When I click NEXT One time, it goes to the second JPanel. However, it does not go to the third, fifth and fourth JPanel. Here is my code:

    public class MG extends JFrame{
GridBagLayout layout = new GridBagLayout();
Question1 q1;
Question2 q2;
Question3 q3;
public MG() {
    initComponents();
    setLocationRelativeTo(null);
    q1 = new Question1();
    q2 = new Question2();
    q3 = new Question3();
    DynamicPanel.setLayout(layout);
    GridBagConstraints c = new GridBagConstraints();
    c.gridx =0;
    c.gridy =0;
    DynamicPanel.add(q1,c);
    c.gridx=0;
    c.gridy=0;
    DynamicPanel.add(q2,c);
    c.gridx=0;
    c.gridy=0;
    DynamicPanel.add(q3,c);
    q1.setVisible(true);
    q2.setVisible(false);
    q3.setVisible(false);
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {

    btnNext = new javax.swing.JButton();
    jButton6 = new javax.swing.JButton();
    jLabel3 = new javax.swing.JLabel();
    DynamicPanel = new javax.swing.JPanel();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    setSize(new java.awt.Dimension(1200, 839));

    btnNext.setText("Next");
    btnNext.addMouseListener(new java.awt.event.MouseAdapter() {
        public void mouseClicked(java.awt.event.MouseEvent evt) {
            btnNextMouseClicked(evt);
        }
    });

    jButton6.setBackground(new java.awt.Color(255, 51, 51));
    jButton6.setText("Exit");
    jButton6.addMouseListener(new java.awt.event.MouseAdapter() {
        public void mouseClicked(java.awt.event.MouseEvent evt) {
            jButton6MouseClicked(evt);
        }
    });

    jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/120725oda_top.jpg"))); // NOI18N

    DynamicPanel.setBackground(new java.awt.Color(255, 255, 255));

    javax.swing.GroupLayout DynamicPanelLayout = new javax.swing.GroupLayout(DynamicPanel);
    DynamicPanel.setLayout(DynamicPanelLayout);
    DynamicPanelLayout.setHorizontalGroup(
        DynamicPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 929, Short.MAX_VALUE)
    );
    DynamicPanelLayout.setVerticalGroup(
        DynamicPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 547, Short.MAX_VALUE)
    );

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(DynamicPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(31, 31, 31)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(btnNext, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(jButton6, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGap(56, 56, 56))
        .addGroup(layout.createSequentialGroup()
            .addGap(194, 194, 194)
            .addComponent(jLabel3)
            .addContainerGap(301, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
            .addContainerGap()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(0, 0, Short.MAX_VALUE)
                    .addComponent(btnNext, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(39, 39, 39)
                    .addComponent(jButton6, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(55, 55, 55))
                .addGroup(layout.createSequentialGroup()
                    .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 221, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(18, 18, 18)
                    .addComponent(DynamicPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(0, 28, Short.MAX_VALUE))))
    );

    setBounds(0, 0, 1218, 867);
}// </editor-fold>                        

private void btnNextMouseClicked(java.awt.event.MouseEvent evt) {                                     
    boolean b = true;

    q1.setVisible(false);
    q2.setVisible(true);
    q3.setVisible(false);
}                                    

private void jButton6MouseClicked(java.awt.event.MouseEvent evt) {                                      
    Hi form = new Hi();
    MG.this.dispose();
    form.setVisible(true);
}                                     



public 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(MainGame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(MainGame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(MainGame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(MainGame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(() -> {
        new MG().setVisible(true);
    });
}

// Variables declaration - do not modify                     
public javax.swing.JPanel DynamicPanel;
private javax.swing.JButton btnNext;
private javax.swing.JButton jButton6;
private javax.swing.JLabel jLabel3;
// End of variables declaration                   

}

  • 1
    Looks like you don't even know what your code is doing, since this is a generated code from Netbeans/Matisse GUI Builder. Try to understand first and then you ask here in SO – Diogo Moreira Feb 16 '17 at 16:32
  • How often do we get asked this question ... [for example](http://stackoverflow.com/questions/27663306/open-a-jpanel-after-pressing-a-button-in-a-jframe/27663749#27663749), [example](http://stackoverflow.com/questions/29571722/java-application-with-multiple-scenes/29639672#29639672), [example](http://stackoverflow.com/questions/35470930/is-this-jpanel-code-good-not-showing-the-other-jpanel-when-clicked-on-the-butto/35471323#35471323) – MadProgrammer Feb 16 '17 at 20:52

1 Answers1

3

You can use a Card Layout to swap panels. Read the tutorial and download the working example to get started.

You may also want to check out Card Layout Actions. It is an extension of CardLayout that allows you to easily create and use "Next" and "Previous" buttons to move between panels.

camickr
  • 321,443
  • 19
  • 166
  • 288