2017-02-16 8 views
-1

私はJFrameと5つの異なるJPanelとNEXTボタンを持っています。私はそれぞれのJPanelに移動するためにNEXTボタンを使いたいが動作しない。 NEXTを1回クリックすると、2番目のJPanelに移動します。しかし、それは第3、第5、第4 JPanelには及ばない。ここに私のコードは次のとおりです。Jframeの次のJpanelに移動するNEXTボタンの作成方法

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

これは、Netbeans/Matisse GUIビルダーから生成されたコードなので、コードが何をしているのかわからないようです。最初に理解しようとすると、あなたはここで尋ねる –

+0

どのくらいの頻度でこの質問をしましたか... [例えば](http://stackoverflow.com/questions/27663306/open-a-jpanel-after-pressing- a-button-in-a-jframe/27663749#27663749)、[例](http://stackoverflow.com/questions/29571722/java-application-with-multiple-scenes/29639672#29639672)、[例]( http://stackoverflow.com/questions/35470930/is-this-jpanel-code-good-not-showing-the-other-jpanel-when-clicked-on-the-butto/35471323#35471323) – MadProgrammer

答えて

3

あなたは、パネルを交換するCard Layoutを使用することができます。チュートリアルを読み、実際のサンプルをダウンロードして始めてください。

また、Card Layout Actionsをチェックアウトすることもできます。 CardLayoutの拡張で、「次へ」と「前の」ボタンを簡単に作成して使用してパネル間を移動できます。

関連する問題