Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
16 views

Java LayoutManagers

Uploaded by

Tejas Shukla
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)
16 views

Java LayoutManagers

Uploaded by

Tejas Shukla
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
You are on page 1/ 55

Dr Bharti Sharma

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. java.awt.BorderLayout
2. java.awt.FlowLayout
3. java.awt.GridLayout
4. java.awt.CardLayout
5. java.awt.GridBagLayout
6. javax.swing.BoxLayout
7. javax.swing.GroupLayout
8. javax.swing.ScrollPaneLayout
9. javax.swing.SpringLayout etc.

Java BorderLayout
Dr Bharti Sharma

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
Dr Bharti Sharma

FileName: Border.java
1. import java.awt.*;
2. import javax.swing.*;
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 button 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 will be labeled as EAST
15. JButton b4 = new JButton("WEST"); // the button will be labeled as WEST
16. JButton b5 = new JButton("CENTER"); // the button will be labeled as CENTER
17.
18. f.add(b1, BorderLayout.NORTH); // b1 will be placed in the North Direction
19. f.add(b2, BorderLayout.SOUTH); // b2 will be placed in the South Direction
20. f.add(b3, BorderLayout.EAST); // b2 will be placed in the East Direction
Dr Bharti Sharma

21. f.add(b4, BorderLayout.WEST); // b2 will be placed in the West Direction


22. f.add(b5, BorderLayout.CENTER); // b2 will be placed in the Center
23. f.setSize(300, 300);
24. f.setVisible(true);
25. }
26. public static void main(String[] args) {
27. new Border();
28. }
29. }
Output:
Dr Bharti Sharma

Example of BorderLayout class: Using BorderLayout(int hgap, int vgap) constructor


The following example inserts horizontal and vertical gaps between buttons using the
parameterized constructor BorderLayout(int hgap, int gap)
FileName: BorderLayoutExample.java
1. // import statement
Dr Bharti Sharma

2. import java.awt.*;
3. import javax.swing.*;
4. public class BorderLayoutExample
5. {
6. JFrame jframe;
7. // constructor
8. BorderLayoutExample()
9. {
10. // creating a Frame
11. jframe = new JFrame();
12. // create buttons
13. JButton btn1 = new JButton("NORTH");
14. JButton btn2 = new JButton("SOUTH");
15. JButton btn3 = new JButton("EAST");
16. JButton btn4 = new JButton("WEST");
17. JButton btn5 = new JButton("CENTER");
18. // creating an object of the BorderLayout class using
19. // the parameterized constructor where the horizontal gap is 20
20. // and vertical gap is 15. The gap will be evident when buttons are placed
21. // in the frame
22. jframe.setLayout(new BorderLayout(20, 15));
Dr Bharti Sharma

23. jframe.add(btn1, BorderLayout.NORTH);


24. jframe.add(btn2, BorderLayout.SOUTH);
25. jframe.add(btn3, BorderLayout.EAST);
26. jframe.add(btn4, BorderLayout.WEST);
27. jframe.add(btn5, BorderLayout.CENTER);
28. jframe.setSize(300,300);
29. jframe.setVisible(true);
30. }
31. // main method
32. public static void main(String argvs[])
33. {
34. new BorderLayoutExample();
35. }
36. }
Output:
Dr Bharti Sharma

Java BorderLayout: Without Specifying Region


The add() method of the JFrame class can work even when we do not specify the region.
Dr Bharti Sharma

