GridBagLayout 제약조건을 사용하여 구성요소를 서로 상대적으로 배치할 수 있는 매우 유연한 레이아웃 관리자입니다. . 각 GridBagLayout 디스플레이 영역이라고 하는 하나 이상의 셀을 차지하는 각 구성 요소와 함께 동적 직사각형 셀 그리드를 사용합니다. GridBagLayout 에 의해 관리되는 각 구성요소 GridBagConstraints 와 연결되어 있습니다. 구성 요소가 표시 영역 내에서 배치되는 방식을 지정하는 인스턴스입니다.
GridBagConstraints
GridBagConstraints 를 사용자 정의할 수 있습니다. 하나 이상의 공용 인스턴스 변수를 설정하여 객체를 생성합니다. 이러한 변수는 구성요소 위치, 크기, 성장 인자, 앵커, 삽입, 채우기 및 패딩을 지정합니다. .
- 그리드x :구성 요소가 차지하는 가장 왼쪽 셀을 지정하는 int 값입니다. gridx는 구성 요소가 배치될 열을 지정합니다.
- 그리디 :구성 요소가 차지하는 최상위 셀을 지정하는 int 값입니다. gridy는 배치될 행을 지정합니다.
- 격자 높이 :컴포넌트가 차지하는 세로 셀의 수를 지정하는 int 값입니다.
- 격자폭 :컴포넌트가 차지하는 가로 셀의 수를 지정하는 int 값입니다.
- ipadx :각 컨트롤에 추가할 내부 수평 패딩의 양을 지정하는 int 값입니다.
- 아이패드 :각 컨트롤에 추가할 내부 수직 패딩의 양을 지정하는 int 값입니다.
- 삽입 :셀의 양쪽에 남겨둘 빈 공간의 양을 지정하는 Insets 개체입니다.
- 무게x :결과 레이아웃이 할당된 영역보다 수평으로 작은 경우 추가 수평 공간이 분산되는 방식을 지정하는 이중 값입니다.
- 무거운 :결과 레이아웃이 할당된 영역보다 세로로 작은 경우 추가 세로 공간이 분산되는 방식을 지정하는 이중 값입니다.
- 앵커 :셀 내 구성 요소의 정렬을 지정하는 int 값입니다.
- 채우기 :셀의 추가 공간으로 수행할 작업을 지정하는 int 값입니다.
- 상대적 :gridx 및 gridy 필드의 경우 이 필드는 구성 요소가 마지막으로 추가된 구성 요소 옆에 배치되도록 지정합니다. gridwidth 및 gridheight 필드의 경우 이 필드는 구성 요소가 행이나 열에서 마지막에서 다음 구성 요소가 되도록 지정합니다.
- 남음 :gridwidth 및 gridheight 필드의 경우 이 필드는 구성 요소가 행 또는 열의 마지막 구성 요소임을 지정합니다.
예시
import java.awt.*;
import javax.swing.*;
public class GridBagLayoutTest {
public static void main(String[] a) {
JFrame frame = new JFrame("GridBagLayout Test");
Container myPane = frame.getContentPane();
myPane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
setMyConstraints(c,0,0,GridBagConstraints.CENTER);
myPane.add(getFieldPanel(),c);
setMyConstraints(c,0,1,GridBagConstraints.CENTER);
myPane.add(getButtonPanel(),c);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 250);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
private static JPanel getFieldPanel() {
JPanel p = new JPanel(new GridBagLayout());
p.setBorder(BorderFactory.createTitledBorder("Details"));
GridBagConstraints c = new GridBagConstraints();
setMyConstraints(c,0,0,GridBagConstraints.EAST);
p.add(new JLabel("Name:"),c);
setMyConstraints(c,1,0,GridBagConstraints.WEST);
p.add(new JTextField(16),c);
setMyConstraints(c,0,1,GridBagConstraints.EAST);
p.add(new JLabel("System:"),c);
setMyConstraints(c,1,1,GridBagConstraints.WEST);
p.add(getSystemPanel(),c);
setMyConstraints(c,0,2,GridBagConstraints.EAST);
p.add(new JLabel("Language:"),c);
setMyConstraints(c,1,2,GridBagConstraints.WEST);
p.add(getLanguagePanel(),c);
setMyConstraints(c,0,3,GridBagConstraints.EAST);
p.add(new JLabel("Year:"),c);
setMyConstraints(c,1,3,GridBagConstraints.WEST);
p.add(new JComboBox(new String[] {"2019","2020","2021"}),c);
return p;
}
private static JPanel getButtonPanel() {
JPanel p = new JPanel(new GridBagLayout());
p.add(new JButton("OK"));
p.add(new JButton("Cancel"));
return p;
}
private static JPanel getSystemPanel() {
JRadioButton winButton = new JRadioButton("Windows",true);
JRadioButton macButton = new JRadioButton("Mac",false);
ButtonGroup systemGroup = new ButtonGroup();
systemGroup.add(winButton);
systemGroup.add(macButton);
JPanel p = new JPanel(new GridBagLayout());
p.add(winButton);
p.add(macButton);
return p;
}
private static JPanel getLanguagePanel() {
JPanel p = new JPanel(new GridBagLayout());
p.add(new JCheckBox("Java",true));
p.add(new JCheckBox("Python",true));
p.add(new JCheckBox("Spark",false));
return p;
}
private static void setMyConstraints(GridBagConstraints c, int gridx, int gridy, int anchor) {
c.gridx = gridx;
c.gridy = gridy;
c.anchor = anchor;
}
} 출력
