• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JFrame not resizing when JPanel changes size

 
Vas Nesterov
Greenhorn
Posts: 2
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone. I am currently working on a game - Sokoban. I ran into a bit of an issue, where I want the frame to automatically resize itself when a new level begins (as most levels are different in size).
I can't seem to get it to work.
Here are parts of my code:



The game is essentially a JPanel that I draw inside a JFrame.
As you can see, I've been trying to revalidate the JFrame in different parts, but it doesn't appear to be working. I think it's revalidating the JPanel. How can I revalidate the JFrame to match the new coordinates of the JPanel?

Thanks!
 
Kemal Sokolovic
Bartender
Posts: 825
5
Java Python Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should instantiate your frame from within another class, not the panel class. That's a very bad approach. In this case, revalidate() refers to panel.

Did you try to invoke pack() on your frame after you change/add new panel? That should resize it to fit its content.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
have you tried calling
f.pack();
when new level starts?

(will need class level 'f')
 
Vas Nesterov
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great! Calling pack() at level starts worked. I had to make my JFrame class level as well as static.
I removed revalidate from my code.

Thank you very much!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic