자바 폼과프레임 AWT 레이아웃 매니저

자바 폼과프레임 AWT 레이아웃 매니저


FlowLayout Manager


정의 : 컴포넌트를 프레임상에 원래의 크기대로 차례차례 배치하는 레이아웃 매니저
생성자 : FlowLayout(), FlowLayout(int pos)// pos = CENTER, LEFT, RIGHT

ex )

1
2
3
4
5
6
7
8
9
10
11
private Label lb = new Label("AAA");
private Label lb1 = new Label("BBB");
private Label lb2 = new Label("CCC");
public void init(){
FlowLayout flow = new FlowLayout();
//FlowLayout flow = new FlowLayout(FlowLayout.LEFT);
this.setLayout(flow);
this.add(lb);
this.add(lb1);
this.add(lb2);
}

  • FlowLayout은 컴포넌트의 원래 크기를 그대로 나타낼 수 있는 레이아웃 메니져이다.
    또한 FlowLayout의 default 생성자를 이용하면 중앙부가 기본배치의 기준점이되고
    매개변수를 가지는 생성자를 이용하면 중앙, 좌측 우측 등을 기준점으로 잡을 수 있다는 것

GridLayout Manager


정의 : 프레임을 그리드(Grid)로 나누어 컴포넌트를 차례대로 배치하는 레이아웃 매니저
생성자 : GridLayout(int x, int y), GridLayout(int x, int y, int xgap, int ygap);
ex )

1
2
3
4
5
6
7
8
9
10
11
12
13
private Label lb = new Label("A");
private Label lb1 = new Label("B");
private Label lb2 = new Label("C");
private Label lb3 = new Label("D");
public void init(){
GridLayout Grid = new GridLayout(2,2);<- 22열의 구조
//GridLayout Grid = new GridLayout(2,2,5,5);<- 2행 2열의 행의 5픽셀만큼 공간 열의 5픽셀 공간
this.setLayout(grid);
this.add(lb);
this.add(lb1);
this.add(lb2);
this.add(lb2);
}

*. Grid Layout은 프레임을 그리드로 분할하여 관리하는 레이아웃 매니져
따라서 행과 열로 구분해 주는 생성자가 있으며 또한 하나의 영역과 다른 영역사이에
간격을 지정할 수 있는 생성자도 있다.
그 간격도 x축 y축 간격을 서로 다르게 지정할수 있다

BorderLayout Manager


정의 : 프레임을 5개 방향(Center, North, South, West, East)으로 나누고 컴포넌트 를
특정 방향으로 배치하는 레이아웃 메니저
생성자 : BorderLayout(), BorderLayout(int xgap, int ygap);

ex )

1
2
3
4
5
6
7
8
9
10
11
12
13
14
private Label lb = new Label("A");
private Label lb1 = new Label("B");
private Label lb2 = new Label("C");
private Label lb3 = new Label("D");
public void init(){
BorderLayout border = new BorderLayout();
//BorderLayout border = new BorderLayout(5, 5);<- 행의 5px만큼의 공간, 열의 5px만큼의 공간
this.setLayout(border);
this.add("Center", lb);
this.add("North", lb1);
this.add("South", lb2);
this.add("West", lb3);
this.add("East", lb4);
}

*.각 방향을 표시하는 방법에는 위치를 지정하는 Center, North, South, West, East와
같은 문자열로 지정해준다

