简体   繁体   English

自动设置JPanel的最小大小

[英]Automatically set minimum size of JPanel

I have a subclass of JPanel , and I want it to, as soon as its parent frame is pack() ed, to set its minimum size to its new preferred size. 我有一个JPanel的子类,我希望它在其父框架被pack() ed后立即将其最小大小设置为其新的首选大小。 I've tried using a ComponentAdapter , but JPanel s are by default visible; 我试过使用ComponentAdapter ,但JPanel默认是可见的; if I setVisible(false) at the beginning of the constructor, the JFrame won't make it visible again. 如果我在构造函数的开始处设置了setVisible(false) ,则JFrame将使其不再可见。 If I use SwingUtilities 's method to get the window root, it will return null because it's in a constructor. 如果我使用SwingUtilities的方法获取窗口根,则它将返回null因为它在构造函数中。

Is there a way to do this? 有没有办法做到这一点?

I have a subclass of JPanel, and I want it to, as soon as its parent frame is pack()ed, to set its minimum size to its new preferred size. 我有一个JPanel的子类,我希望它在其父框架被pack()处理后立即将其最小大小设置为其新的首选大小。

If you know the preferred/minimum size before the pack, set it then. 如果在装箱前知道首选/最小尺寸,则进行设置。

to set its minimum size to its new preferred size 将其最小尺寸设置为其新的首选尺寸

Certainly possible, but whether it is useful ... . 当然可以,但是是否有用。 You can override the getMininumSize() method to return super.getPreferredSize(); 您可以重写getMininumSize()方法以返回super.getPreferredSize();

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

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