-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path53_BorderLayout
More file actions
70 lines (58 loc) · 2.64 KB
/
53_BorderLayout
File metadata and controls
70 lines (58 loc) · 2.64 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
69
70
//Layout Manager = Defines the natural layout for components within a container
//3 common managers
//BorderLayout = A BorderLayout places components in five areas: NORTH, SOUTH, WEST, EAST, CENTER
// All extra space is placed in the center area
import javax.swing.*;
import java.awt.*;
public class Main{
public static void main(String[] args){
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500,500);
frame.setLayout(new BorderLayout(10,10));//margin: empty space between panel
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.green);
panel3.setBackground(Color.yellow);
panel4.setBackground(Color.magenta);
panel5.setBackground(Color.blue);
panel1.setPreferredSize(new Dimension(100,50));
panel2.setPreferredSize(new Dimension(100,100));
panel3.setPreferredSize(new Dimension(100,100));
panel4.setPreferredSize(new Dimension(100,100));
panel5.setPreferredSize(new Dimension(100,100));
//--------------------sub panels--------------------------------
JPanel panel6 = new JPanel();
JPanel panel7 = new JPanel();
JPanel panel8 = new JPanel();
JPanel panel9 = new JPanel();
JPanel panel10 = new JPanel();
panel6.setBackground(Color.black);
panel7.setBackground(Color.darkGray);
panel8.setBackground(Color.gray);
panel9.setBackground(Color.lightGray);
panel10.setBackground(Color.white);
panel5.setLayout(new BorderLayout()); //panel 6,7,8,9,10 within panel5
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);
panel5.add(panel7, BorderLayout.SOUTH);
panel5.add(panel8, BorderLayout.WEST);
panel5.add(panel9, BorderLayout.EAST);
panel5.add(panel10, BorderLayout.CENTER);
//---------------------sub panels-----------------------------
frame.add(panel1, BorderLayout.NORTH);
frame.add(panel2, BorderLayout.WEST);
frame.add(panel3, BorderLayout.EAST);
frame.add(panel4, BorderLayout.SOUTH);
frame.add(panel5, BorderLayout.CENTER);
}
}