Class CardLayout
- All Implemented Interfaces:
-
LayoutManager
,LayoutManager2
,Serializable
public class CardLayout extends Object implements 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.
- Since:
- 1.0
- See Also:
Constructor Summary
Constructor | Description |
---|---|
CardLayout() |
Creates a new card layout with gaps of size zero. |
CardLayout |
Creates a new card layout with the specified horizontal and vertical gaps. |
Method Summary
Modifier and Type | Method | Description |
---|---|---|
void |
addLayoutComponent |
Adds the specified component to this card layout's internal table of names. |
void |
addLayoutComponent |
Deprecated. |
void |
first |
Flips to the first card of the container. |
int |
getHgap() |
Gets the horizontal gap between components. |
float |
getLayoutAlignmentX |
Returns the alignment along the x axis. |
float |
getLayoutAlignmentY |
Returns the alignment along the y axis. |
int |
getVgap() |
Gets the vertical gap between components. |
void |
invalidateLayout |
Invalidates the layout, indicating that if the layout manager has cached information it should be discarded. |
void |
last |
Flips to the last card of the container. |
void |
layoutContainer |
Lays out the specified container using this card layout. |
Dimension |
maximumLayoutSize |
Returns the maximum dimensions for this layout given the components in the specified target container. |
Dimension |
minimumLayoutSize |
Calculates the minimum size for the specified panel. |
void |
next |
Flips to the next card of the specified container. |
Dimension |
preferredLayoutSize |
Determines the preferred size of the container argument using this card layout. |
void |
previous |
Flips to the previous card of the specified container. |
void |
removeLayoutComponent |
Removes the specified component from the layout. |
void |
setHgap |
Sets the horizontal gap between components. |
void |
setVgap |
Sets the vertical gap between components. |
void |
show |
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. |
Constructor Details
CardLayout
public CardLayout()
CardLayout
public CardLayout(int hgap, int vgap)
- Parameters:
-
hgap
- the horizontal gap. -
vgap
- the vertical gap.
Method Details
getHgap
public int getHgap()
- Returns:
- the horizontal gap between components.
- Since:
- 1.1
- See Also:
setHgap
public void setHgap(int hgap)
- Parameters:
-
hgap
- the horizontal gap between components. - Since:
- 1.1
- See Also:
getVgap
public int getVgap()
- Returns:
- the vertical gap between components.
- See Also:
setVgap
public void setVgap(int vgap)
- Parameters:
-
vgap
- the vertical gap between components. - Since:
- 1.1
- See Also:
addLayoutComponent
public void addLayoutComponent(Component comp, 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.- Specified by:
-
addLayoutComponent
in interfaceLayoutManager2
- Parameters:
-
comp
- the component to be added. -
constraints
- a tag that identifies a particular card in the layout. - Throws:
-
IllegalArgumentException
- if the constraint is not a string. - See Also:
addLayoutComponent
@Deprecated public void addLayoutComponent(String name, Component comp)
addLayoutComponent(Component, Object)
.LayoutManager
comp
to the layout, associating it with the string specified by name
.- Specified by:
-
addLayoutComponent
in interfaceLayoutManager
- Parameters:
-
name
- the string to be associated with the component -
comp
- the component to be added
removeLayoutComponent
public void removeLayoutComponent(Component comp)
- Specified by:
-
removeLayoutComponent
in interfaceLayoutManager
- Parameters:
-
comp
- the component to be removed. - See Also:
preferredLayoutSize
public Dimension preferredLayoutSize(Container parent)
- Specified by:
-
preferredLayoutSize
in interfaceLayoutManager
- Parameters:
-
parent
- the parent container in which to do the layout - Returns:
- the preferred dimensions to lay out the subcomponents of the specified container
- See Also:
minimumLayoutSize
public Dimension minimumLayoutSize(Container parent)
- Specified by:
-
minimumLayoutSize
in interfaceLayoutManager
- Parameters:
-
parent
- the parent container in which to do the layout - Returns:
- the minimum dimensions required to lay out the subcomponents of the specified container
- See Also:
maximumLayoutSize
public Dimension maximumLayoutSize(Container target)
- Specified by:
-
maximumLayoutSize
in interfaceLayoutManager2
- Parameters:
-
target
- the component which needs to be laid out - Returns:
- the maximum size of the container
- See Also:
getLayoutAlignmentX
public float getLayoutAlignmentX(Container parent)
- Specified by:
-
getLayoutAlignmentX
in interfaceLayoutManager2
- Parameters:
-
parent
- the target container - Returns:
- the x-axis alignment preference
getLayoutAlignmentY
public float getLayoutAlignmentY(Container parent)
- Specified by:
-
getLayoutAlignmentY
in interfaceLayoutManager2
- Parameters:
-
parent
- the target container - Returns:
- the y-axis alignment preference
invalidateLayout
public void invalidateLayout(Container target)
- Specified by:
-
invalidateLayout
in interfaceLayoutManager2
- Parameters:
-
target
- the target container
layoutContainer
public void layoutContainer(Container parent)
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.
- Specified by:
-
layoutContainer
in interfaceLayoutManager
- Parameters:
-
parent
- the parent container in which to do the layout - See Also:
first
public void first(Container parent)
- Parameters:
-
parent
- the parent container in which to do the layout - See Also:
next
public void next(Container parent)
- Parameters:
-
parent
- the parent container in which to do the layout - See Also:
previous
public void previous(Container parent)
- Parameters:
-
parent
- the parent container in which to do the layout - See Also:
last
public void last(Container parent)
- Parameters:
-
parent
- the parent container in which to do the layout - See Also:
show
public void show(Container parent, String name)
name
, using addLayoutComponent
. If no such component exists, then nothing happens.- Parameters:
-
parent
- the parent container in which to do the layout -
name
- the component name - See Also:
toString
public String toString()
© 1993, 2021, Oracle and/or its affiliates. All rights reserved.
Documentation extracted from Debian's OpenJDK Development Kit package.
Licensed under the GNU General Public License, version 2, with the Classpath Exception.
Various third party code in OpenJDK is licensed under different licenses (see Debian package).
Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
https://docs.oracle.com/en/java/javase/17/docs/api/java.desktop/java/awt/CardLayout.html
addLayoutComponent(Component, Object)
.