Java:组件中 setPreferredSize() 和 setSize() 方法的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1783793/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java: Difference between the setPreferredSize() and setSize() methods in components
提问by David Robles
Ok, I read the Java Documentation and I just can't figure out what is the main difference between those two methods. Sometimes I used setSize()
, sometimes setPreferredSize()
, sometimes one does what I want, sometimes the other.
好的,我阅读了 Java 文档,但我无法弄清楚这两种方法之间的主要区别是什么。有时我使用setSize()
,有时setPreferredSize()
,有时一个做我想做的,有时另一个。
So, what is the main difference between the two? Which one should I use for JFrames
and JPanels
?
那么,两者的主要区别是什么?我应该使用哪一个JFrames
和JPanels
?
Thanks
谢谢
采纳答案by Sbodd
The short answer is: it's complicated.
简短的回答是:这很复杂。
The slightly longer answer is: use setSize()
if your component's parent has no layout manager, and setPreferredSize()
and its related setMinimumSize
and setMaximumSize
if it does.
稍长的答案是:使用setSize()
,如果你的组件父没有布局管理器,并setPreferredSize()
和其相关的setMinimumSize
和setMaximumSize
如果它。
setSize()
probably won't do anything if the component's parent is using a layout manager; the places this will typically have an effect would be on top-level components (JFrames
and JWindows
) and things that are inside of scrolled panes
. You also must call setSize()
if you've got components inside a parent without a layout manager.
setSize()
如果组件的父级使用布局管理器,则可能不会做任何事情;这通常会产生影响的地方是顶级组件(JFrames
和JWindows
)以及scrolled panes
. setSize()
如果您的父组件中有没有布局管理器的组件,您也必须调用。
As a general rule, setPreferredSize()
should do the "right thing" if you've got a layout manager; most layout managers work by getting the preferred (as well as minimum and maximum) sizes of their components, and then using setSize()
and setLocation()
to position those components according to the layout's rules. So (as an example) a BorderLayout
will try to make the bounds of its "north" region equal to the preferred size
of its north component - they may end up larger or smaller than that, depending on the size of the jframe
, the size of the other components in the layout, and so on.
一般来说,setPreferredSize()
如果你有一个布局管理器,就应该做“正确的事”;大多数布局管理器的工作方式是获取其组件的首选(以及最小和最大)尺寸,然后根据布局规则使用setSize()
和setLocation()
定位这些组件。因此(作为示例)aBorderLayout
将尝试使其“北部”区域的边界等于preferred size
其北部分量的边界- 它们最终可能大于或小于该值,具体取决于jframe
的大小,其他组件的大小在布局等方面。
回答by Glen
setSize
will resize the component to the specified size.
setSize
将组件调整到指定的大小。
setPreferredSize
sets the preferred size. The component may not actually be this size depending on the size of the container it's in, or if the user re-sized the component manually.
setPreferredSize
设置首选大小。组件实际上可能不是这个大小,这取决于它所在容器的大小,或者用户是否手动重新调整了组件的大小。
回答by miku
IIRC ...
国际研究中心...
setSize
sets the size of the component.
setSize
设置组件的大小。
setPreferredSize
sets the preferred size.
The Layoutmanager will try to arrange that much space for your component.
setPreferredSize
设置首选大小。Layoutmanager 将尝试为您的组件安排那么多空间。
It depends on whether you're using a layout manager or not ...
这取决于您是否使用布局管理器...
回答by Varun
setSize()
or setBounds()
can be used when no layout manager is being used.
setSize()
或者setBounds()
可以在没有使用布局管理器时使用。
However, if you are using a layout manager you can provide hints to the layout manager using the setXXXSize()
methods like setPreferredSize()
and setMinimumSize()
etc.
但是,如果您使用的是布局管理器可以提供使用提示,布局管理器setXXXSize()
类似的方法setPreferredSize()
和setMinimumSize()
等。
And be sure that the component's container uses a layout manager that respects the requested size. The FlowLayout
, GridBagLayout
, and SpringLayout
managers use the component's preferred size (the latter two depending on the constraints you set), but BorderLayout
and GridLayout
usually don't.If you specify new size hints for a component that's already visible, you need to invoke the revalidate method on it to make sure that its containment hierarchy is laid out again. Then invoke the repaint method.
并确保组件的容器使用符合请求大小的布局管理器。的FlowLayout
,GridBagLayout
和SpringLayout
管理人员使用组件的首选大小(后两者因限制设置),但BorderLayout
和GridLayout
平时don't.If您指定的新大小提示为已在可见的一个组成部分,你需要调用重新验证方法上它以确保再次布置其收容层次结构。然后调用重绘方法。