[Java Swing]GridLayout
# 그리드레이아웃_1107수업내용
- 원하는 행과 열로 구성된 레이아웃을 작성할때 사용한다.
- 사용형식
public GridLayout(int rows, int cols) {
this(rows, cols, 0, 0);
}
public GridLayout(int rows, int cols, int hgap, int vgap) {
if ((rows == 0) && (cols == 0)) {
throw new IllegalArgumentException("rows and cols cannot both be zero");
}
this.rows = rows;
this.cols = cols;
this.hgap = hgap;
this.vgap = vgap;
}
package gui;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
public class G03_GridLayout {
public static void main(String[] args) {
JFrame f = new JFrame("GridLayout Sample");
// 행과 열을 설정한 후 컴포넌트를 붙이는 방식의 레이아웃
GridLayout g1 = new GridLayout(5,3); // 5행 3열
g1.setHgap(15);
g1.setVgap(30);

f.setLayout(g1);
// 그리드 레이아웃에 컴포넌트 붙이기
for(int i = 0; i<15; ++i) {
f.add(new JButton("Button"+i));
}

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setBounds(100,100,500,500);
f.setVisible(true);
}
}