CardLayout layout:
The CardLayout layout Manager can help users to handle two or more members to share the same display space, which divides the container into layers, each layer of display space occupies the size of the entire container, but only one component per layer is allowed, of course, each layer can use the Panel to implement a complex user interface
Treat each component in the container as a card. You can see only one card at a time, and the container acts as a stack of cards. The first component added to an CardLayout object is a visible component when the container is first displayed.
The order of the cards is determined by the order of the component objects themselves inside the container. CardLayoutdefines a set of methods that allow applications to browse these cards sequentially, or to display 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.
1. Constructors
CardLayout(): Create a new card layout with a spacing size of 0.
CardLayout(int hgap, int vgap): Creates a new card layout with the specified horizontal and vertical spacing.
2. Common methods
addLayoutComponent(Component comp, Object constraints): Adds the specified component to the internal name table of this card layout.
first(Container parent): Flips to the first card of the container.
getHgap(): Gets the horizontal spacing between components.
getLayoutAlignmentX(Container parent): Returns the alignment along the x axis.
getLayoutAlignmentY(Container parent): Returns the alignment along the y axis.
getVgap(): Gets the vertical spacing between components.
last(Container parent): Flips to the last card in the container.
3. Example
Import Java.awt.borderlayout;import java.awt.button;import Java.awt.cardlayout;import Java.awt.Frame;import Java.awt.panel;import Java.awt.event.actionevent;import Java.awt.event.actionlistener;import Javax.swing.JFrame; public class Calculator {JFrame F; Panel P1; Panel P2; String[] name = {"1", "2", "3", "4", "5"}; CardLayout c;public void Init () {f = new JFrame ("cardlayout instance");p 1 = new Panel ();p 2 = new Panel (), C = new CardLayout ();p 1. SetLayout (c); for (int i = 0; i < name.length; i++) {P1.add (name[i], new Button (Name[i]));} Controls the button that displays the previous one previous = New button ("previous");p Revious.addactionlistener (new ActionListener () {public void actionperformed (ActionEvent e) {c.previous (p1);}}); /control to display the button next to buttons next = New Button ("Next"); Next.addactionlistener (new ActionListener () {public void actionperformed ( ActionEvent e) {c.next (p1);}); /controls the button that displays the first one, the buttons first = New button ("First.addactionlistener"); new ActionListener () {public void actionperformed ( ActionEvent e) {C.first (p1);}); /control DisplayThe last button of the buttons, the last = New button (the "final One"); Last.addactionlistener (new ActionListener () {public void actionperformed ( ActionEvent e) {c.last (p1);}); /Button Third = New Button ("third") according to the card name; Third.addactionlistener (new ActionListener () {public void actionperformed (ActionEvent e) {c.show (P1, "3");}); P2.add (previous);p 2.add (next);p 2.add (first);p 2.add (last);p 2.add (third) F.add (p1);//default add to Middle F.add (P2, Borderlayout.south); f.setdefaultcloseoperation (jframe.exit_on_close); F.pack (); f.setvisible (true);} public static void Main (string[] args) {new Calculator (). init ();}}
4. Results
CardLayout layout of the Java Swing layout manager