开发者

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

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, b开发者_开发技巧ut 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);
}


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() );
     }
}


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);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