Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How come JFrame window size in Java does not produce the size of window specified?

Tags:

java

pixel

jframe

I am just messing around trying to make a game right now, but I have had this problem before too. When I specify a specific window size (1024 x 768 for instance) the window produced is just a little larger than what I specified. Very annoying. Is there a reason for this? How do I correct it so the window created is actually the size I want instead of being just a little bit bigger? Up till now I have always just gone back and manually adjusted the size a few pixels at a time until I got the result I wanted, but that is getting old. If there was even a formula I could use that would tell me how many pixels I needed to add/subtract from my my variable that would be excellent!

P.S. I don't know if my OS could be a factor in this, but I am using W7X64.

private int windowWidth = 1024;
private int windowHeight = 768;

public SomeWindow() {
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setSize(windowWidth, windowHeight);
    this.setResizable(false);
    this.setLocation(0,0);
    this.setVisible(true);
}
like image 747
ubiquibacon Avatar asked Mar 15 '10 09:03

ubiquibacon


People also ask

How do I change the size of a JFrame window?

Firstly, we use getScreenSize() method of the Java Toolkit class to get the resolution of the screen in dots per inch. Secondly, we use the frame. setSize() to set the dimensions that we receive from the getScreenSize() method. This method contains width as the first parameter and height as the second parameter.

How do you make a JFrame fit the screen?

Setting a JFrame to fill half the screen For instance, if you want to create a JFrame that is half the width and half the height of the screen, you can just divide the dimensions in half, as shown in this code: Dimension screenSize = Toolkit. getDefaultToolkit(). getScreenSize(); aFrame.

What happen if set size is not called on JFrame?

What happen if setSize method is not called on JFrame? The default size of a frame is 0 by 0 pixels. The setVisible(true) method makes the frame appear on the screen. If you forget to do this, the frame object will exist as an object in memory, but no picture will appear on the screen.


2 Answers

I want the total frame that Windows creates to be the exact size I specify

I don't understand your problem. Post your SSCCE that shows the problem.

If I run code like:

frame.setResizable(false);
frame.setSize(1024, 768);
frame.setVisible(true);
System.out.println(frame.getSize());

It displays java.awt.Dimension[width=1024,height=768], is that not what you expect?

If there was even a formula I could use that would tell me how many pixels I needed to add/subtract from my my variable that would be excellent!

Maybe you are referring to the space occupied by the title bar and borders?

import java.awt.*;
import javax.swing.*;

public class FrameInfo
{
    public static void main(String[] args)
    {
        GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
        Rectangle bounds = env.getMaximumWindowBounds();
        System.out.println("Screen Bounds: " + bounds );

        GraphicsDevice screen = env.getDefaultScreenDevice();
        GraphicsConfiguration config = screen.getDefaultConfiguration();
        System.out.println("Screen Size  : " + config.getBounds());

        JFrame frame = new JFrame("Frame Info");
        System.out.println("Frame Insets : " + frame.getInsets() );

        frame.setSize(200, 200);
        System.out.println("Frame Insets : " + frame.getInsets() );
        frame.setVisible( true );

        System.out.println("Frame Size   : " + frame.getSize() );
        System.out.println("Frame Insets : " + frame.getInsets() );
        System.out.println("Content Size : " + frame.getContentPane().getSize() );
     }
}
like image 97
camickr Avatar answered Oct 21 '22 02:10

camickr


When you say the obtained window size is not the asked one, are you talking about the window, with its decorations ?

Indeed, window size is defined without OS specific window decoration.

Try to add

this.setUndecorated(true);

before the

this.setVisible(true);
like image 37
Riduidel Avatar answered Oct 21 '22 01:10

Riduidel