So I'm new in Java programming and have a problem. When I try to switch to a specific panel using .show(), nothing happens, but when I use .next(), it works. I need to switch to a panel called game1 using a button on my MainPanel panel. This is what I have:
private void game1buttonActionPerformed(java.awt.event.ActionEvent evt) {
CardLayout cardLayout = (CardLayout)(MainPanel.getLayout());
cardLayout.show(MainPanel, ""+game1);
}
Here is my full code: *Note: I'm using NetBeans to save time.
package card;
import java.awt.CardLayout;
public class CardTester extends javax.swing.JFrame {
/**
* Creates new form CardTester
*/
public CardTester() {
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() {
MainPanel = new javax.swing.JPanel();
MainMenu = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
game1button = new javax.swing.JButton();
game2button = new javax.swing.JButton();
game3button = new javax.swing.JButton();
game1 = new javax.swing.JPanel();
jLabel2 = new javax.swing.JLabel();
jButton1 = new javax.swing.JButton();
jTextField1 = new javax.swing.JTextField();
game2 = new javax.swing.JPanel();
jLabel3 = new javax.swing.JLabel();
game2menubutton = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
jList1 = new javax.swing.JList();
game3 = new javax.swing.JPanel();
jLabel4 = new javax.swing.JLabel();
jButton2 = new javax.swing.JButton();
jRadioButton1 = new javax.swing.JRadioButton();
jRadioButton2 = new javax.swing.JRadioButton();
jRadioButton3 = new javax.swing.JRadioButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
MainPanel.setLayout(new java.awt.CardLayout());
jLabel1.setText("Main Menu");
game1button.setText("Game 1");
game1button.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
game1buttonActionPerformed(evt);
}
});
game2button.setText("Game 2");
game2button.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
game2buttonActionPerformed(evt);
}
});
game3button.setText("Game 3");
javax.swing.GroupLayout MainMenuLayout = new javax.swing.GroupLayout(MainMenu);
MainMenu.setLayout(MainMenuLayout);
MainMenuLayout.setHorizontalGroup(
MainMenuLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(MainMenuLayout.createSequentialGroup()
.addGroup(MainMenuLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(MainMenuLayout.createSequentialGroup()
.addGap(138, 138, 138)
.addComponent(jLabel1))
.addGroup(MainMenuLayout.createSequentialGroup()
.addGap(19, 19, 19)
.addGroup(MainMenuLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(game3button, javax.swing.GroupLayout.DEFAULT_SIZE, 287, Short.MAX_VALUE)
.addComponent(game2button, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(game1button, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
.addContainerGap(24, Short.MAX_VALUE))
);
MainMenuLayout.setVerticalGroup(
MainMenuLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(MainMenuLayout.createSequentialGroup()
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 18, Short.MAX_VALUE)
.addComponent(game1button)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(game2button)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(game3button)
.addContainerGap())
);
MainPanel.add(MainMenu, "card2");
jLabel2.setText("Game 1");
jButton1.setText("menu");
jTextField1.setText("This is game number 1.");
jTextField1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jTextField1ActionPerformed(evt);
}
});
javax.swing.GroupLayout game1Layout = new javax.swing.GroupLayout(game1);
game1.setLayout(game1Layout);
game1Layout.setHorizontalGroup(
game1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(game1Layout.createSequentialGroup()
.addComponent(jButton1)
.addGap(61, 61, 61)
.addComponent(jLabel2)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, game1Layout.createSequentialGroup()
.addGap(0, 50, Short.MAX_VALUE)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 238, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(42, 42, 42))
);
game1Layout.setVerticalGroup(
game1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(game1Layout.createSequentialGroup()
.addGroup(game1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel2)
.addComponent(jButton1))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 70, Short.MAX_VALUE)
.addGap(18, 18, 18))
);
MainPanel.add(game1, "card3");
jLabel3.setText("Game 2");
game2menubutton.setText("menu");
jList1.setModel(new javax.swing.AbstractListModel() {
String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
public int getSize() { return strings.length; }
public Object getElementAt(int i) { return strings[i]; }
});
jScrollPane1.setViewportView(jList1);
javax.swing.GroupLayout game2Layout = new javax.swing.GroupLayout(game2);
game2.setLayout(game2Layout);
game2Layout.setHorizontalGroup(
game2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(game2Layout.createSequentialGroup()
.addComponent(game2menubutton)
.addGap(82, 82, 82)
.addGroup(game2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel3))
.addContainerGap(153, Short.MAX_VALUE))
);
game2Layout.setVerticalGroup(
game2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(game2Layout.createSequentialGroup()
.addGroup(game2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(game2menubutton)
.addComponent(jLabel3))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE))
);
MainPanel.add(game2, "card4");
jLabel4.setText("Game 3");
jButton2.setText("menu");
jRadioButton1.setText("jRadioButton1");
jRadioButton2.setText("jRadioButton2");
jRadioButton3.setText("jRadioButton3");
javax.swing.GroupLayout game3Layout = new javax.swing.GroupLayout(game3);
game3.setLayout(game3Layout);
game3Layout.setHorizontalGroup(
game3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(game3Layout.createSequentialGroup()
.addGroup(game3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(game3Layout.createSequentialGroup()
.addComponent(jButton2)
.addGap(73, 73, 73)
.addComponent(jLabel4))
.addGroup(game3Layout.createSequentialGroup()
.addGap(102, 102, 102)
.addGroup(game3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jRadioButton2)
.addComponent(jRadioButton1)
.addComponent(jRadioButton3))))
.addContainerGap(135, Short.MAX_VALUE))
);
game3Layout.setVerticalGroup(
game3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(game3Layout.createSequentialGroup()
.addGroup(game3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel4)
.addComponent(jButton2))
.addGap(18, 18, 18)
.addComponent(jRadioButton1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jRadioButton2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jRadioButton3)
.addGap(0, 12, Short.MAX_VALUE))
);
MainPanel.add(game3, "card5");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(MainPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(MainPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
pack();
}// </editor-fold>
private void game2buttonActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void game1buttonActionPerformed(java.awt.event.ActionEvent evt) {
CardLayout cardLayout = (CardLayout)(MainPanel.getLayout());
cardLayout.show(MainPanel, ""+game1);
}
private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
/**
* @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(CardTester.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(CardTester.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(CardTester.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(CardTester.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 CardTester().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JPanel MainMenu;
private javax.swing.JPanel MainPanel;
private javax.swing.JPanel game1;
private javax.swing.JButton game1button;
private javax.swing.JPanel game2;
private javax.swing.JButton game2button;
private javax.swing.JButton game2menubutton;
private javax.swing.JPanel game3;
private javax.swing.JButton game3button;
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JList jList1;
private javax.swing.JRadioButton jRadioButton1;
private javax.swing.JRadioButton jRadioButton2;
private javax.swing.JRadioButton jRadioButton3;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextField jTextField1;
// End of variables declaration
}
Edit: I solved this by adding the name 'game1' to the panel 'game1'. This associates 'game1' with the panel. My mistake was that in this:
cardLayout.show(MainPanel, "game1");
the 'game1' was the panel name and not the card name.
When you add the child panel to his parent, you do this:
MainPanel.add(game1, "card3");
"card3" is the name you must use to refer to that child panel, so then you must use:
cardLayout.show(MainPanel, "card3");