CardLayout layout of the Java Swing layout manager

Source: Internet
Author: User

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

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.