1 / 16

LAB SESSION 10

Learn about different types of layout managers and how to use them to arrange components in Java. This includes FlowLayout, BorderLayout, and GridLayout.

toniaj
Download Presentation

LAB SESSION 10

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. LAB SESSION 10 *LAYOUT MANAGER *LISTENERS

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

  3. Syntax void setLayout(LayoutManager x); There are 4 types of Layouts • FlowLayout • BorderLayout • GridLayout • CardLayout

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

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

  6. The second form lets u specify how each line is aligned. Valid values of how are FlowLayout.LEFT FlowLayout.CENTER FlowLayout.RIGHT

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

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

  9. BorderLayout specifies some constants • BorderLayout.CENTER • BorderLayout.EAST • BorderLayout.NORTH • BorderLayout.SOUTH • BorderLayout.WEST

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

  11. 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)

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

  13. t Grid layout Flow Layout

  14. center west north east south Border layout

  15. 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)); } } } }

  16. 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)); } } } }

More Related