setPreferredSize和setSize的区别及用法
2015-10-18 15:34
162 查看
原文:http://tieba.baidu.com/p/2172505897
我以前很喜欢borderlayout的布局方式,每次想特别调整每个区域的大小,但是每次将一个panel放入到north或者其他4个区域时,总是达不到想要的效果,刚刚才发现原来setPreferredSize和setSize是有区别的。
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() 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.
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 frame, the size of the other components in the layout, and so on
上面大概意思说的就是:
1.setPreferredSize需要在使用布局管理器的时候使用,布局管理器会获取空间的preferredsize,因而可以生效。例如borderlayout在north中放入一个panel,panel的高度可以通过这样实现:panel.setPreferredSize(new
Dimension(0, 100));这样就设置了一个高度为100的panel,宽度随窗口变化。
2.setSize,setLocation,setBounds方法需要在不使用布局管理器的时候使用,也就是setLayout(null)的时候可以使用这三个方法控制布局。
区分好这两个不同点之后,我相信你的布局会更随心所欲。
我以前很喜欢borderlayout的布局方式,每次想特别调整每个区域的大小,但是每次将一个panel放入到north或者其他4个区域时,总是达不到想要的效果,刚刚才发现原来setPreferredSize和setSize是有区别的。
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() 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.
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 frame, the size of the other components in the layout, and so on
上面大概意思说的就是:
1.setPreferredSize需要在使用布局管理器的时候使用,布局管理器会获取空间的preferredsize,因而可以生效。例如borderlayout在north中放入一个panel,panel的高度可以通过这样实现:panel.setPreferredSize(new
Dimension(0, 100));这样就设置了一个高度为100的panel,宽度随窗口变化。
2.setSize,setLocation,setBounds方法需要在不使用布局管理器的时候使用,也就是setLayout(null)的时候可以使用这三个方法控制布局。
区分好这两个不同点之后,我相信你的布局会更随心所欲。
相关文章推荐
- 去除Bootstrap输入框聚焦时的边框轮廓线
- JSP的内置对象
- 你不知道的JavaScript--Item11 arguments对象
- 你不知道的JavaScript--Item11 arguments对象
- effective-1
- js定时器 setTimeout
- jQuery 文件上传插件:uploadify、swfupload
- 废除Ajax向Fetch转型
- 详解js闭包
- 百度谷歌唯一解决方案--解决jQuery Validation插件 相同名称文本框只验证第一个
- .使用 HTML+CSS 实现如图布局,border-widht 5px,一个格子大小是 50*50,hover时候边框变为红色(兼容IE6+,考虑语义化的结构)
- fullpage.js
- Hadoop-2.3.0-cdh5.0.0 获取源码及编译
- CSS3动画
- FlatBuffers release 1.1.0
- javascript中获取Dajango里Tastypie所传过来的数据
- ASP.NET MVC Bootstrap模板选中菜单高亮显示当前项方法
- JSP自定义标签详解
- jQuery源码分析之css方法
- JQuery图片轮换器原理