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"));
	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
	//real level one
	//level two

	//controls[how to, level completed, quit.
	button1 = new JButton(new ImageIcon("howto.png"));
	button2 = new JButton(new ImageIcon("quit.png"));
	button3 = new ImageIcon("levelcompleted.png");
	controlspanel.setLayout(new BorderLayout());
	controlspanel.add(button2, BorderLayout.LINE_END);
	//controlspanel.add(button3, BorderLayout.CENTER);
	setSize(500 ,525);


	public static void main(final String[] args) {
		// TODO Auto-generated method stub
		MainProject frame = new MainProject();
		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" +
		if(arg0.getSource() == button3){
			JOptionPane.showMessageDialog(null, "NEXT LEVEL\n" +
					"Another set of items to search.\n\n" +
		if(arg0.getSource() == welcome2){




Recommended Answers

All 2 Replies

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.