如何重新计算JComponent的首选大小? [英] How do i recalculate preferred size of a JComponent?
问题描述
我知道这样的事实,当我创建JComponent的实例时,它具有自己的首选大小.现在,让我们假设我手动设置了JComponent的PreferredSize,其尺寸为0 x0.我希望该Component重置"其自己的preferredSize.我该怎么办?
I know the fact that when I create an instance of a JComponent, it has it's own preferred size. 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. How do I do that?
推荐答案
1)将首选大小设置为null会将组件重置为重新计算其首选大小,就像从未设置过一样.
1) Setting preferred size to null should reset the component back to getting its preferred size calculated as if it was never set.
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)通常建议不要使用setPreferredSize,请参见
2) It is generally advised to not use setPreferredSize, see this post
这篇关于如何重新计算JComponent的首选大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!