Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSI 3125, Preliminaries, page 1 Layout Managers. CSI 3125, Preliminaries, page 2 Layout Managers The LayoutManagers are used to arrange components in.

Similar presentations


Presentation on theme: "CSI 3125, Preliminaries, page 1 Layout Managers. CSI 3125, Preliminaries, page 2 Layout Managers The LayoutManagers are used to arrange components in."— Presentation transcript:

1 CSI 3125, Preliminaries, page 1 Layout Managers

2 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

3 CSI 3125, Preliminaries, page 3 Layout Managers AWT has different Layout Managers classes FlowLayout GridLayout BorderLayout

4 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.

5 CSI 3125, Preliminaries, page 5 Flow Layout Prog import java.applet.Applet; import java.awt.*; /* */ 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); }}

6 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)

7 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

8 CSI 3125, Preliminaries, page 8 BorderLayout Prog import java.applet.Applet; import java.awt.*; /* */ 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

9 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.

10 CSI 3125, Preliminaries, page 10 GridLayout import java.applet.Applet; import java.awt.*; /* */ 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); }}

11 CSI 3125, Preliminaries, page 11 Layout Managers

12 CSI 3125, Preliminaries, page 12 Layout Managers

13 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.

14 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.

15 CSI 3125, Preliminaries, page 15 CardLayout prog import java.applet.Applet; import java.awt.*; /* */ 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); }}


Download ppt "CSI 3125, Preliminaries, page 1 Layout Managers. CSI 3125, Preliminaries, page 2 Layout Managers The LayoutManagers are used to arrange components in."

Similar presentations


Ads by Google