Jump to content
  • Advertisement
Sign in to follow this  
Nicholas Kong

JPanel does not show up in Java

This topic is 1847 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've been trying to figure why the gameControlArt(which is a JPanel object) does not  appear on the gamePanel which is also a JPanel object. 
 
What I am trying to create: the user sees the mainMenuArt(A JPanel object that has a JLabel containing artwork) adding on to the gamePanel upon running the program. When user presses a key, mainMenuArt gets removed. gameControlArt shows up. User presses a key again, gameControlArt gets removed, canvas object gets added to the gamePanel object. 
 
What winds up happening instead: the mainMenuArt appears on the gamePanel which is good. The problem lies after removing the mainMenuArt, the gameControlArt(which is another JPanel object that has a JLabel object containing artwork) does not get added to the gamePanel(which is a JPanel object). Instead it shows the default gray area when a JPanel object has no components added to it. When the user presses a key, the canvas shows up on the gamePanel. 
 
So to sum what the problem is: the gameControlArt does not show up at all like it is suppose to on the gamePanel. 
 
I try updateUI(), revalidate and repaint() but those method calls do not work for some odd reason. 
    public class GameWindow extends JFrame implements KeyListener{
    gamePanel = (JPanel) getContentPane();
    gamePanel.remove(mainMenuArt);
    gameControlArt = new GameControlArt();
    gamePanel.add(gameControlArt);
    GameWindow.getGamePanel().revalidate();
    }
    
    public class extends Canvas implements KeyListener
    {
    GameWindow.getGamePanel().add(this);
    GameWindow.getGamePanel().revalidate();
    }
Edited by warnexus

Share this post


Link to post
Share on other sites
Advertisement

I found the solution. Apparently I need to resize my frame object relative to the new object's dimension that going to be added to it so object can appear.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!