In such a case, only the latest component added is shown in the frame, and all the components
added previously get discarded.
The latest component covers the whole area. The following example shows the same.
FileName: BorderLayoutWithoutRegionExample.java
1. // import statements
2. import java.awt.*;
3. import javax.swing.*;
4.
5. public class BorderLayoutWithoutRegionExample
6. {
7. JFrame jframe;
8.
9. // constructor
10. BorderLayoutWithoutRegionExample()
11. {
12. jframe = new JFrame();
13.
14. JButton btn1 = new JButton("NORTH");
15. JButton btn2 = new JButton("SOUTH");
Dr Bharti Sharma

16. JButton btn3 = new JButton("EAST");


17. JButton btn4 = new JButton("WEST");
18. JButton btn5 = new JButton("CENTER");
19.
20. // horizontal gap is 7, and the vertical gap is 7
21. // Since region is not specified, the gaps are of no use
22. jframe.setLayout(new BorderLayout(7, 7));
23.
24. // each button covers the whole area
25. // however, the btn5 is the latest button
26. // that is added to the frame; therefore, btn5
27. // is shown
28. jframe.add(btn1);
29. jframe.add(btn2);
30. jframe.add(btn3);
31. jframe.add(btn4);
32. jframe.add(btn5);
33.
34. jframe.setSize(300,300);
35. jframe.setVisible(true);
36. }
Dr Bharti Sharma

37.
38. // main method
39. public static void main(String argvs[])
40. {
41. new BorderLayoutWithoutRegionExample();
42. }
43. }
Output:
Dr Bharti Sharma
Dr Bharti Sharma

Java FlowLayout
The Java FlowLayout class is used to arrange the components in a line, one after another (in 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 a default 5 unit horizontal
and vertical gap.
2. FlowLayout(int align): creates a flow layout with the given alignment and a default 5 unit
horizontal and vertical gap.
Dr Bharti Sharma

3. FlowLayout(int align, int hgap, int vgap): creates a flow layout with the given alignment
and the given horizontal and vertical gap.
Example of FlowLayout class: Using FlowLayout() constructor
FileName: FlowLayoutExample.java
1. // import statements
2. import java.awt.*;
3. import javax.swing.*;
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.
Dr Bharti Sharma

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. frameObj.add(b1); frameObj.add(b2); frameObj.add(b3); frameObj.add(b4);
31. frameObj.add(b5); frameObj.add(b6); frameObj.add(b7); frameObj.add(b8);
32. frameObj.add(b9); frameObj.add(b10);
33.
34. // parameter less constructor is used
35. // therefore, alignment is center
36. // horizontal as well as the vertical gap is 5 units.
Dr Bharti Sharma

37. frameObj.setLayout(new FlowLayout());


38.
39. frameObj.setSize(300, 300);
40. frameObj.setVisible(true);
41. }
42.
43. // main method
44. public static void main(String argvs[])
45. {
46. new FlowLayoutExample();
47. }
48. }
Output:
Dr Bharti Sharma

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


FileName: MyFlowLayout.java
1. import java.awt.*;
2. import javax.swing.*;
3.
Dr Bharti Sharma

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. f.add(b1); f.add(b2); f.add(b3); f.add(b4); f.add(b5);
17.
18. // setting flow layout of right alignment
19. f.setLayout(new FlowLayout(FlowLayout.RIGHT));
20.
21. f.setSize(300,300);
22. f.setVisible(true);
23. }
24. public static void main(String[] args) {
Dr Bharti Sharma

25. new MyFlowLayout();


26. }
27. }
Output:

download this example

Example of FlowLayout class: Using FlowLayout(int align, int hgap, int vgap) constructor
Dr Bharti Sharma

FileName: FlowLayoutExample1.java
1. // import statement
2. import java.awt.*;
3. import javax.swing.*;
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");
Dr Bharti Sharma

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. frameObj.add(b1); frameObj.add(b2); frameObj.add(b3); frameObj.add(b4);
30. frameObj.add(b5); frameObj.add(b6); frameObj.add(b7); frameObj.add(b8);
31. frameObj.add(b9); frameObj.add(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. frameObj.setLayout(new FlowLayout(FlowLayout.LEFT, 20, 25));
37.
38.
39. frameObj.setSize(300, 300);
40. frameObj.setVisible(true);
41. }
Dr Bharti Sharma

