Java 查找 Swing/AWT 对象的“真实”高度/宽度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/211051/
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
Find "real" height/width of Swing/AWT object
提问by Edward Z. Yang
Because Canvas3D doesn't have the ability to resize dynamically with the parent frame, I would like to be able to track when a user resizes a window and then resize it manually myself. (If this ends up crashing Canvas3D, as some docs suggest, I will simply destroy and recreate it when the user resizes their window). Part of this procedure involves being able to accurately tell how big the container panel is to begin with.
因为 Canvas3D 不具有使用父框架动态调整大小的能力,所以我希望能够跟踪用户何时调整窗口大小,然后自己手动调整大小。(如果这最终导致 Canvas3D 崩溃,正如一些文档所建议的那样,当用户调整窗口大小时,我将简单地销毁并重新创建它)。此过程的一部分涉及能够准确地判断集装箱面板的大小。
The two methods I've tried:
我试过的两种方法:
panel.getHeight(); panel.getPreferredSize().height;
Don't seem to accurately report things: getHeight()
is invariably zero, and getPreferredSize()
returns numbers that don't actually have anything to do with the actual size of the panel.
似乎没有准确报告事物:getHeight()
总是为零,并getPreferredSize()
返回与面板的实际大小实际上没有任何关系的数字。
Any ideas?
有任何想法吗?
Edit: So, I took a debugger to the panel object and manually inspected the non-object properties and I didn't see anything that resembled width/height. Granted, there are sub-objects that I didn't look at. Also, maybe the window has to be visible (it isn't, at the point I'm interfacing the object) when I query for height/object?
编辑:因此,我将调试器带到了面板对象并手动检查了非对象属性,但我没有看到任何类似于宽度/高度的东西。当然,有些子对象我没有看过。另外,当我查询高度/对象时,也许窗口必须是可见的(它不是,在我与对象接口时)?
Edit 2: So, Swing classes are subclasses of AWT classes, so I imagine if you're able to find the height/width of those, the approach would generalize. I've amended the title accordingly.
编辑 2:所以,Swing 类是 AWT 类的子类,所以我想如果您能够找到它们的高度/宽度,则该方法可以推广。我已经相应地修改了标题。
采纳答案by Simon Lehmann
To determine the size of a component you have to either:
要确定组件的大小,您必须:
- have set it manually at some point
- run the layout manager responsible for layouting the component
- 在某个时候手动设置了它
- 运行负责布局组件的布局管理器
Generally, you get the exact size of a component via the getSize() method, which returns a Dimension object containing width and height, but getWidth/Height() should work too. But this can only work, if one of the two preconditions are met. If a window has never been made visible, has no layout manager or the component (you want to know the size of) has been added after the window/container has been made visible, the size usually is zero.
通常,您可以通过 getSize() 方法获取组件的确切大小,该方法返回一个包含宽度和高度的 Dimension 对象,但 getWidth/Height() 也应该起作用。但这只有在满足两个先决条件之一的情况下才能起作用。如果一个窗口从未可见,没有布局管理器或在窗口/容器可见后添加了组件(您想知道其大小),则大小通常为零。
So to get the correct size, you have to make the container/frame visible (after you have added the component) or call validate() or doLayout() on the container to recalculate the layout, if you added the component after the last layout was done. Another thing to keep in mind is setting and probably configuring a layout manager on the container. If no layout manager ist set (null), even making a container visible oder calling validate() does not set a size on its children.
因此,要获得正确的大小,您必须使容器/框架可见(在添加组件之后)或在容器上调用 validate() 或 doLayout() 以重新计算布局,如果您在最后一个布局之后添加了组件已完成。要记住的另一件事是在容器上设置并可能配置布局管理器。如果未设置布局管理器 (null),则即使使容器可见或调用 validate() 也不会为其子项设置大小。
The minimumSize/preferredSize/maximumSize properties are hints to the layout manager, how the component should be sized, but it does not have toobey them (most layout managers don't).
minimumSize/preferredSize/maximumSize 属性是对布局管理器的提示,组件应该如何调整大小,但它不必遵守它们(大多数布局管理器没有)。
Edit 2:After I read your other questionabout the same subject, I think you should read Using Layout Managersfrom The Java Tutorials
编辑 2:在我阅读您关于同一主题的其他问题后,我认为您应该阅读Java 教程中的使用布局管理器
Edit:I don't know if you already figured that out, but to react to the resizing of the window, you can do something like this:
编辑:我不知道您是否已经弄清楚了,但是要对窗口大小的调整做出反应,您可以执行以下操作:
public class WindowResizeTest extends JFrame {
public static void main(String[] args) {
new WindowResizeTest();
}
public WindowResizeTest() {
this.setSize(640, 480);
JPanel panel = new JPanel();
panel.setBackground(Color.RED);
this.add(panel);
this.addComponentListener(new ComponentListener() {
public void componentResized(ComponentEvent e) {
System.out.println(e.getComponent().getSize());
}
public void componentHidden(ComponentEvent e) {}
public void componentMoved(ComponentEvent e) {}
public void componentShown(ComponentEvent e) {}
});
this.setVisible(true);
}
}
回答by Taylor Golden
I found out that if you extend by JFrame, this code can be used also to save time, effort and space.
我发现如果你通过 JFrame 扩展,这个代码也可以用来节省时间、精力和空间。
int windowWidth = getWidth();
int windowHeight = getHeight();
I know you already got an answer but if you ever need an alternative, here it is.
我知道您已经得到了答案,但如果您需要替代方案,这里就是。