Java Swing getSize() 返回不准确的值?
最简单地说,我试图使用组件的宽度来设置其父容器的大小。然而,为组件返回的宽度偏离了大约 4 个像素。
更多详情: 我有一个对话框,其中的子面板水平分布在其中间。对话框应始终与子面板列表一样宽,并且一旦列表变得太长,请使用滚动条。我尝试只使用 1 个子面板来开始。无论我做什么,返回的子面板的大小总是错误大约 4 个像素。我可以看到子面板右侧有一个非常烦人的 4 像素间隙,使我的对话框看起来很可怕。随着添加更多子面板,效果显然会更加复杂。
我尝试使用什么来获取宽度:
sup-panel.getSize().width;
sub-panel.getBounds().width;
sub-panel.getWidth();
sub-panel.getPreferredSize().width;
全部返回相同的错误值。
我尝试调用其他方法使其正常运行:
parent.validate();
parent.repaint();
parent.doLayout();
任何帮助将不胜感激。是故意的吗?我是否遗漏了尺寸背后的一些概念?它看起来是一致的,但如果我能在某个地方读到有此记录,那就太好了。
仅供参考:我正在使用 java 1.3(支持旧版应用程序)
注意:这不是调整窗口大小的问题,该部分发生得很好。子面板的大小似乎已正确设置,因为其大小的更改反映在我的代码返回的值中。这些值总是略有偏差。
注意2:在父容器上调用 pack() 会使窗口看起来像我想要的那样,但它不适用于我的所有场景,因为最终在出现足够的子面板后,需要出现滚动条。在这种情况下,pack()只会使我的对话框变得超宽,并使滚动条变得无用。
编辑:我已经解决了我的问题。令人尴尬的是,我所有的子面板都没有应有的均匀宽度。有 2 个可行的解决方案。强制它们全部统一,这可能很棘手,具体取决于您对布局的掌握程度。另一个是总结我想要可见的前 n 个面板的各个宽度。请注意,为了使该解决方案发挥作用,我必须为每个子组件的宽度添加额外的+1。不知道为什么,但我用各种边框和布局对其进行了测试,结果非常一致;我想在我没有认真考虑的地方可能有一个额外的空间像素。但重要的是它现在工作得很好。下面是一个数学示例(对于需要强制转换的老式非类型集合,Java 1.3,我们深表歉意):
int width = 0;
for(int i = 0; i < MAX_VISIBLE_PANELS; i++)
{
width += ((SubPanel)panelList.get(i)).getWidth();
width ++;//extra pixel per panel
}
To put this most simply, I am trying to use the width of a component to set the size of its parent container. However the width being returned for the component is off by about 4 pixels.
More details:
I have a dialog that has sub-panels spreading horizontally across the middle of it. The dialog should always be as wide as this list of sub panels, and use a scroll bar once the list gets too long. I tried it just using 1 sub-panel though to start. No matter what I do, the size of the sub-panel being returned is always wrong by about 4 pixels. I can see just a very annoying 4-pixel gap to the right of my sub-panel making my dialog look ghastly. The effect obviously compounds as more sub-panels are added.
What I have tried using to get the width:
sup-panel.getSize().width;
sub-panel.getBounds().width;
sub-panel.getWidth();
sub-panel.getPreferredSize().width;
All return the same wrong value.
Other things I have tried calling to make it behave:
parent.validate();
parent.repaint();
parent.doLayout();
Any help would be appreciated. Is it intentional? Am I missing some concept behind sizes? It seems consistent, but it would be nice if I could read somewhere that this is documented.
FYI: I am using java 1.3 (supporting a legacy app)
NOTE: This is not an issue getting the window to resize, that part is happening just fine. And the size of the sub-panels appears to be properly set as changes to it's size are reflected in the values returned for my code. The values are just always off slightly.
NOTE2: calling pack() on the parent container makes the window look like I want it to, but it will not work for all scenarios for me because eventually after enough sub-panels are present a scroll bar will need to be present. pack() will simply make my dialog super wide in this case and make the scroll bar useless.
EDIT: I have solved my problem. Rather embarrassingly all of my sub-panels were not of a uniform width as they should have been. There are 2 solutions that work. Force them all to be uniform, which can be tricky depending how good you are with layouts. The other is to sum up the individual widths of the first n panels I want to be visible. Note that to make this solution work, I have to add an extra +1 to the width for each sub component. Not sure why, but I have tested it with various borders and layouts and it is extremely consistent; I imagine there is perhaps a pixel of extra space somewhere I am not thinking hard enough about. But the important thing is it works perfectly now. Here is a sample of the math (sorry about the old school untyped collection requiring a cast, Java 1.3):
int width = 0;
for(int i = 0; i < MAX_VISIBLE_PANELS; i++)
{
width += ((SubPanel)panelList.get(i)).getWidth();
width ++;//extra pixel per panel
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想到两件事:
您也许可以使用
validate()
在pack()
之前,以便建立子面板的几何形状供以后参考,如图所示在这个示例。您可能需要考虑
FlowLayout
,这是JPanel
的默认布局。Two things come to mind:
You may be able to use
validate()
prior topack()
in order to establish a sub-panel's geometry for later reference, as shown in this example.You may need to account for the default gaps specified by
FlowLayout
, which is the default layout forJPanel
.相反,您可以
pack()
窗口,然后将窗口大小限制为可用的屏幕尺寸吗?Instead, could you
pack()
the window and then constrain the window size to the available screen dimensions?