0% found this document useful (0 votes)
49 views15 pages

Java Card Layout Manager Overview

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views15 pages

Java Card Layout Manager Overview

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

BorderLayout (LayoutManagers)

Java LayoutManagers

The LayoutManagers are used to arrange components in a


particular manner. The Java LayoutManagers facilitates us to
control the positioning and size of the components in GUI forms.
LayoutManager is an interface that is implemented by all the
classes of layout managers. There are the following classes that
represent the layout managers:

1. [Link]
2. [Link]
3. [Link]
4. [Link]
5. [Link]
6. [Link]
7. [Link]
8. [Link]
9. [Link] etc.
Java BorderLayout

The BorderLayout is used to arrange the components in five


regions: north, south, east, west, and center. Each region (area)
may contain one component only. It is the default layout of a
frame or window. The BorderLayout provides five constants for
each region:

1. public static final int NORTH


2. public static final int SOUTH
3. public static final int EAST
4. public static final int WEST
5. public static final int CENTER

Constructors of BorderLayout class:

o BorderLayout(): creates a border layout but with no gaps


between the components.
o BorderLayout(int hgap, int vgap): creates a border layout
with the given horizontal and vertical gaps between the
components.

Example of BorderLayout class: Using BorderLayout()


constructor
FileName: [Link]

1. import [Link].*;
2. import [Link].*;
3.
4. public class Border
5. {
6. JFrame f;
7. Border()
8. {
9. f = new JFrame();
10.
11. // creating buttons
12. JButton b1 = new JButton("NORTH");; // the butto
n will be labeled as NORTH
13. JButton b2 = new JButton("SOUTH");; // the button
will be labeled as SOUTH
14. JButton b3 = new JButton("EAST");; // the button w
ill be labeled as EAST
15. JButton b4 = new JButton("WEST");; // the button
will be labeled as WEST
16. JButton b5 = new JButton("CENTER");; // the butto
n will be labeled as CENTER
17.
18. [Link](b1, [Link]); // b1 will be place
d in the North Direction
19. [Link](b2, [Link]); // b2 will be place
d in the South Direction
20. [Link](b3, [Link]); // b2 will be placed i
n the East Direction
21. [Link](b4, [Link]); // b2 will be placed
in the West Direction
22. [Link](b5, [Link]); // b2 will be plac
ed in the Center
23.
24. [Link](300, 300);
25. [Link](true);
26. }
27. public static void main(String[] args) {
28. new Border();
29. }
30. }

Output:
Java GridLayout

The Java GridLayout class is used to arrange the components in


a rectangular grid. One component is displayed in each
rectangle.

Constructors of GridLayout class

1. GridLayout(): creates a grid layout with one column per


component in a row.
2. GridLayout(int rows, int columns): creates a grid layout
with the given rows and columns but no gaps between the
components.
3. GridLayout(int rows, int columns, int hgap, int
vgap): creates a grid layout with the given rows and
columns along with given horizontal and vertical gaps.

Example of GridLayout class: Using GridLayout() Constructor

The GridLayout() constructor creates only one row. The following


example shows the usage of the parameterless constructor.
FileName: [Link]

1. // import statements
2. import [Link].*;
3. import [Link].*;
4.
5. public class GridLayoutExample
6. {
7. JFrame frameObj;
8.
9. // constructor
10. GridLayoutExample()
11. {
12. frameObj = new JFrame();
13.
14. // creating 9 buttons
15. JButton btn1 = new JButton("1");
16. JButton btn2 = new JButton("2");
17. JButton btn3 = new JButton("3");
18. JButton btn4 = new JButton("4");
19. JButton btn5 = new JButton("5");
20. JButton btn6 = new JButton("6");
21. JButton btn7 = new JButton("7");
22. JButton btn8 = new JButton("8");
23. JButton btn9 = new JButton("9");
24.
25. // adding buttons to the frame
26. // since, we are using the parameterless constructor, t
herfore;
27. // the number of columns is equal to the number of b
uttons we
28. // are adding to the frame. The row count remains on
e.
29. [Link](btn1); [Link](btn2); frameObj.a
dd(btn3);
30. [Link](btn4); [Link](btn5); frameObj.a
dd(btn6);
31. [Link](btn7); [Link](btn8); frameObj.a
dd(btn9);
32.
33. // setting the grid layout using the parameterless con
structor
34. [Link](new GridLayout());
35.
36.
37. [Link](300, 300);
38. [Link](true);
39. }
40.
41. // main method
42. public static void main(String argvs[])
43. {
44. new GridLayoutExample();
45. }
46. }

