java에서 GridBagLayout으로 화면 분할이 안됩니다.
글쓴이: dltkddyd / 작성시간: 목, 2012/10/18 - 1:46오후
java 배운지 얼마 안 되는 초보입문자입니다. 지금 레이아웃 관리자에 배우고 있습니다. GridBagLayout이라는 것은 여러 셀에 하나의 컴포넌트를 표시할 수 있다고 해서 lb라는 레이블 컴포넌트를 배열로 4개를 만들었습니다. 프레임을 0~1행 0~2열로 구성하고
lb{0]은 빨간색
lb[1]은 초록색
lb[2]는 파란색
lb[3]은 하늘색
으로 설정했습니다. 소스는 다음과 같습니다.
import java.awt.*;
class GridBag extends Frame {
private Label[] lb=new Label[4];
private GridBagLayout gb;
private GridBagConstraints gbcon;
public GridBag() {
lb[0]=new Label("label1");
lb[1]=new Label("label2");
lb[2]=new Label("label3");
lb[3]=new Label("label4");
lb[0].setBackground(Color.RED);
lb[1].setBackground(Color.GREEN);
lb[2].setBackground(Color.BLUE);
lb[3].setBackground(Color.CYAN);
gb=new GridBagLayout();
setLayout(gb);
gbcon=new GridBagConstraints();
gbcon.fill=GridBagConstraints.BOTH;
gbcon.weightx=1.0;
gbcon.weighty=1.0;
makeCell(0,0,1,1,lb[0]);
makeCell(1,0,2,1,lb[1]);
makeCell(0,1,2,1,lb[2]);
makeCell(2,1,1,1,lb[3]);
setBounds(300,300,400,200);
setVisible(true);
}
public void makeCell(int x, int y, int width, int height, Label l) {
gbcon.gridx=x;
gbcon.gridy=y;
gbcon.gridwidth=width;
gbcon.gridheight=height;
gb.setConstraints(l,gbcon);
add(l);
}
}
public class ExecuteGridBag {
public static void main(String[] args) {
GridBag mygridbag=new GridBag();
}
}위의 코드대로라면 lb[1]과 lb[2]가 두 개의 셀을 차지해야 하는데, 생각과 다르게 컴포넌트들이 셀을 채우지 않습니다. 이 문제 어떻게 해결해야 하나요? 자바에 대해 문의할 곳이 많지 않네요... 도움 부탁드립니다.
Forums:


댓글 달기