Sunday, 8 September 2013

Unable to switch cards in cardlayout

Unable to switch cards in cardlayout

I've been working on a program with multiple cards - a roleplaying
character generator. I have four cards, the first being a menu card, and
the other three each doing a different part of the character generation.
However, I'm having issues with the menu card switching to different
cards.
I want it to, when the user presses "New Character", to switch to the
first character card, CardChar. The code as it stands is below:
public class mabGUI implements MouseListener {
final static String Menu = "Menu";
final static String CharCreation = "Character Creation";
final static String CharSkills = "Character Skills Screen";
final static String CharEquip = "Character Equipment";
private JPanel panel;
public void paneCreation(Container Pane) {
JPanel cardMenu = new JPanel();
JPanel cardChar = new JPanel();
JPanel cardSkills = new JPanel();
JPanel cardEquip = new JPanel();
panel = new JPanel(new CardLayout());
panel.add(cardMenu, Menu);
cardMenu.setBackground(new Color(210, 180, 140));
panel.add(cardChar, CharCreation);
cardChar.setBackground(new Color(210, 100, 140));
panel.add(cardSkills, CharSkills);
panel.add(cardEquip, CharEquip);
Pane.add(panel, BorderLayout.CENTER);
//CardMenu
cardMenu.setLayout(null);
JButton btnNew = new JButton("New Character");
btnNew.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
System.out.println("beep");
CardLayout cl = (CardLayout) (panel.getLayout());
cl.show(panel, "CharCreation");
}
});
btnNew.setBounds(20, 20, 150, 30);
cardMenu.add(btnNew);
}
public void initialise() {
JFrame frame = new JFrame("MBCharGen");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setMinimumSize(new Dimension(800, 600));
mabGUI MBCG = new mabGUI();
MBCG.paneCreation(frame.getContentPane());
frame.pack();
frame.setVisible(true);
}
I have it set up so that the main class activates the initialise function,
which then creates the required gui items.
I've tried declaring the card layout in the class, switching from show to
setvisible, and had a look at the various card layout examples out there,
and I'm getting nothing. The mouse listener event is working, because the
button is returning "beep" each time it's pressed.
Any ideas what I'm screwing up? I'm pretty sure it's going to be something
tiny I've missed, but I'm having difficulties seeing it.

No comments:

Post a Comment