JFrame:获取无边框大小?
在Java中,是否可以获取没有标题和其他边框的JFrame的宽度和高度?
frame.getWidth() 和frame.getHeight()1 似乎返回包括边框的宽度。
谢谢。
In Java, is it possible to get the Width and Height of the JFrame without the title and other borders?
frame.getWidth() and frame.getHeight()1 seems to return the width including the border.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这工作正常
,但如果您还没有添加内容,则不行。就我而言,我想在添加内容之前计算 JFrame 的内部尺寸,以便我可以相应地划分内容。这就是我的想法。
This works fine
But not if you haven't added content yet. In my case, I wanted to calculate the inner dimensions of the JFrame before adding content, so I could divide up the content accordingly. Here's what I came up with.
下面是一个适用于
JFrame
以及 AWT 的Frame
(它恰好是 JFrame 的超类型)的代码片段:注意:插入仅在以下情况下有效:框架已显示。
这是解决此问题的另一个代码片段:
此代码需要使用可见框架调用一次。之后,即使对于不可见的帧,它也可以正确预测插入(由于
defaultInsets
的缓存),假设它们始终相同。当然,这仅在所有窗口都具有相同的窗口装饰时才有效。但我不知道在任何情况下它们可能因窗口而异。
这也可能很有用:
一旦窗口可见,它将调用
getInsetsWithDefault()
方法并初始化正确的defaultInsets
。Here is a code snippet which works on
JFrame
as well as AWT'sFrame
(which happens to be the super-type of JFrame):Beware: The insets are only valid once the frame has been shown.
Here is another code snippet to work around this problem:
This code needs to be called once with a visible Frame. After that it can correctly predict the insets even for invisible frames (due to caching of the
defaultInsets
), assuming they are always the same.Of course this only works if all windows get the same window-decorations. But i am not aware of any case where they might differ from window to window.
This might be useful, too:
It will call the
getInsetsWithDefault()
method once the window is visible and initialize the correctdefaultInsets
.