# SetPreferredSize for a Frame in Java

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

## Recommended Posts

The following code is in Java. I decided to create the dimensions for my frame object relative to the dimensions of an image I want displayed on the frame object which would mean I want the frame to scale with the image. However the frame could not show some of the bottom part of the image. So I needed to give the frame a bit more height by hard-coding 32 to it. While hard-coding works, it does not seem the right way to go about it in terms of efficiency.

How come the setPreferredSize method does not scale with the image's dimensions?

Before:
frame.setPreferredSize(new Dimension(gameMenuArt.getWidth(),gameMenuArt.getHeight()));


After:


Edited by warnexus

##### Share on other sites

I'm kinda rusty on Swing/AWT stuff right now but I think it may have to do with the preferred size taking in account the size of the title bar of the window (the one with the close, minimize and maximize buttons).

##### Share on other sites

I'm kinda rusty on Swing/AWT stuff right now but I think it may have to do with the preferred size taking in account the size of the title bar of the window (the one with the close, minimize and maximize buttons).

Oh! I never thought about it like that. You got a point there!

• 11
• 20
• 12
• 11
• 38
• ### Forum Statistics

• Total Topics
631401
• Total Posts
2999866
×