如何制作一个NEXT按钮,移动到Jframe中的下一个Jpanel

时间:2017-02-16 16:11:22

标签: java swing

我有一个JFrame和5个不同的JPanel和一个NEXT按钮。我想使用NEXT按钮移动到每个JPanel但它不起作用。当我单击NEXT一次时,它会转到第二个JPanel。但是,它没有进入第三,第五和第四个JPanel。这是我的代码:

    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 个答案:

答案 0 :(得分:3)

您可以使用Card Layout来交换面板。阅读教程并下载工作示例以开始使用。

您可能还想查看Card Layout Actions。它是CardLayout的扩展,允许您轻松创建和使用&#34; Next&#34;和&#34;以前&#34;按钮在面板之间移动。