42. // main method


43. public static void main(String argvs[])
44. {
45. new FlowLayoutExample1();
46. }
47. }
Output:
Dr Bharti Sharma

Java GridLayout
The Java GridLayout class is used to arrange the components in a rectangular grid. One component
is displayed in each rectangle.
Dr Bharti Sharma

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: GridLayoutExample.java
1. // import statements
2. import java.awt.*;
3. import javax.swing.*;
4.
5. public class GridLayoutExample
6. {
7. JFrame frameObj;
8.
Dr Bharti Sharma

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, therfore;
27. // the number of columns is equal to the number of buttons we
28. // are adding to the frame. The row count remains one.
29. frameObj.add(btn1); frameObj.add(btn2); frameObj.add(btn3);
Dr Bharti Sharma

30. frameObj.add(btn4); frameObj.add(btn5); frameObj.add(btn6);


31. frameObj.add(btn7); frameObj.add(btn8); frameObj.add(btn9);
32.
33. // setting the grid layout using the parameterless constructor
34. frameObj.setLayout(new GridLayout());
35.
36.
37. frameObj.setSize(300, 300);
38. frameObj.setVisible(true);
39. }
40.
41. // main method
42. public static void main(String argvs[])
43. {
44. new GridLayoutExample();
45. }
46. }
Output:
Dr Bharti Sharma

Example of GridLayout class: Using GridLayout(int rows, int columns) Constructor


FileName: MyGridLayout.java
1. import java.awt.*;
2. import javax.swing.*;
3. public class MyGridLayout{
Dr Bharti Sharma

4. JFrame f;
5. MyGridLayout(){
6. f=new JFrame();
7. JButton b1=new JButton("1");
8. JButton b2=new JButton("2");
9. JButton b3=new JButton("3");
10. JButton b4=new JButton("4");
11. JButton b5=new JButton("5");
12. JButton b6=new JButton("6");
13. JButton b7=new JButton("7");
14. JButton b8=new JButton("8");
15. JButton b9=new JButton("9");
16. // adding buttons to the frame
17. f.add(b1); f.add(b2); f.add(b3);
18. f.add(b4); f.add(b5); f.add(b6);
19. f.add(b7); f.add(b8); f.add(b9);
20.
21. // setting grid layout of 3 rows and 3 columns
22. f.setLayout(new GridLayout(3,3));
23. f.setSize(300,300);
24. f.setVisible(true);
Dr Bharti Sharma

25. }
26. public static void main(String[] args) {
27. new MyGridLayout();
28. }
29. }
Output:

download this example


Dr Bharti Sharma

Example of GridLayout class: Using GridLayout(int rows, int columns, int hgap, int vgap)
Constructor
The following example inserts horizontal and vertical gaps between buttons using the
parameterized constructor GridLayout(int rows, int columns, int hgap, int vgap).
FileName: GridLayoutExample1.java
1. // import statements
2. import java.awt.*;
3. import javax.swing.*;
4.
5. public class GridLayoutExample1
6. {
7.
8. JFrame frameObj;
9.
10. // constructor
11. GridLayoutExample1()
12. {
13. frameObj = new JFrame();
14.
Dr Bharti Sharma

15. // creating 9 buttons


16. JButton btn1 = new JButton("1");
17. JButton btn2 = new JButton("2");
18. JButton btn3 = new JButton("3");
19. JButton btn4 = new JButton("4");
20. JButton btn5 = new JButton("5");
21. JButton btn6 = new JButton("6");
22. JButton btn7 = new JButton("7");
23. JButton btn8 = new JButton("8");
24. JButton btn9 = new JButton("9");
25.
26. // adding buttons to the frame
27. // since, we are using the parameterless constructor, therefore;
28. // the number of columns is equal to the number of buttons we
29. // are adding to the frame. The row count remains one.
30. frameObj.add(btn1); frameObj.add(btn2); frameObj.add(btn3);
31. frameObj.add(btn4); frameObj.add(btn5); frameObj.add(btn6);
32. frameObj.add(btn7); frameObj.add(btn8); frameObj.add(btn9);
33. // setting the grid layout
34. // a 3 * 3 grid is created with the horizontal gap 20
35. // and vertical gap 25
Dr Bharti Sharma

36. frameObj.setLayout(new GridLayout(3, 3, 20, 25));


37. frameObj.setSize(300, 300);
38. frameObj.setVisible(true);
39. }
40. // main method
41. public static void main(String argvs[])
42. {
43. new GridLayoutExample();
44. }
45. }
Output:
Dr Bharti Sharma

