java Java中的JFrame窗口大小为什么不产生指定的窗口大小?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2445971/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 21:11:47  来源:igfitidea点击:

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

javajframepixel

提问by ubiquibacon

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!

我现在只是在尝试制作游戏,但我以前也遇到过这个问题。当我指定特定的窗口大小(例如 1024 x 768)时,生成的窗口仅比我指定的大一点。很烦人。是否有一个原因?我如何更正它以便创建的窗口实际上是我想要的大小而不是稍微大一点?到目前为止,我一直只是返回并一次手动调整几个像素的大小,直到得到我想要的结果,但这已经过时了。如果我可以使用一个公式来告诉我需要从我的变量中添加/减去多少像素,那就太好了!

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

PS 我不知道我的操作系统是否可能是其中的一个因素,但我使用的是 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);
}

采纳答案by camickr

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

我希望 Windows 创建的总框架是我指定的确切大小

I don't understand your problem. Post your SSCCEthat shows the problem.

我不明白你的问题。发布显示问题的SSCCE

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?

它显示 java.awt.Dimension[width=1024,height=768],这不是您所期望的吗?

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

回答by Riduidel

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