AWT 프레임과 레이블
import java.awt.*;
public class Round17_Ex01 {
public static void main(String[] ar) {
Frame f = new Frame();
f.setSize(300, 200); // f.pack();
Dimension dimen = Toolkit.getDefaultToolkit().getScreenSize();
Dimension dimen1 = f.getSize();
int xpos = (int)(dimen.getWidth()/2 - dimen1.getWidth()/2); // 화면넓이/2 - Frame넓이/2
int ypos = (int)(dimen.getHeight()/2 - dimen1.getHeight()/2); // 화면높이/2 - Frame높이/2
f.setLocation(xpos, ypos); // Frame 위치 지정
f.setVisible(true);
}
}
상속관계
java.lang.Object -> java.awt.Component -> java.awt.Container -> java.awt.Window -> java.awt.Frame
사용할 기본 프레임
import java.awt.*;
public class Round17_Ex02 {
public static void main(String[] ar) {
Frame f = new Frame();
f.setSize(300, 200); // f.pack();
Dimension dimen = Toolkit.getDefaultToolkit().getScreenSize();
Dimension dimen1 = f.getSize();
int xpos = (int)(dimen.getWidth()/2 - dimen1.getWidth()/2); // 화면넓이/2 - Frame넓이/2
int ypos = (int)(dimen.getHeight()/2 - dimen1.getHeight()/2); // 화면높이/2 - Frame높이/2
f.setLocation(xpos, ypos); // Frame 위치 지정
f.setVisible(true);
}
class Round17_Ex02_Sub extends Frame {
private Dimension dimen, dimen1;
private int xpos, ypos;
public Round17_Ex02_Sub() {
super("제목 넣기");
this.init();
this.start();
this.setSize(300, 200);
dimen = Toolkit.getDefaultToolkit().getScreenSize();
dimen1 = f.getSize();
xpos = (int)(dimen.getWidth()/2 - dimen1.getWidth()/2);
ypos = (int)(dimen.getHeight()/2 - dimen1.getHeight()/2);
this.setLocation(xpos, ypos);
this.setVisible(true);
}
public void init() {
// 화면 구성 넣을 부분
}
public void start() {
// Event나 Thread 처리할 부분
}
}
}
AWT 레이아웃 매니저
FlowLayout Manager
컴포넌트를 프레임상에 원래의 크기대로 차례차례 배치하는 레이아웃 매니저
private Label lb = new Label("AAA");
private Label lb1 = new Label("BBB");
private Label lb2 = new Label("CCC");
....
public void init() {
FlowLayout flow = new FlowLayout(); // 기본 CENTER
//FlowLayout flow = new FlowLayout(FlowLayout.RIGHT);
//FlowLayout flow = new FlowLayout(FlowLayout.LEFT);
this.setLayout(flow);
lb.setBackground(Color.yellow);
lb1.setBackground(Color.yellow);
lb2.setBackground(Color.yellow);
this.add(lb);
this.add(lb1);
this.add(lb2);
}
GridLayout Manager
프레임을 그리드(Grid)로 나누어 컴포넌트를 차례대로 배치하는 레이아웃 매니저
생성자 : GridLayout(int x, int y), GridLayout(int x, int y, int xgap, int ygap)
1번 예
private List<Label> lbs = Arrays.asList(
new Label("A"), new Label("B"), new Label("C"), new Label("D")
);
....
public void init() {
GridLayout grid = new GridLayout(2, 2)
//GridLayout grid = new GridLayout(2, 2, 5, 5)
for(int i=0; i < lbs.size(); i++) {
lbs.get(i).setBackground(Color.yellow);
}
for(int i=0; i < lbs.size(); i++) {
this.add(lbs.get(i));
}
}
2번 예
private List<Label> lbs = Arrays.asList(
new Label("A"), new Label("B"), new Label("C"), new Label("D")
,new Label("E") ,new Label("F")
);
....
public void init() {
GridLayout grid = new GridLayout(2, 3)
//GridLayout grid = new GridLayout(2, 3, 10, 5)
for(int i=0; i < lbs.size(); i++) {
lbs.get(i).setBackground(Color.yellow);
}
for(int i=0; i < lbs.size(); i++) {
this.add(lbs.get(i));
}
}
3번 예
private List<Label> lbs = Arrays.asList(
new Label("A"), new Label("B"), new Label("C"), new Label("D")
,new Label("E"), new Label("F"),new Label("G"), new Label("H")
,new Label("I")
);
....
public void init() {
GridLayout grid = new GridLayout(3, 3)
//GridLayout grid = new GridLayout(3, 3, 5, 10)
for(int i=0; i < lbs.size(); i++) {
lbs.get(i).setBackground(Color.yellow);
}
for(int i=0; i < lbs.size(); i++) {
this.add(lbs.get(i));
}
}
BorderLayout Manager
프레임을 5개 방향(Center, North, South, West, East)으로 나누고 컴포넌트를 특정 방향으로 배치하는 레이아웃 메니저
생성자 : BorderLayout(), BorderLayout(int xgap, int ygap)
private List<Label> lbs = Arrays.asList(
new Label("A"), new Label("B"), new Label("C"), new Label("D"), new Label("E")
);
....
public void init() {
BorderLayout border = new BorderLayout();
// BorderLayout border = new BorderLayout(5, 5);
for(int i=0; i < lbs.size(); i++) {
lbs.get(i).setBackground(Color.yellow);
}
this.add(lbs.get(0), BorderLayout.CENTER); // this.add("Center", lbs.get(0));
this.add(lbs.get(1), BorderLayout.NORTH); // this.add("North", lbs.get(1));
this.add(lbs.get(2), BorderLayout.SOUTH); // this.add("South", lbs.get(2));
this.add(lbs.get(3), BorderLayout.WEST); // this.add("West", lbs.get(3));
this.add(lbs.get(4), BorderLayout.EAST); // this.add("East", lbs.get(4));
}
// 0 번째를 뺐을때
public void init() {
BorderLayout border = new BorderLayout();
// BorderLayout border = new BorderLayout(5, 5);
for(int i=0; i < lbs.size(); i++) {
if( i == 0) continue;
lbs.get(i).setBackground(Color.yellow);
}
this.add(lbs.get(1), BorderLayout.NORTH); // this.add("North", lbs.get(1));
this.add(lbs.get(2), BorderLayout.SOUTH); // this.add("South", lbs.get(2));
this.add(lbs.get(3), BorderLayout.WEST); // this.add("West", lbs.get(3));
this.add(lbs.get(4), BorderLayout.EAST); // this.add("East", lbs.get(4));
}
// 2, 4번째를 뺐을때
public void init() {
BorderLayout border = new BorderLayout();
// BorderLayout border = new BorderLayout(5, 5);
for(int i=0; i < lbs.size(); i++) {
if(i == 2 || i == 4) continue;
lbs.get(i).setBackground(Color.yellow);
}
this.add(lbs.get(0), BorderLayout.CENTER); // this.add("Center", lbs.get(0));
this.add(lbs.get(1), BorderLayout.NORTH); // this.add("North", lbs.get(1));
this.add(lbs.get(3), BorderLayout.WEST); // this.add("West", lbs.get(3));
}
CardLayout Manager
프레임에 카드를 엎어 놓은 듯이 여러 개의 Container를 상속받은 Panel 등과 같은 객체를 포개 놓은 듯한 배치의 레이아웃 매니저
생성자: CardLayout(), CardLayout(int hgap, int vgap)
private List<Panel> ps = Arrays.asList(new Panel(), new Panel(), new Panel());
private CardLayout card = new CardLayout();
//private CardLayout card = new CardLayout(5, 5);
public 생성자() {
....
this.setVisible(true);
try {Thread.sleep(5000);} catch (InterruptedException e){}
card.show(this,"aaa");
try {Thread.sleep(5000);} catch (InterruptedException e){}
card.show(this,"bbb");
try {Thread.sleep(5000);} catch (InterruptedException e){}
card.show(this, "ccc");
}
public void init() {
ps.get(0).setBackgound(Color.red);
ps.get(1).setBackgound(Color.green);
ps.get(2).setBackgound(Color.blue);
this.setLayout(card);
this.add("aaa",ps.get(0));
this.add("bbb",ps.get(1));
this.add("ccc",ps.get(2));
}
GridBagLayout Manager
프레임 좌측 상단을 기준으로 폭과 크기를 정하여 컴포넌트를 배치하는 고차원의 레이아웃 매니저
생성자: GridBagLayout()
관련클래스생성자: GridBagConstraints(), GridBagConstraints(int gridx, int gridy, int gridwidth, int gridheight, int weightx, int weighty, int anchor, int fill, Inset insets, int ipadx, int ipady)
GridBagConstraint의 지정 값
- .gridx, gridy : 컴포넌트의 x, y 위치
- gridwidth, gridheight: 컴포넌트 디폴트 크기에 대한 폭과 높이의 소속 배율
- weightx, weighty : 컴포넌트 각 크기의 비율
- anchor : 영역 내부에서의 컴포넌트 위치
- fill : 영역을 채우기 위한 속성 지정
- insets : 컴포넌트의 영역 내부에서의 여백
- ipadx, ipady : 컴포넌트의 크기 추가
import java.awt.*;
public class Round17_Ex04 {
public void main(String[] ar) {
new Round17_Ex04_Sub();
}
class Round17_Ex04_Sub extends Frame {
private Dimension dimen, dimen1;
private int xpos, ypos;
private List<Label> lbs = Arrays.asList(
new Label("A"), new Label("B"), new Label("C"), new Label("D")
,new Label("E"), new Label("F"),new Label("G"), new Label("H")
,new Label("I"),new Label("J")
);
public Roound17_Ex04_Sub(){
super();
this.init();
this.start();
this.pack(); // 현 Frame을 JVM이 정하는 크기에 맞춘다.
dimen = Toolkit.getDefaultToolkit().getScreenSize();
dimen1 = f.getSize();
xpos = (int)(dimen.getWidth()/2 - dimen1.getWidth()/2); // 화면넓이/2 - Frame넓이/2
ypos = (int)(dimen.getHeight()/2 - dimen1.getHeight()/2); // 화면높이/2 - Frame높이/2
this.setLocation(xpos, ypos); // Frame 위치 지정
this.setVisible(true);
}
public void init() {
for(int i =0; i < lbs.size(); i++) {
lbs.get(i).setBackground(Color.yellow);
}
GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints gc = new GridBagConstraints();
this.setLayout(gridbag);
// 작업영역 시작
// 작업영역 끝
}
public void start() {
// Event나 Thread 처리할 부분
}
}
}
gridx와 gridy로 위치를 지정하기
gc.girdx = 0; gc.girdy = 0; gridbag.setConstraints(lbs.get(0), gc); this.add(lbs.get(0));
gc.girdx = 1; gc.girdy = 1; gridbag.setConstraints(lbs.get(1), gc); this.add(lbs.get(1));
gc.girdx = 2; gc.girdy = 1; gridbag.setConstraints(lbs.get(2), gc); this.add(lbs.get(2));
gc.girdx = 1; gc.girdy = 2; gridbag.setConstraints(lbs.get(3), gc); this.add(lbs.get(3));
gc.girdx = 2; gc.girdy = 2; gridbag.setConstraints(lbs.get(4), gc); this.add(lbs.get(4));
gc.girdx = 2; gc.girdy = 3; gridbag.setConstraints(lbs.get(5), gc); this.add(lbs.get(5));
gc.girdx = 3; gc.girdy = 2; gridbag.setConstraints(lbs.get(6), gc); this.add(lbs.get(6));
gridwidth와 gridheight로 컴포넌트의 폭과 높이를 지정하기
B 라벨은 x축으로 2배, y축으로 2배 공간이라는 것을 확인할 수 있다.
gc.girdx = 0; gc.girdy = 0;
gc.girdwidth = 1; gc.girdheight = 1;
gridbag.setConstraints(lbs.get(0), gc); this.add(lbs.get(0));
gc.girdx = 1; gc.girdy = 1;
gc.girdwidth = 2; gc.girdheight = 2;
gridbag.setConstraints(lbs.get(1), gc); this.add(lbs.get(1));
gc.girdx = 2; gc.girdy = 1;
gc.girdwidth = 1; gc.girdheight = 1;
gridbag.setConstraints(lbs.get(2), gc); this.add(lbs.get(2));
gc.girdx = 1; gc.girdy = 2;
gc.girdwidth = 1; gc.girdheight = 1;
gridbag.setConstraints(lbs.get(3), gc); this.add(lbs.get(3));
gc.girdx = 2; gc.girdy = 2;
gc.girdwidth = 1; gc.girdheight = 1;
gridbag.setConstraints(lbs.get(4), gc); this.add(lbs.get(4));
gc.girdx = 2; gc.girdy = 3;
gc.girdwidth = 1; gc.girdheight = 1;
gridbag.setConstraints(lbs.get(5), gc); this.add(lbs.get(5));
gc.girdx = 3; gc.girdy = 2;
gc.girdwidth = 1; gc.girdheight = 1;
gridbag.setConstraints(lbs.get(6), gc); this.add(lbs.get(6));
weightx, weighty 를 사용하여 각 컴포넌트 영역의 크기 비율 지정하기
A, B, C가 차지하는 공간의 비율이 달라진다.
// this.pack(); --> this.setSize(250, 200);
this.setSize(250, 200);
gc.weightx = 5;
gridbag.setConstraints(lbs.get(0), gc); this.add(lbs.get(0));
gc.weightx = 2;
gridbag.setConstraints(lbs.get(1), gc); this.add(lbs.get(1));
gc.weightx = 1;
gridbag.setConstraints(lbs.get(2), gc); this.add(lbs.get(2));
anchor로 자기영역에서의 위치 지정하기
weighty를 지정한 이유는 전체높이를 영역으로 주기 위함
gc.weightx = 5; gc.weighty = 1; gc.anchor = GridBagConstraints.NORTHEAST;
gridbag.setConstraints(lbs.get(0), gc); this.add(lbs.get(0));
gc.weightx = 2; gc.weighty = 1; gc.anchor = GridBagConstraints.WEST;
gridbag.setConstraints(lbs.get(1), gc); this.add(lbs.get(1));
gc.weightx = 1; gc.weighty = 1; gc.anchor = GridBagConstraints.SOUTHWEST;
gridbag.setConstraints(lbs.get(2), gc); this.add(lbs.get(2));
fill 속성으로 각 영역을 채워넣기
//GridBagConstraints.NONE;
gc.weightx = 5; gc.weighty = 1; gc.fill = GridBagConstraints.BOTH;
gridbag.setConstraints(lbs.get(0), gc); this.add(lbs.get(0));
gc.weightx = 2; gc.weighty = 1; gc.fill = GridBagConstraints.VERTICAL;
gridbag.setConstraints(lbs.get(1), gc); this.add(lbs.get(1));
gc.weightx = 1; gc.weighty = 1; gc.fill = GridBagConstraints.HORIZONTAL;
gridbag.setConstraints(lbs.get(2), gc); this.add(lbs.get(2));
insets 속성으로 내부 여백 주기
//GridBagConstraints.NONE;
gc.weightx = 5; gc.weighty = 1; gc.fill = GridBagConstraints.BOTH;
gc.insets = new Insets(10, 10, 10, 10);
gridbag.setConstraints(lbs.get(0), gc); this.add(lbs.get(0));
gc.weightx = 2; gc.weighty = 1; gc.fill = GridBagConstraints.VERTICAL;
gc.insets = new Insets(0, 0, 0, 0);
gridbag.setConstraints(lbs.get(1), gc); this.add(lbs.get(1));
gc.weightx = 1; gc.weighty = 1; gc.fill = GridBagConstraints.HORIZONTAL;
gc.insets = new Insets(0, 0, 0, 0);
gridbag.setConstraints(lbs.get(2), gc); this.add(lbs.get(2));
ipadx와 ipady를 사용하여 컴포넌트 크기 추가하기
x축은 서로 맞물려 있으니 y축으로만 증가시킨다.
gc.weightx = 1 gc.weighty = 1; gc.ipady = 10;
gridbag.setConstraints(lbs.get(0), gc); this.add(lbs.get(0));
gc.weightx = 1 gc.weighty = 1; gc.ipady = 20;
gridbag.setConstraints(lbs.get(1), gc); this.add(lbs.get(1));
gc.weightx = 1; gc.weighty = 1; gc.ipady = 30;
gridbag.setConstraints(lbs.get(2), gc); this.add(lbs.get(2));