我能在显示之前得到正确的JComponent大小吗?

时间:2022-03-01 21:11:03

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

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

4 个解决方案

#1


7  

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.

我有时在调试时检查组件的大小,以找出为什么我不能看到它们的原因。在大多数情况下,当GUI被呈现时,大小将被实现。当pack()或setVisible(true)调用*窗口时,就会发生这种情况。我通常的方法调用顺序是首先调用pack(),因为这告诉布局管理器列出它们负责的组件,并设置组件和GUI的大小,然后调用setlocation相对论性(null)将GUI居中,然后调用setVisible(true)来显示它。

#2


6  

The layout manager is responsible for determining the size of a component, so you don't know its actual size until the component has been added to the frame and the frame has been pack()ed ore made visible.

布局管理器负责确定组件的大小,因此您不知道它的实际大小,直到将组件添加到框架中,并且框架已被打包()已显示。

If you use a layout manager that respects the preferred size of a component then you can use:

如果您使用的布局管理器尊重组件的首选大小,那么您可以使用:

component.getPreferredSize();

Why do you think you need to know the size? Generally you don't worry about sizes and let the layout manager do its job.

为什么你认为你需要知道尺寸?通常您不需要担心大小,让布局管理器来完成它的工作。

#3


6  

In addition to the usual pack() > setVisible(true) > getPreferredSize() sequence, you can validate() the relevant Container to preview the geometry, as shown here.

除了通常的pack() > setVisible(true) > getPreferredSize()序列之外,您还可以验证()相关容器来预览几何图形,如下所示。

#4


6  

If I understand properly, the reason why you want to know the size of a component is to reset the size of a JWindow once a user click on the "More options" button, isn't it? I would suggest to do the following: when the user clicks on that button, update your UI adding the extra component, and the execute pack() on the JWindow. It should resize to the proper size.

如果我理解正确的话,您想知道组件大小的原因是在用户单击“More options”按钮时重置JWindow的大小,不是吗?我建议执行以下操作:当用户单击该按钮时,更新添加额外组件的UI,以及JWindow上的execute pack()。它应该调整到适当的大小。

#1


7  

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.

我有时在调试时检查组件的大小,以找出为什么我不能看到它们的原因。在大多数情况下,当GUI被呈现时,大小将被实现。当pack()或setVisible(true)调用*窗口时,就会发生这种情况。我通常的方法调用顺序是首先调用pack(),因为这告诉布局管理器列出它们负责的组件,并设置组件和GUI的大小,然后调用setlocation相对论性(null)将GUI居中,然后调用setVisible(true)来显示它。

#2


6  

The layout manager is responsible for determining the size of a component, so you don't know its actual size until the component has been added to the frame and the frame has been pack()ed ore made visible.

布局管理器负责确定组件的大小,因此您不知道它的实际大小,直到将组件添加到框架中,并且框架已被打包()已显示。

If you use a layout manager that respects the preferred size of a component then you can use:

如果您使用的布局管理器尊重组件的首选大小,那么您可以使用:

component.getPreferredSize();

Why do you think you need to know the size? Generally you don't worry about sizes and let the layout manager do its job.

为什么你认为你需要知道尺寸?通常您不需要担心大小,让布局管理器来完成它的工作。

#3


6  

In addition to the usual pack() > setVisible(true) > getPreferredSize() sequence, you can validate() the relevant Container to preview the geometry, as shown here.

除了通常的pack() > setVisible(true) > getPreferredSize()序列之外,您还可以验证()相关容器来预览几何图形,如下所示。

#4


6  

If I understand properly, the reason why you want to know the size of a component is to reset the size of a JWindow once a user click on the "More options" button, isn't it? I would suggest to do the following: when the user clicks on that button, update your UI adding the extra component, and the execute pack() on the JWindow. It should resize to the proper size.

如果我理解正确的话,您想知道组件大小的原因是在用户单击“More options”按钮时重置JWindow的大小,不是吗?我建议执行以下操作:当用户单击该按钮时,更新添加额外组件的UI,以及JWindow上的execute pack()。它应该调整到适当的大小。