160 likes | 173 Views
Learn about different types of layout managers and how to use them to arrange components in Java. This includes FlowLayout, BorderLayout, and GridLayout.
E N D
LAB SESSION 10 *LAYOUT MANAGER *LISTENERS
Laying the components manually by using a null layout is tedious. • Each container object has a layout manager associated with it. • The layout manager for the object is set using the method set Layout() method • If no set Layout() method is called then default layout is set
Syntax void setLayout(LayoutManager x); There are 4 types of Layouts • FlowLayout • BorderLayout • GridLayout • CardLayout
FlowLayout • This is the default Layout managers set to container object if its Layout is not set. • It has a simple Layout style which is similar to how words flow in a text editor. • Components are laid from the upper-left corner,left to right and top to bottom
Constructors for FLOWLAYOUT • FlowLayout() • FlowLayout(int how) • FlowLayout(int how,int horz,int vert) The first form creates the default layout which centers the components and leaves 5 pixels of space between components
The second form lets u specify how each line is aligned. Valid values of how are FlowLayout.LEFT FlowLayout.CENTER FlowLayout.RIGHT
BORDERLAYOUT • It has four narrow,fixed width components at the edges and one large area in the center. • The four sides are reffered to as north,west,south,east. • The middle area is called as center. The constructors are BorderLayout() BorderLayout(int horz,int vert)
The constructors are BorderLayout() BorderLayout(int horz,int vert) • The first one is default borderLayout. • The second form is used to specify the horizontal and vertical space left between components in horz and vertical.
BorderLayout specifies some constants • BorderLayout.CENTER • BorderLayout.EAST • BorderLayout.NORTH • BorderLayout.SOUTH • BorderLayout.WEST
We can add components to BorderLayout By using the method add() Void add(Component comp,Object region); Component specifies component to be added Region specifies the region the component is to be added LEFT,RIGHT,CENTER,EAST,or WEST
GridLayout lays out components in a two dimensional grid.When you instantiate a gridlayout,you define the number of rows and columns. • GridLayout() • GridLayout(int numrows,int Columns) • GridLayout(int numrows,int Columns,int horz,int vert)
The first form creates a single column grid layout. • The second form creates a grid Layout with the specified number of columns and rows • The third form is used to specify the horizontal and vertical space left between components in horz and vertical
t Grid layout Flow Layout
center west north east south Border layout
Eg of grid Layout public class demo extends JApplet { Static final int n=4; Public void init() { setLayout(new GridLayout(n,n); For(i=0;i<n; i++){ For(j=0;j<n; j++){ Int k=i*n+j; If (k>0) Add(new Button(“ “+k)); } } } }
Eg for border layout public class demo extends JApplet { Static final int n=4; Public void init() { setLayout(new BorderLayoutt()); Add(new Button (“north”, BorderLayout.NORTH)); Add(new Button (“south”, BorderLayout.SOUTH)); Add(new Button (“west”, BorderLayout.WEST)); Add(new Button (“east”, BorderLayout.EAST)); String msg= “adhagfhagfhg hfdgahfgfgahgf adjfhsafj”; Add(new TextArea (msg, BorderLayout.CENTER)); } } } }