Card Layout Manager in Java import java.awt.*; import - TopicsExpress



          

Card Layout Manager in Java import java.awt.*; import java.applet.Applet; import java.awt.event.*; /* This applet demonstrates using the CardLayout manager. Pressing one of three buttons will cause a different card to be displayed. */ public class Card1 extends Applet implements ActionListener { Panel cardPanel; // the container that will hold the various cards Panel firstP, secondP, thirdP; // each of these panels will constitute the cards Panel buttonP; // panel to hold three buttons Button first, second, third; // the three buttons CardLayout ourLayout; // the card layout object public void init() { //create cardPanel which is the panel that will contain the three cards cardPanel = new Panel(); //create the CardLayout object ourLayout = new CardLayout(); //set card Panels layout to be our Card Layout cardPanel.setLayout (ourLayout); //create three dummy panels (the cards) to show firstP = new Panel(); firstP.setBackground(Color.blue); secondP = new Panel(); secondP.setBackground(Color.yellow); thirdP = new Panel(); thirdP.setBackground(Color.green); //create three buttons and add ActionListener first = new Button(First); first.addActionListener(this); second = new Button(Second); second.addActionListener(this); third = new Button(Third); third.addActionListener(this); //create Panel for the buttons and add the buttons to it buttonP = new Panel(); // Panels default Layout manager is FlowLayout buttonP.add(first); buttonP.add(second); buttonP.add(third); //setLayout for applet to be BorderLayout this.setLayout(new BorderLayout()); //button Panel goes South, card panels go Center this.add(buttonP, BorderLayout.SOUTH); this.add(cardPanel, BorderLayout.CENTER); // add the three card panels to the card panel container // method takes 1.) an Object (the card) 2.) an identifying String // first one added is the visible one when applet appears cardPanel.add(firstP, First); //blue cardPanel.add(secondP, Second); //yellow cardPanel.add(thirdP, Third); //green } //------------------------------------ // respond to Button clicks by showing the so named Panel // note use of the CardLayout method show(Container, identifying string) public void actionPerformed(ActionEvent e) { if (e.getSource() == first) ourLayout.show(cardPanel, First); if (e.getSource() == second) ourLayout.show(cardPanel, Second); if (e.getSource() == third) ourLayout.show(cardPanel, Third); } } // end class
Posted on: Mon, 24 Nov 2014 11:53:40 +0000

Trending Topics



Recently Viewed Topics




© 2015