public class CardLayout extends Objectimplements LayoutManager2 , Serializable
CardLayout object is a layout manager for a container. It treats each component in the container as a card. Only one card is visible at a time, and the container acts as a stack of cards. The first component added to a
CardLayout object is the visible component when the container is first displayed.
The ordering of cards is determined by the container's own internal ordering of its component objects. CardLayout defines a set of methods that allow an application to flip through these cards sequentially, or to show a specified card. The addLayoutComponent(java.awt.Component, java.lang.Object) method can be used to associate a string identifier with a given card for fast random access.
Container,
Serialized Form
| Constructor and Description |
|---|
CardLayout()
Creates a new card layout with gaps of size zero.
|
CardLayout(int hgap, int vgap)
Creates a new card layout with the specified horizontal and vertical gaps.
|
| Modifier and Type | Method and Description |
|---|---|
void |
addLayoutComponent(Component
Adds the specified component to this card layout's internal table of names.
|
void |
addLayoutComponent(String
Deprecated.
replaced by
addLayoutComponent(Component, Object).
|
void |
first(Container
Flips to the first card of the container.
|
int |
getHgap()
Gets the horizontal gap between components.
|
float |
getLayoutAlignmentX(Container
Returns the alignment along the x axis.
|
float |
getLayoutAlignmentY(Container
Returns the alignment along the y axis.
|
int |
getVgap()
Gets the vertical gap between components.
|
void |
invalidateLayout(Container
Invalidates the layout, indicating that if the layout manager has cached information it should be discarded.
|
void |
last(Container
Flips to the last card of the container.
|
void |
layoutContainer(Container
Lays out the specified container using this card layout.
|
Dimension |
maximumLayoutSize(Container
Returns the maximum dimensions for this layout given the components in the specified target container.
|
Dimension |
minimumLayoutSize(Container
Calculates the minimum size for the specified panel.
|
void |
next(Container
Flips to the next card of the specified container.
|
Dimension |
preferredLayoutSize(Container
Determines the preferred size of the container argument using this card layout.
|
void |
previous(Container
Flips to the previous card of the specified container.
|
void |
removeLayoutComponent(Component
Removes the specified component from the layout.
|
void |
setHgap(int hgap)
Sets the horizontal gap between components.
|
void |
setVgap(int vgap)
Sets the vertical gap between components.
|
void |
show(Container
Flips to the component that was added to this layout with the specified
name, using
addLayoutComponent.
|
String |
toString()
Returns a string representation of the state of this card layout.
|
public CardLayout()
public CardLayout(int hgap,
int vgap)
hgap - the horizontal gap.
vgap - the vertical gap.
public int getHgap()
setHgap(int),
getVgap()
public void setHgap(int hgap)
hgap - the horizontal gap between components.
getHgap(),
setVgap(int)
public int getVgap()
setVgap(int),
getHgap()
public void setVgap(int vgap)
vgap - the vertical gap between components.
getVgap(),
setHgap(int)
public void addLayoutComponent(Componentcomp, Object constraints)
constraints must be a string. The card layout stores this string as a key-value pair that can be used for random access to a particular card. By calling the
show method, an application can display the component with the specified name.
addLayoutComponent in interface
LayoutManager2
comp - the component to be added.
constraints - a tag that identifies a particular card in the layout.
IllegalArgumentException - if the constraint is not a string.
show(java.awt.Container, java.lang.String)
@Deprecated public void addLayoutComponent(Stringname, Component comp)
addLayoutComponent(Component, Object).
LayoutManager
comp to the layout, associating it with the string specified by
name.
addLayoutComponent in interface
LayoutManager
name - the string to be associated with the component
comp - the component to be added
public void removeLayoutComponent(Componentcomp)
removeLayoutComponent in interface
LayoutManager
comp - the component to be removed.
Container.remove(java.awt.Component) ,
Container.removeAll()
public DimensionpreferredLayoutSize(Container parent)
preferredLayoutSize in interface
LayoutManager
parent - the parent container in which to do the layout
Container.getPreferredSize() ,
minimumLayoutSize(java.awt.Container)
public DimensionminimumLayoutSize(Container parent)
minimumLayoutSize in interface
LayoutManager
parent - the parent container in which to do the layout
Container.doLayout() ,
preferredLayoutSize(java.awt.Container)
public DimensionmaximumLayoutSize(Container target)
maximumLayoutSize in interface
LayoutManager2
target - the component which needs to be laid out
Container,
minimumLayoutSize(java.awt.Container),
preferredLayoutSize(java.awt.Container)
public float getLayoutAlignmentX(Containerparent)
public float getLayoutAlignmentY(Containerparent)
public void invalidateLayout(Containertarget)
public void layoutContainer(Containerparent)
Each component in the parent container is reshaped to be the size of the container, minus space for surrounding insets, horizontal gaps, and vertical gaps.
layoutContainer in interface
LayoutManager
parent - the parent container in which to do the layout
Container.doLayout()
public void first(Containerparent)
parent - the parent container in which to do the layout
last(java.awt.Container)
public void next(Containerparent)
parent - the parent container in which to do the layout
previous(java.awt.Container)
public void previous(Containerparent)
parent - the parent container in which to do the layout
next(java.awt.Container)
public void last(Containerparent)
parent - the parent container in which to do the layout
first(java.awt.Container)
public void show(Containerparent, String name)
name, using
addLayoutComponent. If no such component exists, then nothing happens.
parent - the parent container in which to do the layout
name - the component name
addLayoutComponent(java.awt.Component, java.lang.Object)