我可以在显示之前获得正确的 JComponent 大小吗? [英] Can I get the right JComponent size before it's shown?

查看:25
本文介绍了我可以在显示之前获得正确的 JComponent 大小吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JComponent 的大小是什么时候计算的?在出现在屏幕上之后还是在那之前?如果我在 .setVisible(true) 之前发送 .getSize() 消息,它会给我 正确 答案吗?谢谢

When is the size of the JComponent is calculated? After being shown in the screen or before that? if I send .getSize() message before .setVisible(true), would it give me the right answer? Thanks

推荐答案

我有时会在调试时检查组件的大小,以找出为什么我看不到它们的原因.在大多数情况下,大小将在 GUI 被渲染时实现.当 pack()setVisible(true) 在顶级窗口上调用时,可能会发生这种情况.我通常的方法调用顺序是首先调用 pack() ,因为这告诉布局管理器布局他们负责的组件,并设置组件和 GUI 的大小,然后调用setLocationRelativeTo(null) 使我的 GUI 居中,然后调用 setVisible(true) 来显示它.

I sometimes check the sizes of my components when debugging to find out why I can't see them for instance. In most cases, the sizes will be realized when the GUI has been rendered. This can occur when pack() or setVisible(true) has called on the top-level window. My usual sequence of method calls is to call pack() first as this tells the layout managers to lay out the components that they are responsible for, and sets the sizes of the components and the GUI, then call setLocationRelativeTo(null) to center my GUI, then call setVisible(true) to display it.

这篇关于我可以在显示之前获得正确的 JComponent 大小吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