CardLayout layout of the Java Swing layout manager

Source: Internet
Author: User

CardLayout layout:
The CardLayout layout Manager can help the user to handle two or more members sharing the same display space, which divides the container into layers, each layer showing the size of the entire container, but only one component is allowed per layer, and the panel can be used for each layer to implement a complex user interface.

Treat each component in the container as a card. Only one card can be seen at a time, and the container acts as a stack of cards. When the container is first displayed, the first component to be added to the CardLayout object is the visible component.

The order of the cards is determined by the order of the component objects themselves within the container. CardLayout defines a set of methods that allow applications to browse the cards sequentially or display the specified card. The Addlayoutcomponent (java.awt.Component, Java.lang.Object) method can be used to associate a string identifier with a given card for quick random access.

1. Constructor function

CardLayout (): Creates 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 for this card layout.

the (Container Parent): The first card that flips to 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): Flip to the last card in the container.

3. Examples

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");
		P1 = new Panel ();
		P2 = new Panel ();

		c = new CardLayout ();
		P1.setlayout (c);
		for (int i = 0; i < name.length i++) {p1.add (name[i], new Button (Name[i));
		///control displays the last button previous = New button ("previous");
			Previous.addactionlistener (new ActionListener () {public void actionperformed (ActionEvent e) {c.previous (P1);
		}
		});
		control displays the Next button next = New button ("Next sheet");
			Next.addactionlistener (new ActionListener () {public void actionperformed (ActionEvent e) {c.next (P1);
		}
		});
		Controls the button first = New button ("the first") that displays the number one. FirsT.addactionlistener (new ActionListener () {public void actionperformed (ActionEvent e) {C.first (P1);
		}
		});
		Control button last = New button ("Last Sheet")
			Last.addactionlistener (new ActionListener () {public void actionperformed (ActionEvent e) {c.last (P1);
		}
		});
		Buttons Third = New Button ("third") displayed according to the card name;
			Third.addactionlistener (new ActionListener () {public void actionperformed (ActionEvent e) {c.show (P1, "3");
		}
		});
		P2.add (previous);
		P2.add (next);
		P2.add (a);
		P2.add (last);
		P2.add (third);
		F.add (p1);//default added to intermediate 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. The result



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.