CardLayout Manager


  • . 정의 : 프레임에 카드를 엎어 놓은 듯이 여러개의 Container를 상속받은 Panel등과
    같은 객체를 포개 놓은 듯한 배치의 레이아웃 매니저이다.
    생성자 : CardLayout(), VardLayout(int hgap, int vgap)
    ex )
    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
    private Panel p1 = new Panel();
    private Panel p2 = new Panel();
    private Panel p3 = new Panel();
    private CardLayout card = new CardLayout();
    //private CardLayout card = new CardLayout(5, 5);
    public 생성자(){
    this.init();
    this.setVisible(true);
    try{
    Thread.sleep(5000);
    }catch(InterruptedException ee){}
    card.show(this, "aaa");

    try{
    Thread.sleep(5000);
    }catch(InterruptedException ee){}

    card.show(this, "bbb");
    try{
    Thread.sleep(5000);
    }catch(InterruptedException ee){}
    card.show(this, "ccc");
    }
    public void init() {
    p1.setBackground(Color.red);
    p2.setBackground(Color.green);
    p3.setBackground(Color.blue);
    this.setLayout(card);
    this.add("aaa", p1);
    this.add("bbb", p2);
    this.add("ccc", p3);
    }

GridBagLayout Manager

정의 : 프레임의 좌측 상단을 기준으로 폭과 크기를 정하여 컴포넌트를 배치하는 고차원의
레이아웃 매니져
생성자 : GridBagLayout()
관련 클래스 생성자 : GridBagConstraints(),
GridBagConstraints(int gridx, int gridy, int gridwidth,
int gridheight, int weightx, int weighty, int anchor, int fill,
Insets insets, int ipadx, int ipady)

  • . GridBagConstraints의 지정값

1 . gridx, gridy
컴포넌트의 x ,y 위치

2 . gridwidth, gridheight
컴포넌트의 디폴트 크기에 대한 폭과 높이의 소속 배율

3 . weightx, weighty
컴포넌트 각 영역의 크기 비율

4 . achor
영역 내부에서의 컴포넌트 위치

5 . fill
영역을 채우기 위한 속성 지정

6 . insets
컴포넌트의 영역 내부에서의 여백

7 . ipadx, ipady
컴포넌트의 크기추가

  • . GridBagLayout은 단독적으로 사용해서는 아무런 효과도 볼수가 없다
    GridBagConstraints라는 클래스의 도움을 받아야 제대로 프레임을 분할해서 사용할수 있다

ex )

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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167

import java.awt.*;

public class Round17_Ex04 {
public static void main(String[] ar) {
Round17_Ex04_Sub round = new Round17_Ex04_Sub();
}
}

