[Java][AWT] GridBagLayout


/* GridBagLayout 예제
JavaStudy(http://www.javastudy.co.kr)
→자바 프로그래밍 강좌→Java Programming / 박용우
→제8장 AWT(http://www.javastudy.co.kr/docs/yopark/chap08/chap08.html)
→→ 4. 컨테이너와 레이아웃 관리자(Layout Manager)
→→→ 바. 그리드백 레이아웃 관리자(GridBagLayout)
*/

import java.awt.*;


public class GridBagLayoutTest extends Frame {

Label lblSmtpServer, lblMailFrom, lblRcptTo, lblSubject, lblBody;
TextField smtpServer, mailFrom, rcptTo, subject;
TextArea body;

GridBagLayout gbl;
GridBagConstraints gbc;


public GridBagLayoutTest() {

smtpServer = new TextField(40);
mailFrom = new TextField(40);
rcptTo = new TextField(40);
subject = new TextField(40);
body = new TextArea(5, 40);

lblSmtpServer = new Label("메일서버: ", Label.RIGHT);
lblMailFrom = new Label("From: ", Label.RIGHT);
lblRcptTo = new Label("To: ", Label.RIGHT);
lblSubject = new Label("제 목: ", Label.RIGHT);
lblBody = new Label("내 용: ", Label.RIGHT);

lblSmtpServer.setBackground(Color.ORANGE);
lblRcptTo.setBackground(Color.ORANGE);
lblBody.setBackground(Color.ORANGE);
lblMailFrom.setBackground(Color.YELLOW);
lblSubject.setBackground(Color.YELLOW);


gbl = new GridBagLayout();
setLayout(gbl); //GridBagLayout을 설정

gbc = new GridBagConstraints(); //☆GridBagLayout에 배치할 컴포넌트 위치 정보 등을 담을 객체 준비

gbc.fill = GridBagConstraints.BOTH; //GridBagConstraints.fill: 컴포넌트의 디스플레이 영역이 컴포넌트가 요청한 크기보다 클 때,
//크기설정을 다시 할 것인가를 결정합니다. GridBagConstraints 클래스는 다음과 같은 값을 가능한 값으로 제공해 주고 있습니다.
// ridBagConstraints.NONE: 디폴트 값
// GridBagConstraints.HORIZONTAL: 수평적으로 확장하고 수직적으로는 확장하지 않습니다.
// GridBagConstraints. VERTICAL: 수직적으로 확장하고 수평적으로는 확장하지 않습니다.
// GridBagConstraints.BOTH: 수평 및 수직으로 확장합니다.

gbc.weightx = 1.0;
gbc.weighty = 1.0;
//GridBagConstraints.weightx, GridBagConstraints.weighty: 컴포넌트의 디스플레이 영역이 컴포넌트가 요청한 크기보다 클 때
//남는 영역을 각 컴포넌트들에게 배분해 주어야 하는데, 이 때 컴포넌트가 차지할 너비(weightx)와 높이(weighty)에 대한 웨이트(weight)를
//나타냅니다. 이 너비와 높이에 대한 웨이트는 각 컴포넌트마다 달리 줄 수 있습니다.
//만약, 모든 컴포넌트의 너비와 높이에 대한 웨이트가 0이면, 디스플레이 영역이 커지더라도(사용자가 윈도우의 창을 키울 때)
//각 컴포넌트에 배당되는 영역이 없으므로 모든 컴포넌트들은 한군데 위치하게 되고 남는 영역은 그냥 빈공간으로 나타나게 됩니다.


gbAdd(lblSmtpServer, 0, 0, 1, 1);
gbAdd(smtpServer, 1, 0, 3, 1);
gbAdd(lblMailFrom, 0, 1, 1, 1);
gbAdd( mailFrom, 1, 1, 3, 1);
gbAdd(lblRcptTo, 0, 2, 1, 1);
gbAdd(rcptTo, 1, 2, 3, 1);
gbAdd(lblSubject, 0, 3, 1, 1);
gbAdd(subject, 1, 3, 3, 1);
gbAdd(lblBody, 0, 4, 1, 1);
gbAdd(body, 1, 4, 1, 1);
gbAdd(new Button("Send"), 0, 5, 2, 1);

pack(); // setSize(400, 300);

}


private void gbAdd(Component c, int x, int y, int w, int h) {

gbc.gridx = x;
gbc.gridy = y;
//가장 왼쪽 위 gridx, gridy값은 0
gbc.gridwidth = w; //넓이
gbc.gridheight = h; //높이
//gridwidth를 GridBagConstraints.REMAINDER 값으로 설정하면 현재 행의 마지막 셀이되고,
//gridheight를 GridBagConstraints.REMAINDER 값으로 설정하면 현재 열의 마지막 셀이됩니다.
//gridwidth를 GridBagConstraints. RELATIVE 값으로 설정하면 현재 행의 다음 셀부터 마지막 셀까지 차지하고,
//gridheight를 GridBagConstraints. RELATIVE 값으로 설정하면 현재 열의 다음 셀부터 마지막 셀까지 차지하도록 합니다.

gbl.setConstraints(c, gbc); //컴포넌트를 컴포넌트 위치+크기 정보에 따라 GridBagLayout에 배치

add(c);

}


public static void main(String args[]) {

GridBagLayoutTest f = new GridBagLayoutTest();

f.setTitle("GridBagLayout");
f.setVisible(true);

}

}

Posted by Dy

2009/01/06 12:43 2009/01/06 12:43
Response
1022 Trackbacks , No Comment
RSS :
http://www.dy-room.com/tc/rss/response/7