You are on page 1of 6

Exp 4

Q.1) ) State difference between GridLayout and GridBagLayout.

Ans:

GridLayout GridBagLayout

A GridLayout arranges the components in a A GridBagLayout extends the capabilities of


rectangular grid. the GridLayout.
It arranges component in the cells and each GridBagLayout places component in each
cell has the same size. individual cell in a grid and also allows the
component to span to multiple columns or
rows.
Components are placed in columns and In order to use GridBagLayout, we need to
rows. create a GridBagConstraints object and fill
appropriate properties.
GridBagConstraints gbc = new
GridLayout(int rows, int columns) takes two
GridBagConstraints(); this method is used to
parameters that are a column and a row.
apply GridBagLayout.

2) Explain constructor of GridBagLayout.


Ans: Each GridBagLayout object manages a rectangular grid of cells, dynamic with each
component occupying one or more cells, called its display area. GridBagLayout components are
associated with the instance of GridBagConstraints. These constraints are used to define the
component’s display area and their positions.
GridBagLayout(): It is used to creates a grid bag layout manager.

Program:
Program to create a two level card deck that allows the user to select an
operating system.

import java.awt.*;
import java.applet.*;
import java.awt.event.*;

public class cardlayoutcards extends Applet implements ActionListener


{
Checkbox ch1,ch2,ch3,ch4;
Panel pl1,pl2,pl3;
Button bt1,bt2;
CardLayout cl;

public void init()


{
bt1=new Button("Open source OS");
bt2=new Button("Closed source OS");
add(bt1);
add(bt2);
cl=new CardLayout();
pl1=new Panel();
pl1.setLayout(cl);
ch1=new Checkbox("Linus");
ch2=new Checkbox("Unix");
ch3=new Checkbox("Windows");
ch4=new Checkbox("Mac OS");
pl2=new Panel();
pl2.add(ch1);
pl2.add(ch2);
pl3=new Panel();
pl3.add(ch3);
pl3.add(ch4);
pl1.add(pl2,"Open source OS");
pl1.add(pl3,"Closed source OS");
add(pl1);
bt1.addActionListener(this);
bt2.addActionListener(this);20

}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==bt1)
{
cl.show(pl1,"Open source OS");

}
else
{
cl.show(pl2,"Closed source OS");
}
}
}
/*<applet code="cardlayoutcards"width="500"height="500">
</applet>*/

Output:

You might also like