3

I am having difficulty with CardLayout Manager in my code. I can't figure out why i am getting this exception. I am passing a string in the CardLayout.show() method but still i get this error. Please help. This is my main class.

@SuppressWarnings("serial")
public class Main extends JFrame implements ActionListener {

final static String mainMenuPanel = "Main Menu";
final static String creditsPanel = "Credits";
final static String introPanel = "Introduction";

private CardLayout cardLayout = new CardLayout();
private JPanel cards = new JPanel(cardLayout);


public Main(){
    //Create and set up the window.
    super();
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setLayout(new CardLayout());   
    //this.pack();
    this.setVisible(true);
    this.setSize(new Dimension(800,600));
    this.setLocationRelativeTo(null);
    this.setTitle("Wise Frog Productions.");
    cards.add(new IntroGamePanel(),introPanel);
    cards.add(new MainMenu(),mainMenuPanel);
    this.add(cards);
    swapView(mainMenuPanel);

}
public void swapView(String s){
    cardLayout.show(cards,s);
}
public void actionPerformed(ActionEvent event){

}

public static void main(String[] args){
    javax.swing.SwingUtilities.invokeLater(new Runnable(){
        public void run(){
            new Main();
        }
    });
}

This is my outside class from which i am swapping the card.

public class IntroGamePanel extends JPanel implements MouseInputListener{
private Main main;
ImageIcon beginButtonIcon1 = new ImageIcon(IntroGamePanel.class.getResource("begin_0.gif"));
ImageIcon beginButtonIcon2 = new ImageIcon(IntroGamePanel.class.getResource("begin_1.gif"));
JButton beginButton = new JButton("", beginButtonIcon1);

public IntroGamePanel(){
    super();
    this.setOpaque(true);
    this.add(beginButton);
    this.setPreferredSize(new Dimension(800,600));
    beginButton.setPreferredSize(new Dimension(200,36));
    beginButton.setLocation(240,40);
    beginButton.addMouseMotionListener(this);
    beginButton.addMouseListener(this);
    beginButton.setEnabled(true);
}

@Override
//This will take us to the main menu screen.
public void mouseClicked(MouseEvent e) {    
    if(main != null){
        main.swapView(Main.mainMenuPanel);
    }

}

@Override
public void mouseEntered(MouseEvent e) {
    beginButton.setIcon(beginButtonIcon2);      
}

@Override
public void mouseExited(MouseEvent e) {
    beginButton.setIcon(beginButtonIcon1);
}

@Override
public void mousePressed(MouseEvent e) {
    //not needed
}

@Override
public void mouseReleased(MouseEvent e) {
    //not needed
}

@Override
public void mouseDragged(MouseEvent e) {
    //not needed
}

@Override
public void mouseMoved(MouseEvent e) {
    //not needed
}
public void getMain(Main main){
    this.main = main;
}
}

I need some help regarding this quite urgently actually. :(

kleopatra
  • 51,061
  • 28
  • 99
  • 211
Jha
  • 54
  • 1
  • 5

1 Answers1

6

The error comes from the line

this.add(cards);

Since you changed the layout of this into a CardLayout you have to specify a string as second argument.

Are you sure you wanted Main to have a CardLayout? Your panel cards already contains such a layout.

Howard
  • 38,639
  • 9
  • 64
  • 83
  • :|...that was quite a stupid mistake. No, i didn't intend to have `Main` to have a `CardLayout`. if you can help me with another thing, i am having a problem in switching from the introPanel to the mainMenuPanel. i have checked out stuff online but im not able to figure how to do that. – Jha Dec 03 '11 at 13:38
  • @Jha you are calling `main.swapView` inside your `IntroGamePanel`. It is a field which was never initialised. Maybe you want to pass a reference as an argument to your constructor of `IntroGamePanel` and store it then in this field. – Howard Dec 03 '11 at 13:44
  • @Jha: That's a separate question isn't it? why not upvote and accept Howard's answer and ask that separate question elsewhere. – Hovercraft Full Of Eels Dec 03 '11 at 13:45