Output:
Java FlowLayout

The Java FlowLayout class is used to arrange the components in a line, one
a flow). It is the default layout of the applet or panel.

Fields of FlowLayout class

1. public static final int LEFT


2. public static final int RIGHT
3. public static final int CENTER
4. public static final int LEADING
5. public static final int TRAILING

Constructors of FlowLayout class

1. FlowLayout(): creates a flow layout with centered alignment and


horizontal and vertical gap.
2. FlowLayout(int align): creates a flow layout with the given alignmen
unit horizontal and vertical gap.
3. FlowLayout(int align, int hgap, int vgap): creates a flow layout
alignment and the given horizontal and vertical gap.

Example of FlowLayout class: Using FlowLayout() constructor

FileName: [Link]

1. // import statements
2. import [Link].*;
3. import [Link].*;
4.
5. public class FlowLayoutExample
6. {
7.
8. JFrame frameObj;
9.
10. // constructor
11. FlowLayoutExample()
12. {
13. // creating a frame object
14. frameObj = new JFrame();
15.
16. // creating the buttons
17. JButton b1 = new JButton("1");
18. JButton b2 = new JButton("2");
19. JButton b3 = new JButton("3");
20. JButton b4 = new JButton("4");
21. JButton b5 = new JButton("5");
22. JButton b6 = new JButton("6");
23. JButton b7 = new JButton("7");
24. JButton b8 = new JButton("8");
25. JButton b9 = new JButton("9");
26. JButton b10 = new JButton("10");
27.
28.
29. // adding the buttons to frame
30. [Link](b1); [Link](b2); [Link](b3); frame
31. [Link](b5); [Link](b6); [Link](b7); fram
32. [Link](b9); [Link](b10);
33.
34. // parameter less constructor is used
35. // therefore, alignment is center
36. // horizontal as well as the vertical gap is 5 units.
37. [Link](new FlowLayout());
38.
39. [Link](300, 300);
40. [Link](true);
41. }
42.
43. // main method
44. public static void main(String argvs[])
45. {
46. new FlowLayoutExample();
47. }
48. }

Output:
Example of FlowLayout class: Using FlowLayout(int align) constructor

FileName: [Link]

1. import [Link].*;
2. import [Link].*;
3.
4. public class MyFlowLayout{
5. JFrame f;
6. MyFlowLayout(){
7. f=new JFrame();
8.
9. JButton b1=new JButton("1");
10. JButton b2=new JButton("2");
11. JButton b3=new JButton("3");
12. JButton b4=new JButton("4");
13. JButton b5=new JButton("5");
14.
15. // adding buttons to the frame
16. [Link](b1); [Link](b2); [Link](b3); [Link](b4); [Link](b5);
17.
18. // setting flow layout of right alignment
19. [Link](new FlowLayout([Link]));
20.
21. [Link](300,300);
22. [Link](true);
23. }
24. public static void main(String[] args) {
25. new MyFlowLayout();
26. }
27. }

Output:

download this example

Example of FlowLayout class: Using FlowLayout(int align, int hgap, int vga

FileName: [Link]

1. // import statement
2. import [Link].*;
3. import [Link].*;
4.
5. public class FlowLayoutExample1
6. {
7. JFrame frameObj;
8.
9. // constructor
10. FlowLayoutExample1()
11. {
12. // creating a frame object
13. frameObj = new JFrame();
14.
15. // creating the buttons
16. JButton b1 = new JButton("1");
17. JButton b2 = new JButton("2");
18. JButton b3 = new JButton("3");
19. JButton b4 = new JButton("4");
20. JButton b5 = new JButton("5");
21. JButton b6 = new JButton("6");
22. JButton b7 = new JButton("7");
23. JButton b8 = new JButton("8");
24. JButton b9 = new JButton("9");
25. JButton b10 = new JButton("10");
26.
27.
28. // adding the buttons to frame
29. [Link](b1); [Link](b2); [Link](b3); frame
30. [Link](b5); [Link](b6); [Link](b7); fram
31. [Link](b9); [Link](b10);
32.
33. // parameterized constructor is used
34. // where alignment is left
35. // horizontal gap is 20 units and vertical gap is 25 units.
36. [Link](new FlowLayout([Link], 20, 25)
37.
38.
39. [Link](300, 300);
40. [Link](true);
41. }
42. // main method
43. public static void main(String argvs[])
44. {
45. new FlowLayoutExample1();
46. }
47. }

Output:
Next Topic BoxLayout

← PrevNext →

ADVERTISEMENT
ADVERTISEMENT

You might also like