Java CardLayout
The Java 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.
Constructors of CardLayout Class
Dr Bharti Sharma

1. CardLayout(): creates a card layout with zero horizontal and vertical gap.
2. CardLayout(int hgap, int vgap): creates a card layout with the given horizontal and vertical
gap.
Commonly Used Methods of CardLayout Class
o public void next(Container parent): is used to flip to the next card of the given container.
o public void previous(Container parent): is used to flip to the previous card of the given
container.
o public void first(Container parent): is used to flip to the first card of the given container.
o public void last(Container parent): is used to flip to the last card of the given container.
o public void show(Container parent, String name): is used to flip to the specified card with
the given name.
Example of CardLayout Class: Using Default Constructor
The following program uses the next() method to move to the next card of the container.
FileName: CardLayoutExample1.java
1. // import statements
2. import java.awt.*;
3. import javax.swing.*;
Dr Bharti Sharma

4. import java.awt.event.*;
5.
6. public class CardLayoutExample1 extends JFrame implements ActionListener
7. {
8.
9. CardLayout crd;
10.
11. // button variables to hold the references of buttons
12. JButton btn1, btn2, btn3;
13. Container cPane;
14.
15. // constructor of the class
16. CardLayoutExample1()
17. {
18.
19. cPane = getContentPane();
20.
21. //default constructor used
22. // therefore, components will
23. // cover the whole area
24. crd = new CardLayout();
Dr Bharti Sharma

25.
26. cPane.setLayout(crd);
27.
28. // creating the buttons
29. btn1 = new JButton("Apple");
30. btn2 = new JButton("Boy");
31. btn3 = new JButton("Cat");
32.
33. // adding listeners to it
34. btn1.addActionListener(this);
35. btn2.addActionListener(this);
36. btn3.addActionListener(this);
37.
38. cPane.add("a", btn1); // first card is the button btn1
39. cPane.add("b", btn2); // first card is the button btn2
40. cPane.add("c", btn3); // first card is the button btn3
41.
42. }
43. public void actionPerformed(ActionEvent e)
44. {
45. // Upon clicking the button, the next card of the container is shown
Dr Bharti Sharma

46. // after the last card, again, the first card of the container is shown upon clicking
47. crd.next(cPane);
48. }
49.
50. // main method
51. public static void main(String argvs[])
52. {
53. // creating an object of the class CardLayoutExample1
54. CardLayoutExample1 crdl = new CardLayoutExample1();
55.
56. // size is 300 * 300
57. crdl.setSize(300, 300);
58. crdl.setVisible(true);
59. crdl.setDefaultCloseOperation(EXIT_ON_CLOSE);
60. }
61. }
Output:
Dr Bharti Sharma

When the button named apple is clicked, we get


Dr Bharti Sharma

When the boy button is clicked, we get


Dr Bharti Sharma

Again, we reach the first card of the container if the cat button is clicked, and the cycle continues.
Dr Bharti Sharma

Example of CardLayout Class: Using Parameterized Constructor


FileName: CardLayoutExample2.java
1. import java.awt.*;
2. import java.awt.event.*;
3.
4. import javax.swing.*;
Dr Bharti Sharma

