(1) 개요
-FlowLayout Manager - 컴포넌트의 기본크기대로 (화면 정중앙기준)
-GridLayout Manager -그리드(2,3)처럼 화면을 나누어서 모든 영역을 다 사용
-BorderLayout Manager - 화면을 다섯개의 방향으로 나눔
-CardLayout Manager -카드게임처럼 여러 개의 카드를 겹쳐놓은듯하게 구성
-GridBagLayout Manager - 적절한 위치에 마음대로 배치하도록 함
(2) 예제
<1> FlowLayout
import java.awt.*;
class Exam_04_Sub extends Frame {
private Button bt1 = new Button("1");
private Button bt2 = new Button("2");
private Button bt3 = new Button("3");
private Button bt4 = new Button("4");
private Button bt5 = new Button("5");
private Button bt6 = new Button("6");
//플로우레이아웃
private FlowLayout fl=new FlowLayout(FlowLayout.RIGHT);
public Exam_04_Sub(String title) {
super(title);
this.init();
super.setSize(300, 200);
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frm = super.getSize();
int xpos = (int)(screen.getWidth() / 2 - frm.getWidth() / 2);
int ypos = (int)(screen.getHeight() / 2 - frm.getHeight() / 2);
super.setLocation(xpos, ypos);
//super.setResizable(false);
super.setVisible(true);
}
public void init() {
this.setLayout(fl); //Flow Layout Setting
this.add(bt1);
this.add(bt2);
this.add(bt3);
this.add(bt4);
this.add(bt5);
this.add(bt6);
}
}
public class Exam_04 {
public static void main(String[] ar) {
Exam_04_Sub ex = new Exam_04_Sub("제목");
}
}
<2> GridLayout
import java.awt.*;
class Exam_05_Sub extends Frame {
private Button bt1 = new Button("1");
private Button bt2 = new Button("2");
private Button bt3 = new Button("3");
private Button bt4 = new Button("4");
private Button bt5 = new Button("5");
private Button bt6 = new Button("6");
private GridLayout gl = new GridLayout(3 , 2 , 10 , 10);
// 행렬 (3행 2열), 여백 (10 픽셀, 10 픽셀)
public Exam_05_Sub(String title) {
super(title);
this.init();
super.setSize(300, 200);
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frm = super.getSize();
int xpos = (int)(screen.getWidth() / 2 - frm.getWidth() / 2);
int ypos = (int)(screen.getHeight() / 2 - frm.getHeight() / 2);
super.setLocation(xpos, ypos);
super.setResizable(false);
super.setVisible(true);
}
public void init() {
this.setLayout(gl);
this.add(bt1);
this.add(bt2);
this.add(bt3);
this.add(bt4);
this.add(bt5);
this.add(bt6);
}
}
public class Exam_05 {
public static void main(String[] ar) {
Exam_05_Sub ex = new Exam_05_Sub("제목");
}
}
<3>BorderLayout
import java.awt.*;
class Exam_06_Sub extends Frame {
private Button bt1 = new Button("1");
private Button bt2 = new Button("2");
private Button bt3 = new Button("3");
private Button bt4 = new Button("4");
private Button bt5 = new Button("5");
private BorderLayout bl = new BorderLayout(10, 10);
// 여백 설정 (10 픽셀씩)
public Exam_06_Sub(String title) {
super(title);
this.init();
super.setSize(300, 200);
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frm = super.getSize();
int xpos = (int)(screen.getWidth() / 2 - frm.getWidth() / 2);
int ypos = (int)(screen.getHeight() / 2 - frm.getHeight() / 2);
super.setLocation(xpos, ypos);
super.setResizable(false);
super.setVisible(true);
}
public void init() {
this.setLayout(bl); // BorderLayout 설정
/* 각각의 5개 위치에 컴포넌트 배치
this.add("North",bt1); // 첫번째 글자는 대문자여야 한다.
// this.add(bt1, BorderLayout.NORTH); 와 같은 의미
//this.add("South", bt2);
this.add("West", bt3);
//this.add("East", bt4);
this.add("Center", bt5);
}
}
public class Exam_06 {
public static void main(String[] ar) {
Exam_06_Sub ex = new Exam_06_Sub("제목");
}
}
<4>CardLayout
import java.awt.*;
class Exam_07_Sub extends Frame {
private Button bt1 = new Button("1");
private Button bt2 = new Button("2");
private Button bt3 = new Button("3");
private CardLayout cl = new CardLayout();
public Exam_07_Sub(String title) {
super(title);
this.init();
super.setSize(300, 200);
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frm = super.getSize();
int xpos = (int)(screen.getWidth() / 2 - frm.getWidth() / 2);
int ypos = (int)(screen.getHeight() / 2 - frm.getHeight() / 2);
super.setLocation(xpos, ypos);
super.setResizable(false);
super.setVisible(true);
try {
Thread.sleep(2000);
}catch(InterruptedException e) {}
cl.show(this, "bb"); //2초마다 현재화면에 bb카드를 보여주기
try {
Thread.sleep(2000);
}catch(InterruptedException e) {}
cl.show(this, "cc");
try {
Thread.sleep(2000);
}catch(InterruptedException e) {}
cl.show(this, "aa");
}
public void init() {
this.setLayout(cl);
this.add("aa", bt1); // 카드 이름, 컴포턴트 이름
this.add("bb", bt2);
this.add("cc", bt3);
}
}
public class Exam_07 {
public static void main(String[] ar) {
Exam_07_Sub ex = new Exam_07_Sub("제목");
}
}
<5>GridBackLayout
import java.awt.*;
class Exam_08_Sub extends Frame {
private Button bt1 = new Button("AAA");
private Button bt2 = new Button("BBB");
private Button bt3 = new Button("CCC");
private Button bt4 = new Button("DDD");
private Button bt5 = new Button("EEE");
private Button bt6 = new Button("FFF");
private Button bt7 = new Button("GGG");
private GridBagLayout gb = new GridBagLayout();
private GridBagConstraints gc = new GridBagConstraints();
//컴포넌트 속성을 조절하는 기능
public Exam_08_Sub(String title) {
super(title);
this.init();
super.setSize(300, 200);
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frm = super.getSize();
int xpos = (int)(screen.getWidth() / 2 - frm.getWidth() / 2);
int ypos = (int)(screen.getHeight() / 2 - frm.getHeight() / 2);
super.setLocation(xpos, ypos);
super.setResizable(false);
super.setVisible(true);
}
public void init() {
this.setLayout(gb);
gc.gridx = 0; // 0 행
gc.gridy = 0; // 0 행
gb.setConstraints(bt1, gc); //위치를 설정한 후
this.add(bt1);
//버튼 컴포넌트를 추가한다 => 모든 컴포넌트를 기준으로 좌측상단에 나타나게 한다.
gc.gridx = 1;
gc.gridy = 1;
gb.setConstraints(bt2, gc);
this.add(bt2);
gc.gridx = 2;
gc.gridy = 2;
gb.setConstraints(bt3, gc);
this.add(bt3);
gc.gridx = 3;
gc.gridy = 2;
gb.setConstraints(bt5, gc);
this.add(bt5);
gc.gridx = 2;
gc.gridy = 0;
gc.gridwidth = 2;//기본그리드영역크기에서 폭이 두배로
gc.gridheight = 2;//기본그리드영역크기에서 높이가 두배로
gb.setConstraints(bt4, gc);
this.add(bt4);
}
}
public class Exam_08 {
public static void main(String[] ar) {
Exam_08_Sub ex = new Exam_08_Sub("제목");
}
}