setSize()v / s setPreferredSize()和pack() [英] setSize() v/s setPreferredSize() and pack()

查看:116
本文介绍了setSize()v / s setPreferredSize()和pack()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不清楚 setSize() setPreferredSize()之间的区别。
如果我使用 setSize()而不是 setPreferredSize(),会发生什么。

I am not clear about what is the difference between setSize() and setPreferredSize(). what would happen if i use setSize() instead of setPreferredSize().

pack()方法究竟做了什么?

推荐答案

在窗口上调用 pack()将根据其包含组件的preferredSize调整它的大小。它将尽可能小,但考虑到其组件的preferredSize和布局。
如果你只是随机使用 frame.setSize(),那么添加到内容窗格的组件将扩展/收缩以适应可用空间,这意味着首选可以覆盖每个组件的大小。

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()设置组件的大小和 setPreferredSize 设置首选大小.Layoutmanager将尝试为您的组件安排那么多空间。
这取决于你是否使用布局管理器......

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 ...

参见 Java:组件中setPreferredSize()和setSize()方法之间的差异

这篇关于setSize()v / s setPreferredSize()和pack()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