5.
6. public class CardLayoutExample2 extends JFrame implements ActionListener{
7. CardLayout card;
8. JButton b1,b2,b3;
9. Container c;
10. CardLayoutExample2(){
11.
12. c=getContentPane();
13. card=new CardLayout(40,30);
14. //create CardLayout object with 40 hor space and 30 ver space
15. c.setLayout(card);
16.
17. b1=new JButton("Apple");
18. b2=new JButton("Boy");
19. b3=new JButton("Cat");
20. b1.addActionListener(this);
21. b2.addActionListener(this);
22. b3.addActionListener(this);
23.
24. c.add("a",b1);c.add("b",b2);c.add("c",b3);
25.
Dr Bharti Sharma

26. }
27. public void actionPerformed(ActionEvent e) {
28. card.next(c);
29. }
30.
31. public static void main(String[] args) {
32. CardLayoutExample2 cl=new CardLayoutExample2();
33. cl.setSize(400,400);
34. cl.setVisible(true);
35. cl.setDefaultCloseOperation(EXIT_ON_CLOSE);
36. }
37. }
download this example
Output:
Dr Bharti Sharma

Usage of the Methods of the CardLayout Class


The following example shows how one can use different methods of the CardLayout class.
FileName: CardLayoutExample3.java
Dr Bharti Sharma

