为什么 JFrame setSize() 方法不能正确设置大小?
我已经用java编程一个学期左右了,这个问题我已经遇到过几次了,终于抽出时间来问了。
如果我制作一个 JFrame
然后设置大小,例如 setSize(400,800)
。该帧实际上并不是800 像素
长。据我所知,它实际上更像是 770(或者可能是 769)像素
长。另外,如果您将垂直尺寸设置得非常低(低于 30),则框架甚至不会显示,只有操作系统的顶部窗口栏,并且框架不会变大,直到您将值设置为超过 30(因此setSize(400,0)
看起来与 setSize(400,20)
相同)。为什么会这样,修复起来并不难,但很奇怪,我很好奇为什么会这样?
如果您需要更多信息,请尽管询问,我会将其提供给您。
So I've been programming in java for a semester or so, and I've had this problem a few times and finally got around to asking.
If I make a JFrame
and then set the size, like setSize(400,800)
for example. The frame is not actually 800 pixels
long. From what I can tell it is actually more like 770 (or maybe 769) pixels
long. Also, if you set the vertical size very low (below 30), the frame doesn't even show up, only the top window bar from the OS and the frame doesn't get bigger until you go to a value over 30 (so setSize(400,0)
looks the same as setSize(400,20)
). Why is this, it's not hard to fix but its weird and I'm curious why this is?
If you need more information about anything just ask and I'll get it to you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
框架的上边框大小为 30。您可以使用 MouseInputAdapter 编写代码来打印框架上任意点的坐标。您会发现当光标位于框架上边框的正下方时,y 坐标不为零,其接近30。因此,如果您给框架的尺寸为300 * 300,则可用于将组件放在框架上的尺寸仅为300 * 270。因此,如果您需要尺寸为300 * 300,给出300*330尺寸的框架。
The top border of frame is of size 30.You can write code for printing the coordinate of any point on the frame using MouseInputAdapter.You will find when the cursor is just below the top border of the frame the y coordinate is not zero , its close to 30.Hence if you give size to frame 300 * 300 , the size available for putting the components on the frame is only 300 * 270.So if you need to have size 300 * 300 ,give 300 * 330 size of the frame.
JFrame SetSize() 包含区域 + 边框。
我认为您必须设置其
ContentPane
的大小,因此我建议您使用嵌入在 JFrame 中的 JPanel 并在该 JPanel 上进行绘制。这将最大限度地减少您的问题。
我正在阅读 Javadoc
JFrame SetSize() contains the the Area + Border.
I think you have to set the size of
ContentPane
of thatSo I would advise you to use JPanel embedded in a JFrame and you draw on that JPanel. This would minimize your problem.
I am reading this from Javadoc
这可能是因为框架的大小包括边框的大小。
来源:
http://download.oracle.com/javase/tutorial/uiswing/components /frame.html
It's probably because size of a frame includes the size of the border.
Source:
http://download.oracle.com/javase/tutorial/uiswing/components/frame.html
设置框架的大小有很多充分的理由。一是记住用户设置的最后一个尺寸,并恢复这些设置。我有这个代码似乎对我有用:
}
There are lots of good reasons for setting the size of a frame. One is to remember the last size the user set, and restore those settings. I have this code which seems to work for me:
}
在 OS X 上,您需要考虑现有的窗口装饰。他们在高度上增加了 22 个像素。因此,在 JFrame 上,您需要告诉程序:
On OS X, you need to take into account existing window decorations. They add 22 pixels to the height. So on a JFrame, you need to tell the program this:
我知道这个问题大约有 6 岁多了,但 @Kyle 的答案不起作用。
使用这个
但这总是适用于任何尺寸:
如果您不希望边框为边框,而只想要白色区域,请在此处:
此外,这仅适用于 Windows 10,对于 MacOS 用户,请使用 @ben 的答案。
I know that this question is about 6+ years old, but the answer by @Kyle doesn't work.
Using this
But this always work in any size:
If you don't want the border to border, and only want the white area, here:
Also this only works on Windows 10, for MacOS users, use @ben's answer.