public class SpringLayout extends Object implements LayoutManager2
SpringLayout
勾画出其关联容器的孩子根据一组约束条件。看到
SpringLayout
实例使用的java教程
How to Use SpringLayout。
每个约束,由Spring
对象表示,控制垂直或水平距离两组件之间的边缘。边缘可以属于容器的任何子容器,也可以属于容器本身。例如,可以表示一个组件的允许宽度,使用一个约束,控制的西部(左)和东部(右)的组件的边缘之间的距离。用于一个组件的允许的Y坐标可以通过约束组件的北部(顶部)边缘的组件和它的容器的北边缘之间的距离来表示。
一个SpringLayout
-controlled容器的每一个孩子,以及容器本身,都有一套与之相关的约束。这些限制是由SpringLayout.Constraints
对象表示。默认情况下,SpringLayout
创建的约束,使其相关组件具有最小和最大尺寸,优先返回组件的Component.getMinimumSize()
,Component.getPreferredSize()
,和Component.getMaximumSize()
方法。X和Y的位置最初是不受限的,所以直到你约束他们的Component
将位于0,0相对于父Container
的Insets
。
你可以在几个方面改变一个组件的约束。你可以使用其中的putConstraint
方法建立弹簧连接两部件边缘在同一容器中。或者你可以使用getConstraints
然后修改它的一个或多个弹簧得到适当的SpringLayout.Constraints
对象。或者你可以得到一个组件使用getConstraint
特定边缘的春天,和修改。您也可以将自己的SpringLayout.Constraints
对象与指定对象的约束,当你添加组件到容器中的组件(使用Container.add(Component, Object)
)。
代表每个约束的Spring
对象有一个最低的,首选,最大,和电流值。春天的电流值在最大值和最小值之间,按照Spring.sum(javax.swing.Spring, javax.swing.Spring)
方法描述的计算公式。当最小值、优选值和最大值是相同的时,当前的值总是等于它们;这种不灵活的弹簧称为支柱。你可以使用工厂方法创建Struts Spring.constant(int)
。的Spring
类还提供了用于创建其他类型的弹簧厂的方法,包括依赖于其他弹簧弹簧。
在SpringLayout
,每个边缘的位置是依赖只是另一个边缘的位置。如果随后添加了一个约束,以创建一个新的绑定的边缘,以前的绑定被丢弃,边缘仍然依赖于一个单一的边缘。泉水只能连接的容器及其直系子女之间的边缘;行为的SpringLayout
当提出约束连接不同容器部件边缘(内部或外部)是不确定的。
Note: Unlike many layout managers,SpringLayout
doesn't automatically set the location of the components it manages. If you hand-code a GUI that usesSpringLayout
, remember to initialize component locations by constraining the west/east and north/south locations.Depending on the constraints you use, you may also need to set the size of the container explicitly.
尽管SpringLayout
简单,它可以模仿其他的布局管理器的行为。一些功能,如断线的FlowLayout
,你需要创建一个类的子类的Spring
专用。
SpringLayout
还提供了一种方法来解决许多困难的布局问题,不能用Box
es嵌套组合解决。就是说,SpringLayout
荣誉LayoutManager2
合同正确,所以可以嵌套在其他的布局管理器-技术,可以很好的创建约束的其他布局经理暗示。
对SpringLayout
布局操作的渐近复杂性约束的数目是线性的(和/或部件)。
警告:序列化该类的对象与以后的Swing版本不兼容。当前的序列化支持适用于短期贮藏或RMI运行相同Swing版本的应用程序之间。为1.4,为所有JavaBeans™长期存储的支持已被添加到java.beans
包。请看XMLEncoder
。
Spring
,
SpringLayout.Constraints
Modifier and Type | Class and Description |
---|---|
static class |
SpringLayout.Constraints
一个
Constraints 对象持有支配方式的组件的大小和容器中的
SpringLayout 控制位置变化的限制。
|
Modifier and Type | Field and Description |
---|---|
static String |
BASELINE
指定组件的基线。
|
static String |
EAST
指定一个组件的边框的右边缘。
|
static String |
HEIGHT
指定一个组件的边框的高度。
|
static String |
HORIZONTAL_CENTER
指定一个组件的边框的水平中心。
|
static String |
NORTH
指定一个组件的边框的顶部边缘。
|
static String |
SOUTH
指定一个组件的边框的底部边缘。
|
static String |
VERTICAL_CENTER
指定组件的边框的垂直中心。
|
static String |
WEST
指定一个组件的边框的左边缘。
|
static String |
WIDTH
指定一个组件的边框的宽度。
|
Constructor and Description |
---|
SpringLayout()
构建了一种新的
SpringLayout 。
|
Modifier and Type | Method and Description |
---|---|
void |
addLayoutComponent(Component component, Object constraints)
如果
constraints 是
SpringLayout.Constraints 实例,将约束指定的组件。
|
void |
addLayoutComponent(String name, Component c)
没有效果,因为这个布局管理器不使用一个每个组件字符串。
|
Spring |
getConstraint(String edgeName, Component c)
返回弹簧控制组件的指定边缘与母公司的顶部或左边缘之间的距离。
|
SpringLayout.Constraints |
getConstraints(Component c)
返回指定组件的约束。
|
float |
getLayoutAlignmentX(Container p)
返回通道(中心)。
|
float |
getLayoutAlignmentY(Container p)
返回通道(中心)。
|
void |
invalidateLayout(Container p)
无效的布局,表明如果布局管理器已缓存的信息应该被丢弃。
|
void |
layoutContainer(Container parent)
指定指定的容器。
|
Dimension |
maximumLayoutSize(Container parent)
计算指定容器的最大大小尺寸,给出它所包含的组件。
|
Dimension |
minimumLayoutSize(Container parent)
计算指定容器的最小尺寸尺寸,给出它所包含的组件。
|
Dimension |
preferredLayoutSize(Container parent)
计算指定容器的首选大小尺寸,给出它所包含的组件。
|
void |
putConstraint(String e1, Component c1, int pad, String e2, Component c2)
链接边缘
e1 组件
c1 组件的
c2 边缘
e2 ,与边缘之间的固定距离。
|
void |
putConstraint(String e1, Component c1, Spring s, String e2, Component c2)
链接边缘
e1 组件的组件
c2 边缘
e2
c1 。
|
void |
removeLayoutComponent(Component c)
移除与指定组件关联的约束。
|
public static final String NORTH
public static final String SOUTH
public static final String EAST
public static final String WEST
public static final String HORIZONTAL_CENTER
public static final String VERTICAL_CENTER
public static final String BASELINE
public static final String WIDTH
public static final String HEIGHT
public void addLayoutComponent(String name, Component c)
addLayoutComponent
接口
LayoutManager
name
要与组件关联的字符串
c
-要添加的组件
public void removeLayoutComponent(Component c)
removeLayoutComponent
接口
LayoutManager
c
-从容器中移除的组件
public Dimension minimumLayoutSize(Container parent)
LayoutManager
minimumLayoutSize
接口
LayoutManager
parent
-被设置的组件
LayoutManager.preferredLayoutSize(java.awt.Container)
public Dimension preferredLayoutSize(Container parent)
LayoutManager
preferredLayoutSize
接口
LayoutManager
parent
-容器被设置
LayoutManager.minimumLayoutSize(java.awt.Container)
public Dimension maximumLayoutSize(Container parent)
LayoutManager2
maximumLayoutSize
接口
LayoutManager2
Component.getMaximumSize()
,
LayoutManager
public void addLayoutComponent(Component component, Object constraints)
constraints
是
SpringLayout.Constraints
实例,将约束指定的组件。
addLayoutComponent
接口
LayoutManager2
component
-添加组件
constraints
-组件的约束
SpringLayout.Constraints
public float getLayoutAlignmentX(Container p)
getLayoutAlignmentX
接口
LayoutManager2
public float getLayoutAlignmentY(Container p)
getLayoutAlignmentY
接口
LayoutManager2
public void invalidateLayout(Container p)
LayoutManager2
invalidateLayout
接口
LayoutManager2
public void putConstraint(String e1, Component c1, int pad, String e2, Component c2)
e1
组件
c1
组件的
c2
边缘
e2
,与边缘之间的固定距离。这个约束会导致分配
值(E1、C1)=价值(E2、C2)+垫所有后续的布局操作期间发生。
e1
-依赖的边缘
c1
-依赖的组件
pad
依赖和锚之间的固定距离
e2
-锚的边缘
c2
-锚组件
putConstraint(String, Component, Spring, String, Component)
public void putConstraint(String e1, Component c1, Spring s, String e2, Component c2)
e1
组件的组件
c2
边缘
e2
c1
。边缘
(e2, c2)
变化值,边缘
(e1, c1)
计算将以(春季)和
(e2, c2)
和
s
。每边必须是下列值之一:
SpringLayout.NORTH
,
SpringLayout.SOUTH
,
SpringLayout.EAST
,
SpringLayout.WEST
,
SpringLayout.VERTICAL_CENTER
,
SpringLayout.HORIZONTAL_CENTER
或
SpringLayout.BASELINE
。
e1
-依赖的边缘
c1
-依赖的组件
s
链接依赖和锚的春天
e2
-锚的边缘
c2
-锚组件
putConstraint(String, Component, int, String, Component)
,
NORTH
,
SOUTH
,
EAST
,
WEST
,
VERTICAL_CENTER
,
HORIZONTAL_CENTER
,
BASELINE
public SpringLayout.Constraints getConstraints(Component c)
GridBagLayout
getConstraints
方法,此方法不克隆的限制。如果没有约束已经与此组件,该方法返回一个默认约束的对象定位在0,0相对于父的插图和它的宽度/高度约束的最小,最大,和组件的首选大小。该方法被调用时,该方法不被冻结的大小特性;相反,该方法返回一个约束对象,该对象的特征跟踪组件的特性,因为它们改变。
c
-组件的约束将被退回
public Spring getConstraint(String edgeName, Component c)
SpringLayout
这样的布局操作总是终止。
edgeName
-必须是一个
SpringLayout.NORTH
,
SpringLayout.SOUTH
,
SpringLayout.EAST
,
SpringLayout.WEST
,
SpringLayout.VERTICAL_CENTER
,
SpringLayout.HORIZONTAL_CENTER
或
SpringLayout.BASELINE
c
的边缘弹簧所需组件
NORTH
,
SOUTH
,
EAST
,
WEST
,
VERTICAL_CENTER
,
HORIZONTAL_CENTER
,
BASELINE
public void layoutContainer(Container parent)
LayoutManager
layoutContainer
接口
LayoutManager
parent
-容器被设置
Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2014, Oracle and/or its affiliates. All rights reserved.