GridBagLayout 클래스

GridBagLayout 클래스

GridBagLayout 클래스는 컴포넌트가 같은 크기일 필요 없이 수직·수평·기준선에 따라 정렬하는 유연한 레이아웃 관리자예요. 각 GridBagLayout 객체는 동적인 사각형 셀 그리드를 유지하는데, 각 컴포넌트는 하나 이상의 셀(표시 영역, display area)을 차지해요.

출처: Java API Reference

본문

GridBagLayout이 관리하는 각 컴포넌트는 GridBagConstraints 인스턴스와 연관돼요. 이 제약 객체는 컴포넌트의 표시 영역이 그리드의 어디에 위치해야 하는지, 표시 영역 안에서 컴포넌트가 어떻게 배치될지 지정해요. 또한 GridBagLayout은 컴포넌트의 최소·선호 크기도 고려해서 크기를 결정해요.

public class GridBagLayout
extends Object
implements LayoutManager2, Serializable

그리드의 전체 방향은 컨테이너의 ComponentOrientation 속성에 따라 달라져요.

  • 가로 왼쪽→오른쪽 방향: 그리드 좌표 (0,0)은 컨테이너 왼쪽 위 모서리이고 x는 오른쪽, y는 아래쪽으로 증가해요.
  • 가로 오른쪽→왼쪽 방향: 그리드 좌표 (0,0)은 오른쪽 위 모서리이고 x는 왼쪽으로 증가해요.

GridBagConstraints 커스터마이즈

GridBagLayout을 효과적으로 쓰려면 컴포넌트에 연관된 GridBagConstraints 객체 중 하나 이상을 커스터마이즈해야 해요.

  • gridx / gridy — 컴포넌트 표시 영역의 leading 모서리가 있는 셀을 지정해요. GridBagConstraints.RELATIVE(기본값)는 바로 직전에 추가된 컴포넌트 바로 다음에 배치하라는 의미예요.
  • gridwidth / gridheight — 표시 영역이 차지하는 행·열의 셀 수를 지정해요. 기본값은 1이고, REMAINDER는 현재 행/열의 마지막 셀까지, RELATIVE는 끝에서 두 번째 셀까지 차지해요.
  • fill — 표시 영역이 요청 크기보다 클 때 컴포넌트를 어떻게 리사이즈할지 결정해요. NONE(기본), HORIZONTAL, VERTICAL, BOTH가 있어요.
  • ipadx / ipady — 컴포넌트의 내부 패딩을 지정해요. 최소 크기에 ipadx·ipady 픽셀을 더해요.
  • insets — 컴포넌트와 표시 영역 가장자리 사이의 최소 공간(외부 패딩)을 지정해요.
  • anchor — 표시 영역 안에서 컴포넌트 위치를 지정해요. 절대값(NORTH, SOUTH, EAST, WEST, CENTER 등), 방향 상대값(PAGE_START, LINE_END 등), 기준선 상대값(BASELINE, ABOVE_BASELINE 등) 세 종류가 있어요.
  • weightx / weighty — 여분 공간을 분배하는 방법을 결정해요. 행에 weightx나 열에 weighty를 지정한 컴포넌트가 하나도 없으면 모든 컴포넌트가 컨테이너 중앙에 뭉쳐요.

기준선 정렬

각 행에는 기준선이 있을 수 있고, 그 행에서 유효한 기준선을 가지며 기준선을 따라 정렬된(anchor가 BASELINE, BASELINE_LEADING, BASELINE_TRAILING 중 하나인) 컴포넌트로 결정돼요. 행을 넘어 span하는 컴포넌트는 시작 행(기준선 리사이즈가 CONSTANT_ASCENT인 경우)이나 끝 행(CONSTANT_DESCENT인 경우)의 기준선에 정렬돼요.

예제 코드

import java.awt.*;
import java.util.*;
import java.applet.Applet;

public class GridBagEx1 extends Applet {
    protected void makebutton(String name, GridBagLayout gridbag, GridBagConstraints c) {
        Button button = new Button(name);
        gridbag.setConstraints(button, c);
        add(button);
    }
    public void init() {
        GridBagLayout gridbag = new GridBagLayout();
        GridBagConstraints c = new GridBagConstraints();
        setFont(new Font("SansSerif", Font.PLAIN, 14));
        setLayout(gridbag);
        c.fill = GridBagConstraints.BOTH;
        c.weightx = 1.0;
        makebutton("Button1", gridbag, c);
        makebutton("Button2", gridbag, c);
        makebutton("Button3", gridbag, c);
        c.gridwidth = GridBagConstraints.REMAINDER; //end row
        makebutton("Button4", gridbag, c);
        c.weightx = 0.0; //reset to the default
        makebutton("Button5", gridbag, c); //another row
        c.gridwidth = GridBagConstraints.RELATIVE; //next-to-last in row
        makebutton("Button6", gridbag, c);
        c.gridwidth = GridBagConstraints.REMAINDER; //end row
        makebutton("Button7", gridbag, c);
        c.gridwidth = 1; //reset to the default
        c.gridheight = 2;
        c.weighty = 1.0;
        makebutton("Button8", gridbag, c);
        c.weighty = 0.0; //reset to the default
        c.gridwidth = GridBagConstraints.REMAINDER; //end row
        c.gridheight = 1; //reset to the default
        makebutton("Button9", gridbag, c);
        makebutton("Button10", gridbag, c);
        setSize(300, 100);
    }
    public static void main(String args[]) {
        Frame f = new Frame("GridBag Layout Example");
        GridBagEx1 ex1 = new GridBagEx1();
        ex1.init();
        f.add("Center", ex1);
        f.pack();
        f.setSize(f.getPreferredSize());
        f.show();
    }
}

GridBagConstraintssetConstraints(Component, GridBagConstraints)add(Component, Object)로 컴포넌트에 연결해요.

더 알아보기