-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBorderLayout_05.java
More file actions
68 lines (56 loc) · 2.66 KB
/
Copy pathBorderLayout_05.java
File metadata and controls
68 lines (56 loc) · 2.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package GUI;
import javax.swing.*;
import java.awt.*;
public class BorderLayout_05{
public static void main(String[] args){
JFrame frame = new JFrame("Layout Manager");
frame.setSize(500, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//frame.setLayout(new BorderLayout()); // set layout manager to borderlayout; although
// no need to do this, the default layout is BorderLayout manager
frame.setLayout(new BorderLayout(10, 10)); // with horizontal, vertical gap from center
frame.setVisible(true);
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
JPanel panel3 = new JPanel();
JPanel panel4 = new JPanel();
JPanel panel5 = new JPanel();
panel1.setBackground(Color.red);
panel2.setBackground(Color.yellow);
panel3.setBackground(Color.pink);
panel4.setBackground(Color.blue);
panel5.setBackground(Color.magenta);
panel1.setPreferredSize(new Dimension(100, 50)); // set dim. to panel1
panel2.setPreferredSize(new Dimension(100, 50));
panel3.setPreferredSize(new Dimension(100, 50));
panel4.setPreferredSize(new Dimension(100, 50));
panel5.setPreferredSize(new Dimension(100, 50));
frame.add(panel1, BorderLayout.NORTH); // set panel1 to North(top) of frame
frame.add(panel2, BorderLayout.WEST);
frame.add(panel3, BorderLayout.SOUTH);
frame.add(panel4, BorderLayout.EAST);
frame.add(panel5, BorderLayout.CENTER);
// for sub panel
JPanel panel6 = new JPanel();
JPanel panel7 = new JPanel();
JPanel panel8 = new JPanel();
JPanel panel9 = new JPanel();
JPanel panel10 = new JPanel();
panel5.setLayout(new BorderLayout(5, 5)); // panel5 will act as another container
panel6.setBackground(new Color(150, 150, 0)); // through RGB values
panel7.setBackground(Color.black);
panel8.setBackground(Color.green);
panel9.setBackground(new Color(0xFFFFBB)); // through HEX values
panel10.setBackground(Color.cyan);
panel6.setPreferredSize(new Dimension(50, 50));
panel7.setPreferredSize(new Dimension(50, 50));
panel8.setPreferredSize(new Dimension(50, 50));
panel9.setPreferredSize(new Dimension(50, 50));
panel10.setPreferredSize(new Dimension(50, 50));
panel5.add(panel6, BorderLayout.NORTH); // set panel6 subpanel at north of panel5
panel5.add(panel7, BorderLayout.EAST);
panel5.add(panel8, BorderLayout.SOUTH);
panel5.add(panel9, BorderLayout.WEST);
panel5.add(panel10, BorderLayout.CENTER);
}
}