The card layout of Java learning notes Cardlayout__java

Source: Internet
Author: User

CardLayout (constructed) method:

Method Description
Public CardLayout () Create a new card layout with a spacing size of 0
Public cardlayout (int hgap,int vgap) Creates a new card layout with the specified horizontal and vertical spacing. The horizontal spacing is placed on the left and right edges. Vertical spacing to upper and lower edges
public void A (Container parent) Flip to the first card of the container
public void last (contain parent) Flip to the last card in the container
public void next (contain parent) Flip to the next card in the specified container

public void Previous (contain

Parent

Flip to the previous card in the specified container
public void Show (contain parent,string name) Flip to a component with the specified name that is added to this layout using addlayoutcomponent

Example:

Package com.lc.awt;
Import Java.awt.BorderLayout;
Import Java.awt.CardLayout;
Import Java.awt.Color;
Import Java.awt.Insets;
Import java.awt.event.ActionEvent;

Import Java.awt.event.ActionListener;
Import Javax.swing.JButton;
Import Javax.swing.JFrame;
Import Javax.swing.JLabel;

Import Javax.swing.JPanel;
	public class Cardlayoutdemo extends JFrame {private JPanel pane = null;
	Private JPanel p = null;
	Private CardLayout card = null;
	Private JButton Button_1=null;
	Private JButton Button_2=null;
	Private JButton B_1=null,b_2=null,b_3=null; 
	Private JPanel P_1=null,p_2=null,p_3=null;
		Public Cardlayoutdemo () {card = new cardlayout (5,5);
		pane = new JPanel (card);
		p = new JPanel ();
		button_1 = new JButton ("< previous step");
		button_2 = new JButton ("Next >");
		b_1 = new JButton ("1");
		b_2 = new JButton ("2");
		B_3 = new JButton ("3");
		B_1.setmargin (New Insets (2,2,2,2));
		B_2.setmargin (New Insets (2,2,2,2));
		B_3.setmargin (New Insets (2,2,2,2));
		P.add (button_1); P.add (b_1);
		P.add (b_2);
		P.add (B_3);
		P.add (button_2);
		P_1 = new JPanel ();
		p_2 = new JPanel ();
		P_3 = new JPanel ();
		P_1.setbackground (color.red);
		P_2.setbackground (Color.Blue);
		P_3.setbackground (Color.green);
		P_1.add (New JLabel ("Jpanel_1"));
		P_2.add (New JLabel ("jpanel_2"));
		P_3.add (New JLabel ("Jpanel_3"));
		Pane.add (p_1, "P1");
		Pane.add (p_2, "P2");
	
		Pane.add (P_3, "P3"); Button_1.addactionlistener (new ActionListener () {public void actionperformed (ActionEvent arg0) {card.previous (pan
			e);
		}
		});
			Button_2.addactionlistener (new ActionListener () {public void actionperformed (ActionEvent e) {card.next (pane);
		}
		});
			B_1.addactionlistener (new ActionListener () {public void actionperformed (ActionEvent e) {card.show (pane, "P1");
		}
		});
			B_2.addactionlistener (new ActionListener () {public void actionperformed (ActionEvent e) {card.show (pane, "P2");
		}
		}); B_3.addactionlistener (new ActionListener () {public void actionperformed(ActionEvent e)
			{card.show (pane, "P3");
		}
		});
		This.getcontentpane (). Add (pane);
		This.getcontentpane (). Add (P,borderlayout.south);
		This.setsize (300, 200);
	This.setvisible (TRUE);
	public static void Main (string[] args) {new Cardlayoutdemo ();
 }

}

Run 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.