简体   繁体   English

如何重新计算JComponent的首选大小?

[英]How do i recalculate preferred size of a JComponent?

I know the fact that when I create an instance of a JComponent, it has it's own preferred size. 我知道当我创建一个JComponent的实例时,它有自己的首选大小。 Now let's suppose that I setPreferredSize that JComponent manually with a dimension of 0 x 0. And i want that Component to "reset" its own preferredSize. 现在让我们假设我setPreferredSize手动JComponent的维度为0 x 0.我希望该组件“重置”自己的preferredSize。 How do I do that? 我怎么做?

1) Setting preferred size to null should reset the component back to getting its preferred size calculated as if it was never set. 1)将首选大小设置为null应该重新将组件重置为计算其首选大小,就像从未设置它一样。

component.setPreferredSize(null);

This might not do what you want, depending on how you signal that the layout should be redone - but at least it is technically the answer to your question. 这可能不会做你想要的,取决于你如何表示应该重做布局 - 但至少它在技术上是你的问题的答案。

2) It is generally advised to not use setPreferredSize, see this post 2)通常建议不要使用setPreferredSize,请参阅此帖子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM