• Post Reply Bookmark Topic Watch Topic
  • New Topic

Trouble setting size of a JFrame  RSS feed

 
Sam Seymour
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am rather new to Swing, and I am building a game right now in which I need to display a pop-up window as a reminder for what commands exist within the game. I created a class extending JFrame for this and added all the information I need. However, for some reason, no matter how many different ways I try to set the size of this window (setSize(w, h), pack(), using a different layout, adding the compnents to a JPanel first and setting the preffered size of that, then adding the JPanel to the JFrame), it doesn't work. Instead of a window of my requested size, I get a tiny, maybe 100 x 100 pixel window that needs to be resized in order for its contents to be visible.

What am I doing wrong? Any help figuring this out would be appreciated!

 
Dieter Quickfend
Bartender
Posts: 543
4
Java Netbeans IDE Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unless you're using Absolute Positioning, you should use setPreferredSize(). Use the JPanel approach though, using JFrame for that is bad practice.
 
Sam Seymour
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dieter Quickfend wrote:Unless you're using Absolute Positioning, you should use setPreferredSize(). Use the JPanel approach though, using JFrame for that is bad practice.


Well, like I said before, I already tried that and it doesn't work. I get a tiny 100 x 100 window (approximately). Here is that code. Can anyone point out what I have done wrong?

 
Rob Camick
Ranch Hand
Posts: 2787
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't use the setPreferredSize() method. The layout manager will determine the preferred size of the panel.

The basic code for creating a GUI is something like:



That is you should add all the component to the panel/frame BEFORE you invoke pack() and setVisible() on the frame.
 
Sam Seymour
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Camick wrote:Don't use the setPreferredSize() method. The layout manager will determine the preferred size of the panel.

The basic code for creating a GUI is something like:



That is you should add all the component to the panel/frame BEFORE you invoke pack() and setVisible() on the frame.


Yes, I've done that too, indeed calling pack() after the components have been added. It didn't work. Here is that code, and here is a screenshot of the result I get. You can see the tiny JFrame in the middle of the screen.

tiny-jframe.png
[Thumbnail for tiny-jframe.png]
 
Rob Camick
Ranch Hand
Posts: 2787
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why does your code not look like the code I suggested? Did I not suggest you add and the component to the panel and frame BEFORE you do the pack() and setVisible()? Why are you using validate(). I did not mention that method anywhere.

Why don't you look at the example programs found in the Swing Tutorial to see how they are structured. I think you will find they all verify my suggestion.

Having said all that, your posted code still works fine for me. I'm using JDK7 on Windows 7.

The only other suggestion I have is to make sure you create the GUI components on the Event Dispatch Thread by using the invokeLater() method. The tutorials have examples if you are not sure what I mean.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!