I have a non resizable Jframe of size 1280x800. Of course this size appears bigger and smaller according to the resolution of the screen. (It has a background image). Now, if i try this on lets say a 4k monitor, it would be absolutely impractical because to small. Isn't there a way to scale the JFrame? Or a solution to this problem? What i thought i would do is write bigger jFrames and tell the main class which one to open according to the resolution. I am sure there is a much more elegant way to do that, since i guess it is a problem that many would have come across! What a nightmare! Please help me! Thank you
-
See also this [pitfall](http://stackoverflow.com/a/12532237/230513) of non-resizable containers. – trashgod Jun 20 '16 at 01:35
2 Answers
One way you could achieve that is by getting the screen size of the device, and then setting the size of your JFrame accordingly:
Dimension screenDimension = Toolkit.getDefaultToolkit().getScreenSize();
double jFrameWidth = screenDimension.width * 0.7;
double jFrameHeight = screenDimension.height * 0.5;
Or if you just want to maximise the JFrame you can use:
jFrame.setExtendedState(JFrame.MAXIMIZED_BOTH);

- 2,709
- 10
- 17
-
yes, that you, but you see, this makes the frame of the right size, but not what is in it. And so my background image would never fit properly – Jun 19 '16 at 19:41
-
@MarcoEdoardoPalma You should write the contents of your JFrame using layout managers and non-fixed sizes so that they will scale to fit the size of the JFrame. – explv Jun 20 '16 at 09:44
Detect the resolution of the window you're launching on and scale the frame to a percentage of that window. Have a baseline dimension (1280 * 800) so it isn't too-squashed on smaller screens.
As an example I pull the local GraphicsEnvironment
, I pull the data from each relevant GraphicsDevice
into a data class that I wrote myself, and I use that throughout my project as it gives me all sorts of stuff like buffer strategies, window dimensions, and so on. I do this when the application is loading (using a SplashScreen
) which affords me control over the whole process.
That's about as elegant as you can get, I think.
EDIT
Editing in some example pseudocode to give an idea of what I'm getting at. I write primarily in Java, but I'm not doing this in an IDE so it won't necessarily be compile-ready:
public void scaleWindowDimensions(JFrame frame, GraphicsDevice gd) {
Rectangle bounds = gd.getDefaultConfiguration().getBounds();
int screenX = (int) bounds.getWidth();
int screenY = (int) bounds.getHeight();
// substitute this for however you're setting the size of the JFrame; this is simply how I sometimes do it
frame.getContentPane().setPreferredSize(new Dimension(screenX, screenY));
}
This is a very quick example that will set the size of your JFrame to that of the monitor it's running on. You'll need to modify the background image you're using as well. There's a lot more you could do with this but I'm keeping it simple on purpose.

- 1,169
- 12
- 27
-
wow this seems to be what I was looking for, but how do you scale it? Thank you! – Jun 19 '16 at 19:39
-
You need to decide what screen size your `JFrame` works best on. You then scale the JFrame up on screen sizes that are larger, and down on screen sizes that are smaller (so long as you don't hit the minimum size). You can scale your background image in the same way. – Gorbles Jun 20 '16 at 08:30
-
-
Hi, if people are going to randomly downvote this answer, I'd appreciate a comment. – Gorbles Jul 26 '16 at 09:58