Card layouts allow multiple components to share the same display space, and the relationships between components in a shared space are like stacks of cards, and components are stacked together, initially displaying the first added component of the space, and the methods provided by the CardLayout class can toggle the components displayed in that space.
1. Common constructor functions and methods of CardLayout class
2. Use the method provided by the CardLayout class to toggle the display of components in that space
method One step:
(1) Define containers that use card layouts
For example: Panel cardpanel=new panel ();
(2) Define Card object: CardLayout Layout object name =new cardlayout ();
For example: CardLayout card=new cardlayout ();
(3) To set the card layout using a container for the card layout:
Format: container name. setlayout (Layout object name);
For example: Cardpanel.setlayout (card);
(4) Set the components shown in the container
For example: for (int i = 0; i < 5; i++) {
Cardpanel.add (Newjbutton ("button" +i));
}
(5) Define the response event code to allow the container to display the corresponding component
Format:
N Layout object name. Next (container name) displays a component after the current component in the container, and if the current component is the last component added, the first component is displayed, that is, the card component appears to be circular.
n the name of the Layout object. First (container name) to display one component in the container
n the name of the layout object. Last (container name) displays the final component in the container
N Layout object name. Previous (container name) displays a component before the current component in the container, and if the current component is the first added component, the last component is displayed, that is, the card component display is circular.
For example:
Card.next (Cardpanel);
Card.previous (Cardpanel);
Card.first (Cardpanel);
Card.last (Cardpanel);
Example one: Card switching
Form default border layout, one panel with card layout, add five buttons to the panel, add to center location, another panel add two buttons, two buttons to add events to toggle components that display panels in center location
<span style= "FONT-SIZE:18PX;"
>//Cardlayout.java import java.awt.*;
Import javax.swing.*;
Import java.awt.event.*;//implement listener interface public class CardLayout Extendsjframe implements actionlistener{when introducing event Pack//Definition Classes
JButton Nextbutton;
JButton Prebutton;
Panel cardpanel=new Panel ();
Panel controlpapanel=new Panel ();
Defines the card layout object CardLayout card=new cardlayout ();
Defines the constructor public CardLayout () {super ("card layout manager");
SetSize (300, 200);
Setdefaultcloseoperation (Jframe.exit_on_close);
Setlocationrelativeto (NULL);
SetVisible (TRUE);
Set the Cardpanel Panel object for the card layout cardpanel.setlayout (cards); Loop, add five buttons to the Cardpanel Panel object//Because the Cardpanel panel object is a card layout, show only the components that were first added for (int i = 0; I & Lt 5; i++) {cardpanel.add (new JButton) ("button" +i));
}//Instantiate button object Nextbutton=new JButton ("next card");
Prebutton=new JButton ("last Card");
Registers the listener Nextbutton.addactionlistener for the button object (this);
Prebutton.addactionlistener (this);
Controlpapanel.add (Prebutton);
Controlpapanel.add (Nextbutton);
Defines the container object as the current form container object Container container=getcontentpane ();
Place the Cardpanel panel in the middle of the window boundary layout, and the window defaults to the Border layout container.add (Cardpanel,borderlayout.center);
Place the Controlpapanel panel on the south side of the window's border layout, Container.add (Controlpapanel,borderlayout.south); }//Implementation of the button's listening trigger when processing public void actionperformed (ActionEvent e) {//If the user clicks Nextbutto N, execute the statement if (E.getsource () ==nextbutton) {//Toggle CARDPA component after the current component in the ANEL panel//If the current component is the last component added, the first component is displayed, that is, the card component display is circular.
Card.next (Cardpanel);
} if (E.getsource () ==prebutton) {//Toggle a component before the current component in the Cardpanel panel
If the current component is the first component added, the last component is displayed, that is, the card component display is circular.
Card.previous (Cardpanel);
} public static void Main (string[] args) {cardlayout kapian=new cardlayout (); }}</span>
The program shows the results as shown in the following figure, click on the "previous", "Next" and so on buttons can display a different button in the upper panel.
method Two steps:
(1) Define containers that use card layouts
For example: Panel cardpanel=new panel ();
(2) Define Card object: CardLayout Layout object name =newcardlayout ();
For example: CardLayout card=new cardlayout ();
(3) To set the card layout using a container for the card layout:
Format: container name. setlayout (Card object name);
For example: Cardpanel.setlayout (card);
(4) Set the component shown in the container and name the corresponding card for the component
Format: container name. ADD (card name, component name)
For example: for (int i = 0; i < 4; i++) {
Cardpanel.add ("0", Newjbutton ("button" +i));
}
(5) Define the response event code to allow the container to display the corresponding component
Format: Card object name. Show (container name, card name)
For example: Card.show (Cardpanel, "0");
Example two: Displays a component using the Show method of the CardLayout class.
Form default border layout, one panel with card layout, add 4 buttons to the panel, add to center location, another panel to add 4 buttons, 4 buttons to toggle the display of the component buttons for the panel in the center location.
Cardlayout1.java import java.awt.*;
Import javax.swing.*;
Import java.awt.event.*;//implement listener interface public class CARDLAYOUT1 extends jframeimplements actionlistener{when introducing event Pack//Definition Classes
JButton b0,b1,b2,b3;
Panel cardpanel=new Panel ();
Panel Controlpapanel=newpanel ();
Defines the card layout object CardLayout card=newcardlayout ();
Defines an array of characters for the card named stringcardname[]={"0", "1", "2", "3"};
Defines the constructor public cardlayout1 () {super ("card layout manager");
SetSize (400,200);
Setdefaultcloseoperation (Jframe.exit_on_close);
Setlocationrelativeto (NULL);
SetVisible (TRUE);
Set the Cardpanel Panel object for the card layout cardpanel.setlayout (cards); loops, adding 4 buttons to the Cardpanel Panel object//Because the Cardpanel panel object is a card layout, the component for the first addition is displayed for (int i = 0; i < 4; i++) {//Panel added to each button corresponds to set aA card name Cardpanel.add (Cardname[i],newjbutton ("button" +i));
//Instantiate button object B0=newjbutton ("0");
B1=newjbutton ("1");
B2=newjbutton ("2");
B3=newjbutton ("3");
Registers the listener B0.addactionlistener for the button object (this);
B1.addactionlistener (this);
B2.addactionlistener (this);
B3.addactionlistener (this);
Controlpapanel.add (B0);
Controlpapanel.add (B1);
Controlpapanel.add (B2);
Controlpapanel.add (B3);
Defines the container object as the current form container object Containercontainer=getcontentpane ();
Place the Cardpanel panel in the middle of the window boundary layout, and the window defaults to the Border layout container.add (Cardpanel,borderlayout.center); Place the Controlpapanel panel to the south of the window's border layout, contAiner.add (Controlpapanel,borderlayout.south);
}//Implementation of the button's listener trigger when processing public voidactionperformed (ActionEvent e) {//The statement executed when the user clicks the B0 button
The IF (E.getsource () ==b0) {//) displays the component in the container through the card name in the show () method.
Card.show (Cardpanel,cardname[0]);
} if (E.getsource () ==b1) {card.show (cardpanel,cardname[1]);
} if (E.getsource () ==b2) {card.show (cardpanel,cardname[2]); } if (E.getsource () ==b3) {card.show (Cardpanel,card
NAME[3]);
} public static Voidmain (string[] args) {cardlayout1kapian=new cardlayout1 (); }
}
Program execution Results: