I made a game for my project. it contains two levels and a welcome screen. The problem is I cant go to the other panels. I tried using cardlayout. but since my panels are in different classes, I don't know how to do it. Please help.
here's part of the code:
public mainProject2(){
Container c = getContentPane();
panel = new JPanel();
firstLevel = new JPanel();
secondLevel = new JPanel();
welcomepanel = new JPanel();
welcomepanel.setLayout(new BorderLayout());
welcomeimage = Toolkit.getDefaultToolkit().getImage("welcome.png");
welcome2 = new JButton(new ImageIcon("welcome.png"));
welcome2.addActionListener(this);
welcome2.setBorderPainted(false);
controlspanel = new JPanel();
//first level.
mainpanel = new MainPanel();
listpanel = new ListPanel();
//second level
nextlevelpanel = new NextLevelPanel();
listpanel2 = new ListPanel2();
//panel containing the first level panels.
firstLevel.setLayout(new BorderLayout());
firstLevel.add(mainpanel, BorderLayout.CENTER);
firstLevel.add(listpanel, BorderLayout.CENTER);
firstLevel.add(controlspanel, BorderLayout.PAGE_END);
//panel containing the second level panels.
secondLevel.setLayout(new BorderLayout());
secondLevel.add(nextlevelpanel, BorderLayout.CENTER);
secondLevel.add(controlspanel, BorderLayout.PAGE_END);
panel.setLayout(new BorderLayout());
//welcome scrren
panel.add(welcomepanel);
//real level one
panel.add(firstLevel);
//level two
panel.add(nextlevelpanel);
//controls[how to, level completed, quit.
panel.add(controlspanel,BorderLayout.PAGE_END);
button1 = new JButton(new ImageIcon("howto.png"));
button2 = new JButton(new ImageIcon("quit.png"));
button3 = new ImageIcon("levelcompleted.png");
controlspanel.setLayout(new BorderLayout());
controlspanel.setBackground(Color.BLACK);
controlspanel.add(button1,BorderLayout.LINE_START);
button1.addActionListener(this);
controlspanel.add(button2, BorderLayout.LINE_END);
//controlspanel.add(button3, BorderLayout.CENTER);
setSize(500 ,525);
setLocation(0,0);
//c.add(cardPanel);
c.add(panel);
}
public static void main(final String[] args) {
// TODO Auto-generated method stub
MainProject frame = new MainProject();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setResizable (false);
}
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
if(arg0.getSource() == button1){
JOptionPane.showMessageDialog (null,"How to play : \n" +
"Search the items given in the list.\n " +
"Click at the position of the image to ...\n" +
"After finding all the things in list, click the level completed button to proceed to the next level.\n" +
"@@ENJOY!!!@@");
}
if(arg0.getSource() == button3){
JOptionPane.showMessageDialog(null, "NEXT LEVEL\n" +
"Another set of items to search.\n\n" +
".......");
}
if(arg0.getSource() == welcome2){
System.out.println("gosh!!");
getContentPane().remove(welcome);
}
}
}
Thanks!