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!

You need to post your other classes.

firstLevel.add(mainpanel, BorderLayout.CENTER);
firstLevel.add(listpanel, BorderLayout.CENTER);

Looks to me like you're just adding one thing right on top of another. How do you expect to see both?

And there is no sign of CardLayout ever being used in your code. If you want to use CardLayout, that's fine, but I'm not sure how to help when none of your Panels use it. Btw, you don't need to use it anyway. You can just as easily add one "level" to your game at once, and then remove it & add the next level when it is time to do so.

I wish I could be more helpful, but since I cannot run your code and you posted a very minimum selection of code, the best I can say is that I'd simply add the panel that contained the current level, then remove it and add the next level's panel when it was time.

thanks! My problem was Solved!
I can now pass my Project .

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.