배치 관리자

Dec 29, 2023
배치 관리자
배치 관리자 : 컨테이너 안에 존재하는 컴포넌트들의 크기와 위치를 관리하는 객체
컨테이너마다 하나만 존재 가능
 

1. 배치 관리자 설정하기

1) 배치 관리자 객체를 생성
2) panel.setLayout(new 배치 관리자()); 로 설정
 

2. 배치 관리자의 종류

** hGap : 수평 간격
** vGap : 수직 간격

FlowLayout


package ex09; import javax.swing.*; import java.awt.*; public class MyFrame05 extends JFrame { public MyFrame05() { setTitle("FlowLayoutTest"); setSize(300, 150); setLayout(new FlowLayout()); add(new JButton(("Button1"))); add(new JButton(("Button2"))); add(new JButton(("Button3"))); add(new JButton(("Button4"))); add(new JButton(("Button5"))); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public static void main(String[] args) { MyFrame05 f = new MyFrame05(); } }
notion image

위쪽에서 아래쪽으로, 왼쪽에서 오른쪽으로 배치
FlowLayout(int align) : 정렬 방법을 지정
FlowLayout(int align, int hGap, int vGap) : 간격을 지정

BorderLayout


package ex09; import javax.swing.*; import java.awt.*; public class MyFrame06 extends JFrame { public MyFrame06() { setTitle("BorderLayoutTest"); setSize(300, 150); setLayout(new BorderLayout()); JButton b1 = new JButton("North"); JButton b2 = new JButton("South"); JButton b3 = new JButton("East"); JButton b4 = new JButton("West"); JButton b5 = new JButton("Center"); add(b1, "North"); add(b2, "South"); add(b3, "East"); add(b4, "West"); add(b5, "Center"); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public static void main(String[] args) { MyFrame06 f = new MyFrame06(); } }
notion image

동서남북 중앙의 5개 영역으로 구분하여 배치
BorderLayout(int hGap, int vGap) : 간격을 지정

GridLayout


package ex09; import javax.swing.*; import java.awt.*; public class MyFrame07 extends JFrame { public MyFrame07() { setTitle("GridLayoutTest"); setSize(300, 150); setLayout(new GridLayout(2, 3)); add(new JButton(("Button1"))); add(new JButton(("Button2"))); add(new JButton(("Button3"))); add(new JButton(("B4"))); add(new JButton(("Long Button5"))); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public static void main(String[] args) { MyFrame07 f = new MyFrame07(); } }
notion image

동일한 크기의 격자로 나누어 배치
GridLayout(int rows, int cols) :
GridLayout(int rows, int cols, int hGap, int vGap) : 간격을 지정

CardLayout


package ex09; import javax.swing.*; import java.awt.*; public class MyFrame08 extends JFrame { JButton b1, b2, b3; Container cPane; CardLayout layoutm; public MyFrame08() { setTitle("BorderLayoutTest"); setSize(300, 150); cPane = getContentPane(); layoutm = new CardLayout(); setLayout(layoutm); JButton b1 = new JButton("Card #1"); JButton b2 = new JButton("Card #2"); JButton b3 = new JButton("Card #3"); add(b1); add(b2); add(b3); b1.addActionListener(e->layoutm.next(cPane)); b2.addActionListener(e->layoutm.next(cPane)); b3.addActionListener(e->layoutm.next(cPane)); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public static void main(String[] args) { MyFrame08 f = new MyFrame08(); } }
notion image
notion image

카드처럼 겹치게 쌓아서 배치
next(container) : 주어진 컨테이너의 다음 카드로 이동
previous(container) : 주어진 컨테이너의 이전 카드로 이동
first(container) : 주어진 컨터이너의 첫 번째 카드로 이동
last(container) : 주어진 컨테이너의 마지막 카드로 이동
 

3. 절대 위치로 배치하기


package ex09; import javax.swing.*; public class MyFrame09 extends JFrame { JButton b1, b2; public MyFrame09() { setTitle("Absolute Position Test"); setSize(300, 150); setLayout(null); b1 = new JButton("Button #1"); add(b1); b1.setLocation(50, 30); b1.setSize(90, 50); b2 = new JButton("Button #2"); add(b2); b2.setBounds(180, 30, 90, 20); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public static void main(String[] args) { MyFrame09 f = new MyFrame09(); } }
notion image

1) 배치 관리자를 null로 설정
2) add()메서드를 사용하여 컴포턴트를 컨테이너에 추가
3) setSize(w, h)와 setLocation(x,y)을 이용하여 컴포넌트의 위치와 크기를 지정
setBounds(x, y, w, h)를 사용해도 됨
 
Share article
RSSPowered by inblog