1. // Import statements.
2. import java.awt.*;
3. import java.awt.event.ActionEvent;
4. import java.awt.event.ActionListener;
5. import javax.swing.*;
6.
7. public class CardLayoutExample3 extends JFrame
8. {
9.
10. // Initializing the value of
11. // currCard to 1 .
12. private int currCard = 1;
13.
14. // Declaring of objects
15. // of the CardLayout class.
16. private CardLayout cObjl;
17.
18. // constructor of the class
19. public CardLayoutExample3()
20. {
21.
Dr Bharti Sharma

22. // Method to set the Title of the JFrame


23. setTitle("Card Layout Methods");
24.
25. // Method to set the visibility of the JFrame
26. setSize(310, 160);
27.
28. // Creating an Object of the "Jpanel" class
29. JPanel cPanel = new JPanel();
30.
31. // Initializing of the object "cObjl"
32. // of the CardLayout class.
33. cObjl = new CardLayout();
34.
35. // setting the layout
36. cPanel.setLayout(cObjl);
37.
38. // Initializing the object
39. // "jPanel1" of the JPanel class.
40. JPanel jPanel1 = new JPanel();
41.
42. // Initializing the object
Dr Bharti Sharma

43. // "jPanel2" of the CardLayout class.


44. JPanel jPanel2 = new JPanel();
45.
46. // Initializing the object
47. // "jPanel3" of the CardLayout class.
48. JPanel jPanel3 = new JPanel();
49.
50. // Initializing the object
51. // "jPanel4" of the CardLayout class.
52. JPanel jPanel4 = new JPanel();
53.
54. // Initializing the object
55. // "jl1" of the JLabel class.
56. JLabel jLabel1 = new JLabel("C1");
57.
58. // Initializing the object
59. // "jLabel2" of the JLabel class.
60. JLabel jLabel2 = new JLabel("C2");
61.
62. // Initializing the object
63. // "jLabel3" of the JLabel class.
Dr Bharti Sharma

64. JLabel jLabel3 = new JLabel("C3");


65.
66. // Initializing the object
67. // "jLabel4" of the JLabel class.
68. JLabel jLabel4 = new JLabel("C4");
69.
70. // Adding JLabel "jLabel1" to the JPanel "jPanel1".
71. jPanel1.add(jLabel1);
72.
73. // Adding JLabel "jLabel2" to the JPanel "jPanel2".
74. jPanel2.add(jLabel2);
75.
76. // Adding JLabel "jLabel3" to the JPanel "jPanel3".
77. jPanel3.add(jLabel3);
78.
79. // Adding JLabel "jLabel4" to the JPanel "jPanel4".
80. jPanel4.add(jLabel4);
81.
82. // Add the "jPanel1" on cPanel
83. cPanel.add(jPanel1, "1");
84.
Dr Bharti Sharma

85. // Add the "jPanel2" on cPanel


86. cPanel.add(jPanel2, "2");
87.
88. // Add the "jPanel3" on cPanel
89. cPanel.add(jPanel3, "3");
90.
91. // Add the "jPanel4" on cPanel
92. cPanel.add(jPanel4, "4");
93.
94. // Creating an Object of the "JPanel" class
95. JPanel btnPanel = new JPanel();
96.
97. // Initializing the object
98. // "firstButton" of the JButton class.
99. JButton firstButton = new JButton("First");
100.
101. // Initializing the object
102. // "nextButton" of the JButton class.
103. JButton nextButton = new JButton("->");
104.
105. // Initializing the object
Dr Bharti Sharma

106. // "previousbtn" of JButton class.


107. JButton previousButton = new JButton("<-");
108.
109. // Initializing the object
110. // "lastButton" of the JButton class.
111. JButton lastButton = new JButton("Last");
112.
113. // Adding the JButton "firstbutton" on the JPanel.
114. btnPanel.add(firstButton);
115.
116. // Adding the JButton "nextButton" on the JPanel.
117. btnPanel.add(nextButton);
118.
119. // Adding the JButton "previousButton" on the JPanel.
120. btnPanel.add(previousButton);
121.
122. // Adding the JButton "lastButton" on the JPanel.
123. btnPanel.add(lastButton);
124.
125. // adding firstButton in the ActionListener
126. firstButton.addActionListener(new ActionListener()
Dr Bharti Sharma

127. {
128. public void actionPerformed(ActionEvent ae)
129. {
130.
131. // using the first cObjl CardLayout
132. cObjl.first(cPanel);
133.
134. // value of currCard is 1
135. currCard = 1;
136. }
137. });
138.
139. // add lastButton in ActionListener
140. lastButton.addActionListener(new ActionListener()
141. {
142. public void actionPerformed(ActionEvent ae)
143. {
144.
145. // using the last cObjl CardLayout
146. cObjl.last(cPanel);
147.
Dr Bharti Sharma

148. // value of currCard is 4


149. currCard = 4;
150. }
151. });
152.
153. // add nextButton in ActionListener
154. nextButton.addActionListener(new ActionListener()
155. {
156. public void actionPerformed(ActionEvent ae)
157. {
158.
159. if (currCard < 4)
160. {
161.
162. // increase the value of currCard by 1
163. currCard = currCard + 1;
164.
165. // show the value of currCard
166. cObjl.show(cPanel, "" + (currCard));
167. }
168. }
Dr Bharti Sharma

169. });
170.
171. // add previousButton in ActionListener
172. previousButton.addActionListener(new ActionListener()
173. {
174. public void actionPerformed(ActionEvent ae)
175. {
176.
177. if (currCard > 1)
178. {
179.
180. // decrease the value
181. // of currCard by 1
182. currCard = currCard - 1;
183.
184. // show the value of currCard
185. cObjl.show(cPanel, "" + (currCard));
186. }
187. }
188. });
189.
Dr Bharti Sharma

190. // using to get the content pane


191. getContentPane().add(cPanel, BorderLayout.NORTH);
192.
193. // using to get the content pane
194. getContentPane().add(btnPanel, BorderLayout.SOUTH);
195. }
196.
197. // main method
198. public static void main(String argvs[])
199. {
200.
201. // Creating an object of the CardLayoutExample3 class.
202. CardLayoutExample3 cll = new CardLayoutExample3();
203.
204. // method to set the default operation of the JFrame.
205. cll.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
206.
207. // aethod to set the visibility of the JFrame.
208. cll.setVisible(true);
209. }
210. }
Dr Bharti Sharma

Output:

You might also like