swing布局管理器文档

上传人:F****n 文档编号:97955719 上传时间:2019-09-07 格式:DOC 页数:17 大小:102.50KB
返回 下载 相关 举报
swing布局管理器文档_第1页
第1页 / 共17页
swing布局管理器文档_第2页
第2页 / 共17页
swing布局管理器文档_第3页
第3页 / 共17页
swing布局管理器文档_第4页
第4页 / 共17页
swing布局管理器文档_第5页
第5页 / 共17页
点击查看更多>>
资源描述

《swing布局管理器文档》由会员分享,可在线阅读,更多相关《swing布局管理器文档(17页珍藏版)》请在金锄头文库上搜索。

1、天一Star整体编著一, 概述实际上,在Swing使用的布局管理器是在Awt中的,这也不是什么奇怪的事,因为Swing本身也在AWT的基础上开发的。容器仅仅记录其包含的组件,而布局管理器则指明了容器中组件的位置和尺寸大小。通过布局管理器,您只需要告知您想放置的组件同其他组件的相对位置即可,这有助于用户实现软件的平台无关性。AWT提供了五种类型的布局管理器。BorderLayout布局管理器:该管理器将容器分到北、南、东、西、中五个区域,当您向容器中添加组件时,您要告诉BorderLayout将组件放置到五个域中的某个域。CardLayout布局管理器:该布局管理器将加入到容顺中的组件视为卡片栈

2、,把每个组件放置在一个单独的卡片上,而每次只能看见一张卡片。FlowLayout布局管理器:该布局管理器将组件从左到右、从上到下放置。GridLayout布局管理器:该布局管理器将容器分成相同尺寸的网格,将组件按从左到右、从上到下的顺序放置在网格中GridBagLayout布局管理器:和上面的GridLayout布局管理器不一样的是,一个组件不只是占一个网格位置,加入组件时,必须指明一个对应的参数。二, 布局管理器的工作过程每个容器保存一个对一个布局管理器的引用,这个布局管理器对容器中的组件进行定位和整形。容器只是可以含有其他组件的组件。如图10-2所描述的,AWT提供了一些扩展Contain

3、er的类。每个容器都访问一个布局管理器,该布局管理器有责任对容器中的组件进行定位和整形。当发生一个可以引起容器布置它的组件(例如调整一个K窗口的大小)的事件时,调用容器的布局管理器布置容器内的组件。从原则上讲,容器把布置它的组件的工作授权给一个布局管理器。不同的布局管理器使用不同的算法布置组件,容器可以通过使用适当的布局管理器自由选择布置算法。这种定义一个算法集并把这个算法封装在一个类里的技术通称为策略模式(strategy pattern)。我们来看一下:Container类的部分实现,/* * 我们可以看到Container扩展了Compoent类*/public class Contai

4、ner extends Component /* * 容器内组件的数量,此值可为null */ int ncomponents; /* * 容器内的组件集,它把它们保存一个数组中 */ Component component = new Component0; /* *容器的布局管理器 */ LayoutManager layoutMgr;/*当我们向容器中add组件时,实际上调用的就是这方法,这里只给出了和布局管理有关的部分*/ protected void addImpl(Component comp, Object constraints, int index) synchronized

5、 (getTreeLock() /*通知布局管理器已经add了组件 */ if (layoutMgr != null) if (layoutMgr instanceof LayoutManager2) (LayoutManager2)layoutMgr).addLayoutComponent(comp, constraints); else if (constraints instanceof String) layoutMgr.addLayoutComponent(String)constraints, comp); /*当我们删除组件时,要调用这个方法,而它要遍布容器内的所有组件,然后根据

6、索引值调用remove(int *index)方法。*/public void remove(Component comp) synchronized (getTreeLock() if (comp.parent = this) Component component = ponent; for (int i = ncomponents; -i = 0; ) if (componenti = comp) remove(i); /*根据索引值删除容器的组件,可以看到该方法调用了布局管理器的removeLayoutComponent()*/public void remove(int index)

7、 synchronized (getTreeLock() if (index = ncomponents) throw new ArrayIndexOutOfBoundsException(index); Component comp = componentindex; if (layoutMgr != null) layoutMgr.removeLayoutComponent(comp); if (valid) invalidate(); /*返回容器当前的布局管理器*/ public LayoutManager getLayout() return layoutMgr; /*这个是我们经常

8、调用的方法,设置容器的布局管理器*/ public void setLayout(LayoutManager mgr) layoutMgr = mgr;if (valid) invalidate();public void doLayout() /让布局管理器进行布局组件layout(); Deprecated public void layout() LayoutManager layoutMgr = this.layoutMgr;if (layoutMgr != null) layoutMgr.layoutContainer(this); /* * Invalidates the cont

9、ainer. The container and all parents above it are marked as needing * to be laid out. This method can be called often, so it needs to execute quickly. */ public void invalidate() LayoutManager layoutMgr = this.layoutMgr; if (layoutMgr instanceof LayoutManager2) LayoutManager2 lm = (LayoutManager2) l

10、ayoutMgr; lm.invalidateLayout(this); super.invalidate(); /*取得容器的首选大小*/public Dimension getPreferredSize() return preferredSize();Deprecatedpublic Dimension preferredSize() Dimension dim = prefSize; if (dim = null | !(isPreferredSizeSet() | isValid() synchronized (getTreeLock() prefSize = (layoutMgr

11、!= null) ? layoutMgr.preferredLayoutSize(this) : super.preferredSize(); dim = prefSize; if (dim != null) return new Dimension(dim); else return dim; /*取得容器的最小大小*/public Dimension getMinimumSize() return minimumSize();Deprecatedpublic Dimension minimumSize() Dimension dim = minSize; if (dim = null |

12、!(isMinimumSizeSet() | isValid() synchronized (getTreeLock() minSize = (layoutMgr != null) ? layoutMgr.minimumLayoutSize(this) : super.minimumSize(); dim = minSize; if (dim != null) return new Dimension(dim); else return dim; /*取得容器的最大大小*/public Dimension getMaximumSize() Dimension dim = maxSize; if (dim = null | !(isMaximumSizeSet() | isValid() synchronized (getTreeLock() if (layoutMgr instanceof LayoutManager2) LayoutManager2 lm = (LayoutManager2) layoutMgr; maxSize = lm.maximumLayoutSize(this); else maxSize = super.getMaximumSize(); dim = maxSiz

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 办公文档 > 教学/培训

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号