【Swing基础】setPreferredSize和setSize的区别及用法
来源:互联网 发布:阿里云空间怎么样 编辑:程序博客网 时间:2024/06/06 07:06
首先看 @那十无忧 的【Swing基础】布局管理器 - LayoutManager
http://tieba.baidu.com/p/2101752057
我以前很喜欢borderlayout的布局方式,每次想特别调整每个区域的大小,但是每次将一个panel放入到north或者其他4个区域时,总是达不到想要的效果,刚刚才发现原来setPreferredSize和setSize是有区别的。
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,宽度随窗口变化。
http://tieba.baidu.com/p/2101752057
我以前很喜欢borderlayout的布局方式,每次想特别调整每个区域的大小,但是每次将一个panel放入到north或者其他4个区域时,总是达不到想要的效果,刚刚才发现原来setPreferredSize和setSize是有区别的。
原因:
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)的时候可以使用这三个方法控制布局。
区分好这两个不同点之后,我相信你的布局会更随心所欲。
英文来源:http://stackoverflow.com/questions/1783793/java-difference-between-the-setpreferredsize-and-setsize-methods-in-compone
- 【Swing基础】setPreferredSize和setSize的区别及用法
- 【Swing基础】setPreferredSize和setSize的区别及用法
- 【Swing基础】setPreferredSize和setSize的区别及用法
- setPreferredSize和setSize的区别及用法
- swing 中setSize和setPreferredSize的区别
- swing中setSize和setPreferredSize区别
- swing中setSize与setPreferredsize的区别
- setSize和setPreferredSize的区别与应用
- setPreferredSize和的setSize和setbounds的用法
- 自定义布局管理器, setSize和setPreferredSize的区别
- 自定义布局管理器, setSize和setPreferredSize的区别
- java setPreferredSize和setsize,setLocation,setBounds方法的不同
- java setPreferredSize和setsize,setLocation,setBounds方法的不同
- setPreferredSize---setSize
- setSize()和setPreferredSize(new Dimension())使用小结
- setPreferredSize()、getPreferredSize()与pack()的配合使用以及setSize()方法
- CArray::SetSize 的用法
- JButton中的setPreferredSize() 与 setSize()
- jQuery包装集
- ModSecurity and ModSecurity Core Rule Set Multipart Bypasses
- SAP项目中开发用到的BAPI
- 实战解决问题开发出功能固然重要--->基本功锤炼和技术的实现原理,也非常重要
- 指针与数组
- 【Swing基础】setPreferredSize和setSize的区别及用法
- winform多个panel在设计时编辑的问题
- java设计模式一(单例模式singleton)
- java网络编程--解析通过web.xml文件控制利用过滤器实现编码格式步骤
- UML中Include与Extend的关系
- ios合并模拟器和真机静态库
- IOS面试题之一些概念的区别
- 转:使用matlab绘画柱状图,且使用不同的图案填充
- Sap abap 字符串处理