class Round17_Ex04_Sub extends Frame {
private Dimension dimen, dimen1;
private int xpos, ypos;
private Label lb1 = new Label("AAA");
private Label lb2 = new Label("BBB");
private Label lb3 = new Label("CCC");
private Label lb4 = new Label("DDD");
private Label lb5 = new Label("EEE");
private Label lb6 = new Label("FFF");
private Label lb7 = new Label("GGG");
private Label lb8 = new Label("HHH");
private Label lb9 = new Label("III");
private Label lb10 = new Label("JJJ");

public Round17_Ex04_Sub() {
super();
this.init();
this.start();
this.setSize(300, 200);
dimen = Toolkit.getDefaultToolkit().getScreenSize();
dimen1 = this.getSize();
xpos = (int) (dimen.getWidth() / 2 - dimen1.getSize() / 2);
ypos = (int) (dimen.getHeight() / 2 - dimen1.getHeight() / 2);
this.setLocation(xpos, ypos);
this.setVisible(true);
}

public void init() {
lb1.setBackground(Color.yellow);
lb2.setBackground(Color.yellow);
lb3.setBackground(Color.yellow);
lb4.setBackground(Color.yellow);
lb5.setBackground(Color.yellow);
lb6.setBackground(Color.yellow);
lb7.setBackground(Color.yellow);
lb8.setBackground(Color.yellow);
lb9.setBackground(Color.yellow);
lb10.setBackground(Color.yellow);
GridBagLayout gridbag = new GridBagLayout();
// GridBagLayout의 객체를 선언한다
GridBagConstraints gc = new GridBagConstraints();
// GridBagConstraints의 객체를 선언한다 Default의 선언이다
this.setLayout(gridbag);
// 현재의 Frame의 Layout을 설정한다.
// 작업영역 시작
gc.gridx = 0;
gc.gridy = 0;
gridbag.setConstraints(lb1, gc);
this.add(lb1);
gc.gridx = 1;
gc.gridy = 1;
gridbag.setConstraints(lb2, gc);
this.add(lb2);
gc.gridx = 2;
gc.gridy = 1;
gridbag.setConstraints(lb3, gc);
this.add(lb3);
gc.gridx = 1;
gc.gridy = 2;
gridbag.setConstraints(lb4, gc);
this.add(lb4);
gc.gridx = 2;
gc.gridy = 2;
gridbag.setConstraints(lb5, gc);
this.add(lb5);
gc.gridx = 2;
gc.gridy = 3;
gridbag.setConstraints(lb6, gc);
this.add(lb6);
gc.gridx = 3;
gc.gridy = 2;
gridbag.setConstraints(lb7, gc);
this.add(lb7);
// gridx와 gridy는 임의의x, y축의 위치를 지정하는 속성이다.
gc.gridx = 0;
gc.gridy = 0;
gc.gridwidth = 1;
gc.gridheight = 1;
gridbag.setConstraints(lb1, gc);
this.add(lb1);
gc.gridx = 1;
gc.gridy = 1;
gc.gridwidth = 2;
gc.gridheight = 2;
gridbag.setConstraints(lb2, gc);
this.add(lb2);
gc.gridx = 2;
gc.gridy = 1;
gc.gridwidth = 1;
gc.gridheight = 1;
gridbag.setConstraints(lb3, gc);
this.add(lb3);
gc.gridx = 1;
gc.gridy = 2;
gc.gridwidth = 1;
gc.gridheight = 1;
gridbag.setConstraints(lb4, gc);
this.add(lb4);
gc.gridx = 2;
gc.gridy = 2;
gc.gridwidth = 1;
gc.gridheight = 1;
gridbag.setConstraints(lb5, gc);
this.add(lb5);
// gridwidth와 gridheight로 컴포넌트의 폭과 높이를 지정하기
// 속성을 사용해서 특정 컴포넌트의 영역배율을 확장시킬수 있다.
gc.weightx = 5;
gridbag.setConstraints(lb1, gc);
this.add(lb1);
gc.weightx = 2;
gridbag.setConstraints(lb2, gc);
this.add(lb2);
gc.weightx = 1;
gridbag.setConstraints(lb3, gc);
this.add(lb3);
// weightx, weighty를 사용하여 각 컴포넌트 영역의 크기비율을 지정한다 .
// weightx, weighty를 사용하게 되면 x축으로 차지할수 있는 비율과,
// y축으로 차지할수 있는 비율을 지정할수 있다
gc.weightx = 5;
gc.weighty = 1;
gc.anchor = GridBagConstraints.NORTHEAST;
gridbag.setConstraints(lb1, gc);
this.add(lb1);
gc.weightx = 2;
gc.weighty = 1;
gc.anchor = GridBagConstraints.WEST;
gridbag.setConstraints(lb2, gc);
this.add(lb2);
gc.weightx = 1;
gc.weighty = 1;
gc.anchor = GridBagConstraints.SOUTHWEST;
gridbag.setConstraints(lb2, gc);
this.add(lb2);
// anchor로 자기 영역에서의 위치를 지정할수 있다
gc.weightx = 5;
gc.weighty = 1;
gc.fill = GridBagConstraints.BOTH;
gridbag.setConstraints(lb1, gc);
this.add(lb1);
gc.weightx = 5;
gc.weighty = 1;
gc.fill = GridBagConstraints.VERTICAL;
gridbag.setConstraints(lb2, gc);
this.add(lb2);
gc.weightx = 5;
gc.weighty = 1;
gc.fill = GridBagConstraints.HORIZONTAL;
gridbag.setConstraints(lb3, gc);
this.add(lb3);
// fill 속성으로 각 영역을 채운다 .
// 각 영역을 채우는 속성으로 가로와 세로 축을 모두 채울것인지를 결정할수 있다
// 작업영역 끝
}

public void start() {
// Event 나 Thread 처리할 부분
}
}

Share