Java's cardlayout

Source: Internet
Author: User

CardLayout allows us to create a layout that is roughly the same as a "card dialog box" in a more complex environment with real folder cards and a side encounter, and we have to press a card to bring the different dialog boxes to the front. This is not the case in AWT: CardLayout is a simple empty space and we are free to bring new cards to the front. (The Jfc/swing library, which includes a card-like pane, looks great, and we can handle all the details.)

1. Federated layout (combining layouts)
The following example is combined with more layout types, where initially only one layout manager appears to be quite difficult for a piece or application to manipulate. This is true, but if we create more Panel objects, each panel can have a layout manager and use the patch or application layout Manager as if it were integrated into a program or application. This gives us more flexibility as in the following program:
 

: Cardlayout1.java//Demonstrating the CardLayout import java.awt.*;

Import Java.applet.Applet;
    Class Buttonpanel extends Panel {buttonpanel (String id) {setlayout (New BorderLayout ());
  Add ("Center", New button (ID)); The public class CardLayout1 extends Applet {button A = New button ("a"), Second = New button ("second")
  , third = New Button ("third");
  panel cards = new Panel ();
  CardLayout cl = new CardLayout ();
    public void init () {setlayout (New BorderLayout ());
    panel p = new Panel ();
    P.setlayout (New FlowLayout ());
    P.add (a);
    P.add (second);
    P.add (third);
    Add ("North", p);
    Cards.setlayout (CL);
    Cards.add ("The One", New Buttonpanel ("the");
    Cards.add ("Second card", New Buttonpanel ("The Second One"));
    Cards.add ("Third card", New Buttonpanel ("The third One"));
  Add ("Center", cards); Public boolean action (Event evt, Object Arg) {if (evt.target.equals)) {Cl.first (cards);
      else if (evt.target.equals (second)) {Cl.first (cards);
    Cl.next (cards);
    else if (Evt.target.equals (third)) {Cl.last (cards);
    else return super.action (EVT, ARG);
  return true; }
} ///:~

This example first creates a new type of panel: Bottonpanel (button panel). It includes a separate button, placed in the center of the borderlayout, which means it will fill the entire panel. The label on the button will let us know that we are on that panel on the cardlayout.
In the program, the Panel card will hold the card and the layout manager CL because cardlayout must form classes because we need to access these handles when we need to process the cards.
This patch becomes the default flowlayout that uses BorderLayout to replace it, creates a panel to hold three buttons (using FlowLayout), and the panel is positioned "north" at the end of the program. The card panel is added to the "Center" of the program, effectively occupying the rest of the panel.
When we add bottonpanels (or any other component we want) to the card panel, the first argument to the Add () method is not "North", "South", and so on. Instead, it is a string that describes the card. If we want to swipe that card using a string, we can use it, although the string will not appear anywhere in the card. The method used is not to use the action (), and the use of the primary (), next (), and Last () methods. Please check our file for other methods.
In Java, some of the card-style panel structures that are used are very important, because (as we'll see later) the pop-up dialog boxes used in program programming are very frustrating. For Java version 1.0 of the program, CardLayout is the only effective way to get a lot of different "pop-up" forms.

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.