CSI 3125, Preliminaries, page 1 Layout Managers. CSI 3125, Preliminaries, page 2 Layout Managers The...
-
Upload
valentine-lawrence -
Category
Documents
-
view
229 -
download
0
description
Transcript of CSI 3125, Preliminaries, page 1 Layout Managers. CSI 3125, Preliminaries, page 2 Layout Managers The...
CSI 3125, Preliminaries, page 1
Layout Managers
CSI 3125, Preliminaries, page 2
Layout Managers• The LayoutManagers are used to arrange
components in a particular manner. • Layout Managers used to define the location
and size of Graphical User Interface components.
• setLayout() – to set the layout
CSI 3125, Preliminaries, page 3
Layout Managers• AWT has different Layout Managers classes• FlowLayout• GridLayout• BorderLayout
CSI 3125, Preliminaries, page 4
Flow Layout• The FlowLayout is used to arrange the components in a line, one after another (in a
flow). • It is the default layout of Applet.• Here are the constructors for FlowLayout:• FlowLayout( )• FlowLayout(int how)• FlowLayout(int how, int horz, int vert)• The first form creates the default layout, with centered alignment and a default 5
unit horizontal and vertical gap. • The second form lets you specify how each line is aligned. • Valid values for how are as follows:• FlowLayout.LEFT or 0• FlowLayout.CENTER or 1• FlowLayout.RIGHT or 2• The third form allows you to specify the horizontal and vertical space left between
components in horz and vert, respectively.
CSI 3125, Preliminaries, page 5
Flow Layout Prog• import java.applet.Applet;• import java.awt.*;• /*<applet code="Button1" width=500 height=500></applet>*/• public class Button1 extends Applet{• public void init(){• setLayout(new FlowLayout(FlowLayout.RIGHT));• //setLayout(new FlowLayout(FlowLayout.RIGHT,100,20));• Button b1=new Button("button1");• Button b2=new Button("button2");• Button b3=new Button("button3");• add(b1);• add(b2);• add(b3);• • }}
CSI 3125, Preliminaries, page 6
BorderLayout• The BorderLayout class implements a common
layout style for top-level windows. • It has four narrow, fixed-width components at the
edges and one large area in the center.• The four sides are referred to as north, south, east,
and west. • The middle area is called the center. • constructors defined by BorderLayout:• BorderLayout( )• BorderLayout(int horz, int vert)
CSI 3125, Preliminaries, page 7
BorderLayout• BorderLayout defines the following constants that specify
the regions:• BorderLayout.CENTER • BorderLayout.SOUTH• BorderLayout.EAST • BorderLayout.WEST• BorderLayout.NORTH• When adding components, will use these constants with
the following form of add( )• void add(Component compObj, Object region);• It give more preference to CENTER when not specified
CSI 3125, Preliminaries, page 8
BorderLayout Prog• import java.applet.Applet;• import java.awt.*;• /*<applet code="border" width=500 height=500></applet>*/• public class border extends Applet{• public void init(){• setLayout(new BorderLayout());• Button b1=new Button("button1");• Button b2=new Button("button2");• Button b3=new Button("button3");• Button b4=new Button("button4");• Button b5=new Button("button5");• add(b1,BorderLayout.CENTER);• add(b2,BorderLayout.SOUTH);• add(b3,BorderLayout.EAST);• add(b4,BorderLayout.WEST);• add(b5,BorderLayout.NORTH);• }• }• //Centre higher priority
CSI 3125, Preliminaries, page 9
GridLayout• The GridLayout is used to arrange the components in
rectangular grid. • define the number of rows and columns• Constructors • GridLayout( )• GridLayout(int numRows, int numColumns )• GridLayout(int numRows, int numColumns, 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 rows and columns. • The third form allows you to specify the horizontal and vertical
space left between components in horz and vert, respectively.
CSI 3125, Preliminaries, page 10
GridLayout• import java.applet.Applet;• import java.awt.*;• /*<applet code="grid" width=300 height=300>• </applet>*/• public class grid extends Applet{• String s;• public void init(){• setLayout(new GridLayout(2,2));• //setLayout(new GridLayout());• Button b1=new Button("button1");• Button b2=new Button("button2");• Button b3=new Button("button3");• Button b4=new Button("button4");• Button b5=new Button("button5");• add(b1);• add(b2);• add(b3);• add(b4);• add(b5);• }}
CSI 3125, Preliminaries, page 11
Layout Managers
CSI 3125, Preliminaries, page 12
Layout Managers
CSI 3125, Preliminaries, page 13
CardLayout• 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 that is why it is known as CardLayout.
CSI 3125, Preliminaries, page 14
CardLayout• constructors:• CardLayout( )• CardLayout(int horz, int vert)• The first form creates a default card layout. • The second form allows you to specify the
horizontal and vertical space left between components in horz and vert, respectively.
CSI 3125, Preliminaries, page 15
CardLayout prog• import java.applet.Applet;• import java.awt.*;• /*<applet code="card" width=500 height=500>• </applet>*/• public class card extends Applet{• String s;• public void init(){• CardLayout ca = new CardLayout();• Panel p = new Panel();• p.setLayout(ca); • Button b1=new Button("button1");• Button b2=new Button("button2");• Button b3=new Button("button3");• Button b4=new Button("button4");• Button b5=new Button("button5");• add(b1);• add(b2);• add(b3);• add(b4);• add(b5);• }}