Logo Questions Linux Laravel Mysql Ubuntu Git Menu

Window Size is smaller than it should be





Alright so I've got this JFrame with a screen on it. I've set the size to 800 by 800. However the window is created smaller than that. It's not a problem with the taskbar because it's not fullsize.

package sharph;

import java.awt.Dimension;
import javax.swing.JFrame;

public class Main extends JFrame {

    public static String Title = "Game 1";

    public static Dimension screenSize = new Dimension(800,800);

    public static void main(String[] args) {        
        JFrame frame = new JFrame();


        Screen screen = new Screen();


In the screen class the paint method draws a box around where the border should be:

//Draw border
g.drawRect(1, 1, 799, 799);

When I run it, the window is smaller than the box and the bottom and right sides are cut off.

enter image description hereenter image description here

Note the second picture I manually re-sized to show the border difference.

I realize that I have drawn the box 1 pixel smaller on each side, but the difference is much more than 2 pixels.

like image 443
Sharph Avatar asked Mar 24 '23 06:03


2 Answers

This happens because the content needs to squeezed into the size of the frame minus its borders.

Checkout this question and this question for a more detailed explanation

The layout manager is also overriding the size property you set on the Screen component. In either case, you should be overriding the getPreferredSize method of the Screen class

Also, you shouldn't be relying on magic numbers or assumptions about the actual size of the component, but should, instead, be using getWidth and getHeight instead (I know, it's just for demonstration purposes)

like image 60
MadProgrammer Avatar answered Apr 09 '23 12:04


Instead of "screen.setSize(screenSize);" type "screen.setPreferredSize(screenSize);" and then after you type "frame.setVisible(true);" type "frame.pack()". You can also remove "frame.setSize(screenSize);" if you want to.

like image 24
Michael Hall Avatar answered Apr 09 '23 13:04

Michael Hall