Perdorimi i CardLayout

 •        CardLayout manager

–    Gjeneron nje stive konteinerash ose komponentesh njeri mbi tjetrin

–    Cdo komponent ne grup referohet si leter (card)

–    Shume komponente ndajne te njejten hapesire  

•        Per te krijuar nje card layout,

Perdoret nje nga dy konstruktoret me poshte:

–    CardLayout()

•     Krijon nje card layout pa hapesira vertikale ose horizontale

–    CardLayout(int hgap, int vgap)

•     Krijon nje card layout me hapesira vertikale ose horizontale te specifikuara

–    Per te shtuar nje komponent ne content pane, perdoret: add(aString, aContainer);

 Metodat e CardLayout:

first (Container parent)

Shfaq letren e pare te konteinerit.

next (Container parent)

Shfaq letren pasardhese te konteinerit. Nese ka arritur tek letra e fundit atehere do te shfaqet perseri e para.

previous (Container parent)

Shfaq letren paraardhese te konteinerit. Nese ka arritur tek letra e pare atehere do te shfaqet letra e fundit.

last (Container parent)

Shfaq letren e fundit te konteinerit.

show (Container parent, String name)

Shfaq komponentin qe eshte shtuar ne layout me stringun e dhene duke perdorur metoden addLayoutComponent.

Shembull:


     import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

public class JDemoCardLayout extends JFrame

     implements ActionListener

{

   private CardLayout cards = new CardLayout();

   private JButton b1 = new JButton("Ace of Hearts");

   private JButton b2 = new JButton("Three of Spades");

   private JButton b3 = new JButton("Queen of Clubs");

   private Container con = getContentPane();

   public JDemoCardLayout()

   {

      con.setLayout(cards);

      con.add("ace", b1);

      b1.addActionListener(this);

      con.add("three", b2);

      b2.addActionListener(this);

      con.add("queen", b3);

      b3.addActionListener(this);

      setSize(200, 100);

   }

   public void actionPerformed(ActionEvent e)

   {

      cards.next(getContentPane());

   }

   public static void main(String[] args)

   {

      JDemoCardLayout frame = new JDemoCardLayout();

      frame.setVisible(true);

   }

}