110 likes | 213 Views
Layout Manager. Layouts. Java has introduced a unique way of creating UI and displaying them on screen. Java has an interface called the LayoutManager which belongs to the java.awt package. A Layout manager determines how components will be arranged when they are added to a container(screen).
E N D
Layouts • Java has introduced a unique way of creating UI and displaying them on screen. • Java has an interface called the LayoutManager which belongs to the java.awt package. • A Layout manager determines how components will be arranged when they are added to a container(screen).
FlowLayout, BorderLayout,GridLayout and CardLayout are all classses which implement the LayoutManager interface. • To add a specific layout for a component an object of the above classes is created. • After creating a layout manager, the layout manager is made part of the container by using the setLayout() method of the Container class. • The layout manager must be established before any components are added to the container. • If no layout manager is specified, the container uses the Flow Layout manager by default.
import java.awt.*; import java.applet.*; public class Starter extends Applet { FlowLayout flow = new FlowLayout(); public void init(){ setLayout(flow); } } Now components can be added to the container; in this case it is applet.
Flow Layout • It lays out components in a manner similar to the way are laid out on a page, from left to right until there is no room, then onto the next row. • By default the components on each row centered by using FlowLayout(). • FlowLayout.RIGHT • FlowLayout.LEFT • FlowLayout.CENTER
FlowLayout(int algn,int hrzGapPixel,int vrtGapPixel) • Example
Grid Layout • Arranges components in grid of rows and columns. • Components are added first to the top of row of the grid, beginning with leftmost grid cell and continuing to the right. • GridLayout(int rows, int columns); • GridLayout(int rows,int col, int hrz_gap, int vrt_gap); • Example
Border Layout • Divide a container into 5 location:north,south,east,west,center. • Components take as much space as they need, the center gets whatever space is left over. • BorderLayout() without gap between any of the component. • BorderLayout(int,int) specifies the gap. • add(String <loc>,component); • Example
Card Layout • The card layout manager can be mixed with the other managers by nesting one container inside another. • It is a group of containers or components that are displayed one at a time. • Each container in the group is called a card.
Card Layout is created by – CardLayout <obj> = new CardLayout(); setLayout(<obj>); add(String <name of card>, component); <obj>.show(container,cardName); Note: when using card layout the previously displyed card is hidden. Only one card layout can be viewed at a time
Example • Example