Skip to content

Commit 35b2b43

Browse files
committed
layout in awt
Java swing and awt layout Null Layout, BorderLayout, FlowLayout, GridLayout
1 parent 71559ab commit 35b2b43

File tree

4 files changed

+305
-0
lines changed

4 files changed

+305
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package layout;
2+
3+
import java.awt.Color;
4+
import java.awt.Container;
5+
import javax.swing.JFrame;
6+
import javax.swing.JTextArea;
7+
8+
/**
9+
*
10+
* @author rafiul islam
11+
*/
12+
public class NullLayout {
13+
/**
14+
* Null Layout simply means fixed position.
15+
* When a component set on panel with a bound the will not
16+
* adjust with frame resize.
17+
*/
18+
public NullLayout(){
19+
20+
JFrame frame = new JFrame("Null Layout"); // create a frame
21+
frame.setBounds(10, 10, 800, 450); // set position on screen
22+
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // set exit on close
23+
24+
Container pane = frame.getContentPane(); // get the panel on frame
25+
pane.setLayout(null); // set the panel layout null
26+
pane.setBackground(Color.BLUE); // set panel or frame background color
27+
28+
JTextArea txtArea = new JTextArea(); // create a text area component
29+
txtArea.setBounds(0, 0, 700, 400); // set position on frame panel
30+
31+
pane.add(txtArea); // add the text area component on panel
32+
33+
frame.setResizable(true); // set frame resizable true
34+
frame.setVisible(true); // set frame visible after execution
35+
}
36+
37+
// driver function
38+
public static void main(String[] args) {
39+
new NullLayout(); // calling the object that will display the swing frame
40+
}
41+
}
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
package layout;
2+
3+
import java.awt.BorderLayout;
4+
import java.awt.Color;
5+
import java.awt.Container;
6+
import java.awt.Dimension;
7+
import javax.swing.JFrame;
8+
import javax.swing.JLabel;
9+
import javax.swing.SwingConstants;
10+
import javax.swing.SwingUtilities;
11+
12+
/**
13+
*
14+
* @author rafiul islam
15+
*/
16+
public class TheBorderLayout {
17+
/**
18+
* Border Layout provide a directional based layout.
19+
* A component can be set after, before, left , right,
20+
* top bottom of another.
21+
*
22+
* BorderLayout class provide some static positioning string
23+
* to set the components on a preferred position
24+
*
25+
* @see java.awt.BorderLayout
26+
*/
27+
28+
public TheBorderLayout(){
29+
30+
JFrame frame = new JFrame("Border Layout");// create a frame window
31+
frame.setPreferredSize(new Dimension(400, 220));// set dimension instead of position on screen
32+
frame.setResizable(true);// set resize of the window frame true
33+
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//set stop the program for close the frame
34+
35+
Container pane = frame.getContentPane();// get the content panel of the frame
36+
pane.setLayout(new BorderLayout());// set the panel layout
37+
38+
JLabel top = new JLabel("I AM FROM TOP",SwingConstants.CENTER);//create a jlabel and set the text on center
39+
top.setBackground(Color.red); //set the jabel background color
40+
top.setOpaque(true);// set background color visible
41+
42+
JLabel bottom = new JLabel("I AM FROM BOTTOM",SwingConstants.CENTER);
43+
bottom.setBackground(Color.LIGHT_GRAY);
44+
bottom.setOpaque(true);
45+
46+
JLabel right = new JLabel("I AM FROM RIGHT",SwingConstants.CENTER);
47+
right.setBackground(Color.ORANGE);
48+
right.setOpaque(true);
49+
50+
JLabel left = new JLabel("I AM FROM LEFT",SwingConstants.CENTER);
51+
left.setBackground(Color.GREEN);
52+
left.setOpaque(true);
53+
54+
JLabel center = new JLabel("I AM FROM CENTER",SwingConstants.CENTER);
55+
center.setBackground(Color.PINK);
56+
center.setOpaque(true);
57+
58+
pane.add(top, BorderLayout.PAGE_START);// set on the top
59+
pane.add(left, BorderLayout.WEST); // set on the left
60+
pane.add(right, BorderLayout.EAST); // set on the right
61+
pane.add(bottom, BorderLayout.PAGE_END); // set on the bottom
62+
pane.add(center, BorderLayout.CENTER); // set on the center
63+
64+
frame.pack(); // pack everything on the frame and ready to display
65+
frame.setVisible(true); // set frame visible
66+
}
67+
68+
public static class WithRowCol{
69+
public WithRowCol(){
70+
JFrame frame = new JFrame("Border Layout With GAP");//create a frame window
71+
frame.setPreferredSize(new Dimension(400, 220)); //set dimension
72+
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//set close to exit
73+
frame.setResizable(true); // set frame resizable
74+
75+
Container pane = frame.getContentPane(); // get content panel from frame
76+
pane.setLayout(new BorderLayout(20, 10));// set layout with Horizontal and Vertical Px gap
77+
78+
JLabel top = new JLabel("I AM FROM TOP",SwingConstants.CENTER);//create a jlabel and set the text on center
79+
top.setBackground(Color.red); //set the jabel background color
80+
top.setOpaque(true);// set background color visible
81+
82+
JLabel bottom = new JLabel("I AM FROM BOTTOM",SwingConstants.CENTER);
83+
bottom.setBackground(Color.LIGHT_GRAY);
84+
bottom.setOpaque(true);
85+
86+
JLabel right = new JLabel("I AM FROM RIGHT",SwingConstants.CENTER);
87+
right.setBackground(Color.ORANGE);
88+
right.setOpaque(true);
89+
90+
JLabel left = new JLabel("I AM FROM LEFT",SwingConstants.CENTER);
91+
left.setBackground(Color.GREEN);
92+
left.setOpaque(true);
93+
94+
JLabel center = new JLabel("I AM FROM CENTER",SwingConstants.CENTER);
95+
center.setBackground(Color.PINK);
96+
center.setOpaque(true);
97+
98+
pane.add(top, BorderLayout.PAGE_START);// set on the top
99+
pane.add(left, BorderLayout.WEST); // set on the left
100+
pane.add(right, BorderLayout.EAST); // set on the right
101+
pane.add(bottom, BorderLayout.PAGE_END); // set on the bottom
102+
pane.add(center, BorderLayout.CENTER); // set on the center
103+
frame.pack();
104+
frame.setVisible(true);
105+
}
106+
}
107+
108+
// driver function
109+
public static void main(String[] args) {
110+
//create a swing thread for run first class
111+
SwingUtilities.invokeLater(new Runnable(){
112+
public void run(){
113+
new TheBorderLayout();
114+
}
115+
});
116+
117+
//create another swing thread for run the sub-class
118+
SwingUtilities.invokeLater(new Runnable(){
119+
public void run(){
120+
new TheBorderLayout.WithRowCol();
121+
}
122+
});
123+
}
124+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package layout;
2+
3+
import java.awt.FlowLayout;
4+
import java.awt.Color;
5+
import java.awt.Container;
6+
import java.awt.Dimension;
7+
import javax.swing.JFrame;
8+
import javax.swing.JLabel;
9+
import javax.swing.SwingConstants;
10+
11+
/**
12+
*
13+
* @author rafiul islam
14+
*/
15+
public class TheFlowLayout {
16+
17+
/**
18+
* Flow Layout provide the view of automatically rearranged of components.
19+
* Flow window will rearrange every component position with their
20+
* preferred size on frame after resize the frame.It will also allow
21+
* to set horizontal and vertical gap between components.
22+
*
23+
* @see java.awt.FlowLayout
24+
*/
25+
public TheFlowLayout() {
26+
JFrame frame = new JFrame("Border Layout");// create a frame window
27+
frame.setPreferredSize(new Dimension(600, 340));// set dimension instead of position on screen
28+
frame.setResizable(true);// set resize of the window frame true
29+
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//set stop the program for close the frame
30+
31+
Container pane = frame.getContentPane();// get the content panel of the frame
32+
pane.setLayout(new FlowLayout(FlowLayout.CENTER,10,15));// set the panel layout
33+
34+
Dimension d = new Dimension(80, 30);// size for every jlabel
35+
36+
JLabel lb1 = new JLabel("One",SwingConstants.CENTER);//create a jlabel and set the text on center
37+
lb1.setBackground(Color.red); //set the jabel background color
38+
lb1.setOpaque(true);// set background color visible
39+
lb1.setPreferredSize(d);// set the label size
40+
41+
JLabel lb2 = new JLabel("Two",SwingConstants.CENTER);
42+
lb2.setBackground(Color.LIGHT_GRAY);
43+
lb2.setOpaque(true);
44+
lb2.setPreferredSize(d);
45+
46+
JLabel lb3 = new JLabel("Three",SwingConstants.CENTER);
47+
lb3.setBackground(Color.ORANGE);
48+
lb3.setOpaque(true);
49+
lb3.setPreferredSize(d);
50+
51+
JLabel lb4 = new JLabel("Four",SwingConstants.CENTER);
52+
lb4.setBackground(Color.GREEN);
53+
lb4.setOpaque(true);
54+
lb4.setPreferredSize(d);
55+
56+
JLabel lb5 = new JLabel("Five",SwingConstants.CENTER);
57+
lb5.setBackground(Color.PINK);
58+
lb5.setOpaque(true);
59+
lb5.setPreferredSize(d);
60+
61+
pane.add(lb1);// add componets on the frame panel with index(start from 0)
62+
pane.add(lb4);
63+
pane.add(lb3);
64+
pane.add(lb2,1);
65+
pane.add(lb5, 0);
66+
67+
frame.pack(); // pack everything on the frame and ready to display
68+
frame.setVisible(true); // set frame visible
69+
}
70+
71+
// driver function
72+
public static void main(String[] args) {
73+
new TheFlowLayout(); // invoke the frame to display
74+
}
75+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package layout;
2+
3+
import java.awt.Color;
4+
import java.awt.Container;
5+
import java.awt.Dimension;
6+
import java.awt.GridLayout;
7+
import javax.swing.JFrame;
8+
import javax.swing.JLabel;
9+
import javax.swing.SwingConstants;
10+
11+
/**
12+
*
13+
* @author rafiul islam
14+
*/
15+
public class TheGridLayout {
16+
/**
17+
* Grid Layout arrange component in row and col index.
18+
*
19+
* @see java.awt.GridLayout
20+
*/
21+
public TheGridLayout(){
22+
JFrame frame = new JFrame("Grid Layout");//create a jframe window
23+
frame.setPreferredSize(new Dimension(600, 350));//set window size
24+
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//set close to exit
25+
frame.setResizable(true);//set frame resizable
26+
27+
Container pane = frame.getContentPane();//get the panel of frame window
28+
pane.setLayout(new GridLayout(2, 3, 15, 10));//set panel layout:ROW,COL, HGAP, VGAP
29+
30+
Dimension d = new Dimension(100, 60);//dimension for components size
31+
32+
JLabel lb1 = new JLabel("One",SwingConstants.CENTER);//create a label component
33+
lb1.setBackground(Color.cyan);//set label background color
34+
lb1.setOpaque(true);//set background color to visible
35+
lb1.setPreferredSize(d);//set size
36+
37+
JLabel lb2 = new JLabel("Two",SwingConstants.CENTER);//create a label component
38+
lb2.setBackground(Color.red);//set label background color
39+
lb2.setOpaque(true);//set background color to visible
40+
lb2.setPreferredSize(d);//set size
41+
42+
JLabel lb3 = new JLabel("Three",SwingConstants.CENTER);//create a label component
43+
lb3.setBackground(Color.green);//set label background color
44+
lb3.setOpaque(true);//set background color to visible
45+
lb3.setPreferredSize(d);//set size
46+
47+
JLabel lb4 = new JLabel("Four",SwingConstants.CENTER);//create a label component
48+
lb4.setBackground(Color.yellow);//set label background color
49+
lb4.setOpaque(true);//set background color to visible
50+
lb4.setPreferredSize(d);//set size
51+
52+
pane.add(lb1);
53+
pane.add(lb4);
54+
pane.add(lb3);
55+
pane.add(lb2, 0); //add the component with index
56+
57+
frame.pack();//ready all component to view
58+
frame.setVisible(true); //set frame window visible
59+
}
60+
61+
//driver function
62+
public static void main(String[] args) {
63+
new TheGridLayout();
64+
}
65+
}

0 commit comments

Comments
 (0)