Core Java - [AWT CardLayout Manager]

♠ Posted by Unknown in at 22:00

CardLayout

The CardLayout manager arranges components in a queue of cards. You can only see one card at a time. To construct a CardLayout manager, simply use the constructor CardLayout().
Cards are usually placed in a container such as a panel. Components are placed into the card queue in the order in which they are added. To add a component in the CardLayout container, use the following method.
                 
                  void add(Component com, String name)

This add the specified component to this container at the specified index denoted by the specified string. The String argument of the method, name, gives an explicit identity to the component in the queue.

To make a component visible in the container with CardLayout, you can use the following instance methods in the CardLayout object:

public void first(container)
This method views the first card in the container.
public void last(container)
This method views the last card in the container.
public void next(container)
This method views the next card in the container.
public void previous(container)
This method views the previous card in the container.
public void show(container, String name)
This method views the component with the specified name in the container. You can  use this method to directly display the component.

Example:


import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class CardLayoutExample extends Applet implements ActionListener 
{
    TextField tfFirstValue, tfSecondValue, tfAnswer;
    Label lblFirstValue, lblSecondValue, lblAnswer;
    Button btnSum,btnPrevious,btnNext,btnSub;
    Panel p1;
    Panel p2;
    Panel CardPanel = new Panel();
    CardLayout queue = new CardLayout();
    int a, b, c, d;
    
    public void init()
    {
        lblFirstValue = new Label("Enter First value : ");
        lblSecondValue = new Label("Enter Second Value : ");
        lblAnswer = new Label("The Answer : ");

        tfFirstValue = new TextField();
        tfSecondValue = new TextField();
        tfAnswer = new TextField();

        btnSum = new Button("Sum");
        btnSub = new Button("Subtract");
        btnPrevious = new Button("Previous");
        btnNext = new Button("Next");

        CardPanel.setLayout(queue);
Image: Java CardLayout Example        p1 = new Panel(new GridLayout(3,2));
        p2 = new Panel(new FlowLayout());
        CardPanel.add(p1,"Previous");
        CardPanel.add(p2,"Next");

        p1.add(lblFirstValue);
        p1.add(tfFirstValue);
        p1.add(lblSecondValue);
        p1.add(tfSecondValue);
        p1.add(btnNext);

Image: Java CardLayout Example

        p2.add(lblAnswer);
        p2.add(tfAnswer);
        p2.add(btnPrevious);
        p2.add(btnSum);
        p2.add(btnSub);
        setLayout(new FlowLayout());
        add(CardPanel);
        btnSum.addActionListener(this);
        btnSub.addActionListener(this);
        btnPrevious.addActionListener(this);
        btnNext.addActionListener(this);
    }

    public void actionPerformed(ActionEvent ae)
    {
        String btnName = ae.getActionCommand(); 
        if(btnName =="Next")
        {
                queue.next(CardPanel);
                a = Integer.parseInt(tfFirstValue.getText());
                b = Integer.parseInt(tfSecondValue.getText());
                c = a + b;
                d = a - b;
        }
        else if(btnName == "Sum")
        {

                tfAnswer.setText(Integer.toString(c));
        }
        else if(btnName == "Subtract")
        {
                tfAnswer.setText(Integer.toString(d));
        }
        else
        {
                queue.previous(CardPanel);
                tfFirstValue.setText("");
                tfSecondValue.setText("");
        }
    }
}

1 comments:

Post a Comment