Practical No. 4: Use of CardLayout to write a program to create a two-level card deck that allows the user to select an operating system.

 I.                Practical Significance:

The CardLayout class manages the components in such a manner that only one component is visible at a time. It treats each component as a card hence known as CardLayout.

 

II.             Relevant Program Outcomes (POs)

 Basic knowledge: Apply knowledge of basic mathematics, sciences and basic engineering to solve the computer group related problems.


 Discipline knowledge: Apply Computer Programming knowledge to solve the computer group related problems.

                       Experiments and practice: Plan to perform experiments and practices to use the results to                     solve the computer group related problems.

 Engineering tools: Apply relevant Computer programming / technologies and  tools with an understanding of the limitations.

                       Individual and Team work: Function effectively as a leader and team member in                                 diverse/multidisciplinary teams.

                            Communication: Communicate effectively in oral and written form.


III.           Competency and Practical skills

To develop standalone applications

The practical is expected to develop the following skills:

1.     Able to apply different layouts to Applet, Frame and Panel

2.     Able to demonstrate the use of different types of Layout Manager

 

IV.           Relevant Course Outcome(s)

Develop programs using GUI Framework (AWT and Swing).

 

V.              Practical Outcome (PrOs)

Write a program to demonstrate the use of Border layout showing four buttons at four sides of an applet with captions “left”, “right”, “top” and “bottom”

 

VI.           Relevant Affective domain related Outcome(s)

1.     Follow precautionary measures.

2.     Follow naming conventions.

3.     Follow ethical practices.


 VII.   Minimum Theoretical Background Constructors of CardLayout class

1.     CardLayout(): creates a card layout with zero horizontal and vertical gap.

2.     CardLayout(int hgap, int vgap): creates a card layout with the given horizontal and vertical gap.

 

Commonly used methods of CardLayout class

1.     public void next (Container parent): is used to flip to the next card of the given container.

2.     public void previous (Container parent): is used to flip to the previous card of  the given container.

3.     public void first (Container parent): is used to flip to the first card of the given container.

4.     public void last (Container parent): is used to flip to the last card of the given container.

5.     public void show (Container parent, String name): is used to flip to  the  specified card with the given name.

 

X.              Program Code: Teacher must assign a separate program statement to group of  3-4 students.

Execute the following Program and write the output.


import java.awt.*; import java.awt.event.*; import javax.swing.*;

 

public class CardLayoutExample extends JFrame implements ActionListener

{

CardLayout card; JButton b1, b2, b3; Container c; CardLayoutExample()

{

c=getContentPane(); card=new CardLayout(40,30);

//create CardLayout object with 40 hor space and 30 ver space c.setLayout(card);

b1=new JButton("Apple"); b2=new JButton("Boy"); b3=new JButton("Cat"); b1.addActionListener(this); b2.addActionListener(this); b3.addActionListener(this);

c.add("a",b1);c.add("b",b2);c.add("c",b3);

}


public void actionPerformed(ActionEvent e)

{

card.next(c);

}

public static void main(String[] args)

{

CardLayoutExample cl=new CardLayoutExample(); cl.setSize(400,400);

cl.setVisible(true); cl.setDefaultCloseOperation(EXIT_ON_CLOSE);

}

}

 

Output:



XII.         Practical Related Questions

Note: Below given are few sample questions for reference. Teacher must  design  more such questions so as to ensure the achievement of identified CO.

1.     State difference between GridLayout and GridBagLayout.

2.     Explain constructor of GridbagLayout.


 

I.                Exercise

1.     Write Java program to display following output.



Figure 5

 

import java.awt.Button;

import java.awt.GridBagConstraints;

import java.awt.GridBagLayout;

import javax.swing.*;


public class PR4a extends JFrame {

    public PR4a() {

        GridBagLayout grid = new GridBagLayout();

        GridBagConstraints gbc = new GridBagConstraints();

        setLayout(grid);

        setTitle("GridBag Layout Example");

        GridBagLayout layout = new GridBagLayout();

        this.setLayout(layout);

        gbc.fill = GridBagConstraints.HORIZONTAL;

        gbc.gridx = 0;

        gbc.gridy = 0;

        this.add(new Button("Button One"), gbc);

        gbc.gridx = 1;

        gbc.gridy = 0;

        this.add(new Button("Button two"), gbc);

        gbc.fill = GridBagConstraints.HORIZONTAL;

        gbc.ipady = 20;

        gbc.gridx = 0;

        gbc.gridy = 1;

        this.add(new Button("Button Three"), gbc);

        gbc.gridx = 1;

        gbc.gridy = 1;

        this.add(new Button("Button Four"), gbc);

        gbc.gridx = 0;

        gbc.gridy = 2;

        gbc.fill = GridBagConstraints.HORIZONTAL;

        gbc.gridwidth = 2;

        this.add(new Button("Button Five"), gbc);

        setSize(300, 300);

        setPreferredSize(getSize());

        setVisible(true);

        setDefaultCloseOperation(EXIT_ON_CLOSE);

    }

   public static void main(String[] args) {

        PR4a a = new PR4a();

    }

}


2.     Write Java Program to display following output.


Figure 6


import java.awt.Button;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.*;
public class PR4b extends JFrame {
    public PR4b() {
        GridBagLayout grid = new GridBagLayout();
        GridBagConstraints gbc = new GridBagConstraints();
        setLayout(grid);
        setTitle("GridBag Layout Example");
        GridBagLayout layout = new GridBagLayout();
        JLabel L1 = new JLabel("Name");
        JLabel L2 = new JLabel("Comments");
        JTextField T1 = new JTextField();
        JTextArea T2 = new JTextArea(10, 10);
        JScrollPane SP = new JScrollPane(T2);
        SP.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        this.setLayout(layout);
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridx = 0;
        gbc.gridy = 0;
        this.add(L1, gbc);
        gbc.gridx = 1;
        gbc.gridy = 0;
        this.add(T1, gbc);
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridx = 0;
        gbc.gridy = 1;
        this.add(L2, gbc);
        gbc.gridx = 1;
        gbc.gridy = 1;
        this.add(T2, gbc);
        this.getContentPane().add(SP);
        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridwidth = 1;
        this.add(new Button("Submit"), gbc);
        setSize(300, 300);
        setPreferredSize(getSize());
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
        PR4b a = new PR4b();
    }
}

Comments