아래는 GridBagLayout의 생성자입니다.

GridBagLayout은 GridLayout을 더 유연하게 사용할 수 있습니다.
GridLayout은 셀 영역이 (1x1) 모두 같은 형태이지만
GridBagLayout 은 엑셀처럼 셀을 병합하여 사용자가 원하는 스타일로 지정할 수 있습니다.
GridBagLayout은 생성에 다른 Layout에 비하면 조금 까다롭지만 확인해보도록 합시다.
1) GridBagLayout의 생성은 앞의 Layout과 같습니다.
setLayout(new GridBagLayout());
2) 레이아웃은 GridBagConstraints의 변수와 관계가 있다. (Constraints는 영어로 제약/ 통제)
아래는 GridBagConstraints의 생성자 입니다.

당연히 레이아웃이 통제를 받으니 GridBagConstraints는 필요한 존재입니다.
GridBagConstraints의 인스턴스를 확인하도록 하겠습니다.
GridBagConstraints.girdx 는 grid행렬의 시작점 x좌표
GridBagConstraints.girdy 는 grid행렬의 시작점 y좌표
GridBagConstraints.gridwidth 는grid행렬의 w너비(버튼이면 버튼의 가로 길이)
GridBagConstraints.gridheight 는grid행렬의 h너비(버튼이면 버튼의 세로 길이)

GridBagConstraints.weightx 는 비율로 영역 분배
GridBagConstraints.weighty 는 비율로 영역 분배

GridBagConstraints.fill 은 component을 배치하고 남는 여백 채우는 것
GridBagConstraints.fill= GridBagConstraints.BOTH (X,Y 축 채움)
GridBagConstraints.fill= GridBagConstraints.HORIZONTAL (X 축 채움)
GridBagConstraints.fill= GridBagConstraints.VERTICAL (Y 축 채움)
GridBagConstraints.fill= GridBagConstraints.NONE (X,Y 축 COMPONENT 그대로)

3)GridBagLayout은 GridBagConstraints을 아래와 같이 사용합니다.
add(component, GridBagConstraints의 객체이름);
예)
JButton b1=new JButton();
GridBagConstraints gbc=new GridBagConstraints();
add( b1, gbc);
위의 사항을 보셨다면 아래의 내용을 살펴보도록 합시다.
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class M7 extends JFrame {
JPanel
p1 = new JPanel();
JButton
b1 = new JButton("1");
JButton
b2 = new JButton("2");
public M7() {
p1.setLayout(new GridBagLayout()); //레이아웃 생성자
GridBagConstraints
gbc=new GridBagConstraints();
//GridBagLayout에 필요한 GridBagConstraints의 생성자
gbc.fill=GridBagConstraints.BOTH;
//x,y축 다채움
gbc.weightx=0.2;// 비율이 0.2:0.1이므로 버튼의 크기는 가로축으로 2배
gbc.gridx=0;
gbc.gridy=0; //버튼이 두개로 0,0 기준으로 생성
p1.add(b1,gbc);
gbc.weightx=0.1; // 비율이 0.2:0.1이므로 버튼의 크기는 가로축으로 1배
gbc.gridx=1;
gbc.gridy=0; //버튼이 두개로 1,0 가 버튼 생성시작점
p1.add(b2,gbc); //
add(p1);
//
------------------------------------------------
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 프레임의 x버튼을 활성화하여 닫기버튼이 실행가능해짐
setSize(500,
500); // 프레임 사이즈 지정
setVisible(true); // 프레임을 보이게 함
setLocationRelativeTo(null); // 프레임 실행시 위치 중앙
}
public static void main(String args[]) {
M7
test = new M7();
}
} |
<결과물>

위의 사항을 보셨다면 아래의 내용을 살펴보도록 합시다.
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class M6 extends JFrame {
JPanel
p1 = new JPanel();
JButton
b1 = new JButton("1");
JButton
b2 = new JButton("2");
JButton
b3 = new JButton("3");
JButton
b4 = new JButton("4");
JButton
b5 = new JButton("5");
JButton
b6 = new JButton("6");
JButton
b7 = new JButton("7");
public M6() {
setLayout(new GridBagLayout());
GridBagConstraints
gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.BOTH;
gbc.weightx = 1.0;
gbc.weighty = 1.0;
// y축의 비율은 1이며 아래는 0.5이다. 그렇다면 아래 전까지 모든 y셀은 두배가된다
// 1번과 4번 버튼의 y축의 길이는 6번과 7번축보다 2배라는 뜻
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 1;
gbc.gridheight = 1;
add(b1, gbc);
gbc.gridx = 1;
gbc.gridy = 0;
gbc.gridwidth = 1;
gbc.gridheight = 1;
add(b2, gbc);
gbc.gridx = 2;
gbc.gridy = 0;
gbc.gridwidth = 1;
gbc.gridheight = 1;
add(b3, gbc);
gbc.gridx = 0;
gbc.gridy = 1;
gbc.gridwidth = 2;
gbc.gridheight = 1;
add(b4, gbc);
gbc.gridx = 2;
gbc.gridy = 1;
gbc.gridwidth = 1;
gbc.gridheight = 1;
add(b5, gbc);
gbc.weighty = 0.5;
// y축의 비율은 1이며 아래는 0.5이다. 그렇다면 아래 전까지 모든 y셀은 두배가된다
// 1번과 4번 버튼의 y축의 길이는 6번과 7번축보다 2배라는 뜻
gbc.gridx = 0;
gbc.gridy = 2;
gbc.gridwidth = 3;
gbc.gridheight = 1;
add(b6, gbc);
gbc.gridx = 0;
gbc.gridy = 3;
gbc.gridwidth = 1;
gbc.gridheight = 1;
add(b7, gbc);
//
------------------------------------------------
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 프레임의 x버튼을 활성화하여 닫기버튼이 실행가능해짐
setSize(500,
500); // 프레임 사이즈 지정
setVisible(true); // 프레임을 보이게 함
setLocationRelativeTo(null); // 프레임 실행시 위치 중앙
}
public static void main(String args[]) {
M6
test = new M6();
}
} |
<결과물>

다음강의에서는 GridBagLayout을 더 효과적으로 사용하는 법을 알려드리겠습니다.
gbc.gridx = 0;
gbc.gridy = 3;
gbc.gridwidth = 1;
gbc.gridheight = 1;
add(b7, gbc);
를 각 component에 구현하기에는 너무 많은 일이 필요하니 이것을 좌표화 시켜서
(b7,0,3,1,1) 구현화하도록 하겠습니다.
자료가 마음에 드셨다면 자주 찾아주세요^^ 글 올리는데 힘이됩니다.