즐거운프로그래밍

[자바] 배치 관리자(GridLayout 예제)

수수께끼 고양이 2023. 10. 24. 13:35
728x90
반응형

 

자바 AWT는 기존 GUI 프로그램과 다르게 배치 관리자라는 것을 이용해 컴포넌트를 배치한다.

배치관리자들은 아래와 같다.

BorderLayout, BoxLayout, CardLayout, DefaultMenuLayout, FlowLayout, GridBagLayout,
GridLayout, OverlayLayout, ScrollPaneLayout, SpringLayout, ViewportLayout 등

BorderLayout, GridLayout을 이용하면 적당히 컴포넌트들을 배치 할 수 있다.

 

 

GridLayout 

바둑판 같은 형태로 컴포넌트들을 배치하며, 계산기와 비슷한 형태이다.

GridLayout 객체를 생성할 때 가로와 세로 셀의 갯수를 인자로 넘겨주며, 각 셀의 크기가 모두 동일하기 때문에 배치되는 컴포넌트의 크기는 무시되며, 좌측에서 우측으로 위에서 아래의 순서로 컴포넌트를 배치한다.

 

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class Main {
    public static void main(String[] args) {
        Frame frm = new Frame("GridLayout 예제");
        frm.setBounds(100,100,300,200);

        frm.setLayout(new GridLayout(3,2));

        frm.add(new Button("버튼 1번"));
        frm.add(new Button("버튼 2번"));
        frm.add(new Button("버튼 3번"));
        frm.add(new Button("버튼 4번"));
        frm.add(new Button("버튼 5번"));

        frm.setVisible(true);

        frm.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                super.windowClosing(e);
                System.exit(0);
            }
        });
    }
}

 

 

728x90
반응형