开发者

setSize() v/s setPreferredSize() and pack() [duplicate]

开发者 https://www.devze.com 2023-03-13 20:34 出处:网络
This question already has answers here: Java: Difference between the setPreferredSize() and setSize() methods in components
This question already has answers here: Java: Difference between the setPreferredSize() and setSize() methods in components (5 answers) Closed 6 years ago.

I am not clear about what is the difference between setSize() and setPreferredSize(). what would happen if i use setSize() instead of setP开发者_开发百科referredSize().


And what exactly does pack() method do ?


Calling pack() on a window will size it based on the preferredSize of its containing components. It will be as small as possible but taking into account the preferredSize and layout of its components. If you just randomly use frame.setSize(), then the components added to the content pane will expand/contract to fit the space available, which means the preferred size of each component may be overridden.

setSize() sets the size of the component and setPreferredSize sets the preferred size.The Layoutmanager will try to arrange that much space for your component. It depends on whether you're using a layout manager or not ...

See Java: Difference between the setPreferredSize() and setSize() methods in components

0

精彩评论

暂无评论...
验证码 换一张
取 消