Java/Swing

AWT 레이아웃 매니저

야구소년 2006. 11. 15. 11:25

1. FlowLayout Manager
  정의:컴포넌트를 프레임상에 원래의 크기대로 차례차례 배치하는 레이아웃 메니저
  생성자:FlowLayout(), FlowLayout(int pos)  // pos = CENTER, LEFT, RIGHT
     
 
private Label lb = new Label("AAA");
  FlowLayout flow = new FlowLayout(FlowLayout.RIGHT);  // 중앙,좌,우로 기준점 잡을수 있음
  this.setLayout(flow);
  lb.setBackground(Color.yellow);
  this.add(lb);

2. GridLayout Manager (창살,격자)
  정의:프레임을 Grid로 나누어 컴포넌트를 차례대로 배치하는 레이아웃 매니저
  생성자:GridLayout(int x, int y), GridLayout(int x, int y, int xgap, int ygap)
  - 컴포넌트의 크기가 프레임의 크기와 행과 열의 개수에 좌우됨

  private Label lb = new Label("AAA");
  GridLayout grid = new GridLayout(2,2,5,5); // 2행 2열에 x,y의 간격이 5
  this.setLayout(grid);
  lb.setBackground(Color.yellow);
  this.add(lb);

3. BorderLayout Manager (가장자리, 변두리)
  정의:프레임을 5개 방향(Center, North, South, West, East)으로 나누고 컴포넌트를 특정 방향으
       로 배치하는 레이아웃 메니저
  생성자:BorderLayout(), BorderLayout(int xgap, int ygap)
  - 컴포넌트의 크기가 프레임의 크기와 행과 열의 개수에 좌우됨  

  private Label lb = new Label("AAA");
  BorderLayout border = new BorderLayout(5,5);  // 2행 2열에 x,y의 간격이 5
  this.setLayout(border);
  lb.setBackground(Color.yellow);
  this.add(lb, BorderLayout.CENTER); or this.add("Center", lb);

4. GridBagLayout Manager
  정의:프레임의 좌측 상단을 기준으로 폭과 크기를 정하여 컴포넌트를 배치하는 고차원의 레이아웃 매니저
  생성자:GridBagLayout()
  관련 클래스 생성자:GridBagConstraints(), GridBagConstraints(int gridx, int gridy, int
                 gridwidth, int gridheight, int weightx, int weighty, int anchor, int
                 fill, Insets insets, int ipadx, int ipady)
  - GridBagLayout은 단독적으로 사용해서는 아무런 효과도 볼 수 없고, 반드시 GridBagConstraints
  클래스의 도움을 받아 프레임을 분할해서 사용해야 한다.


  [GridBagConstraints의 지정값]

  • gridx, gridy : 컴포넌트의 x, y 위치

     GridBagLayout grid = new GridBagLayout();
     GridBagConstraints gc = new GridBagConstraints();
     this.setLayout(gridbag);    // 이렇게 셋팅후 작업 시작

     gc.gridx = 0; gc.gridy = 0;
     gridbag.setConstraints(lb, gc);  // lb는 레이블 컴포넌트
     this.add(lb);

  • gridwidth, gridheight : 컴포넌트의 디폴트 크기에 대한 폭과 높이의 소속 배율

     gc.gridx = 0; gc.gridy = 0;
     gc.gridwidth = 1; gc.gridheight = 1;
     gridbag.setConstraints(lb, gc);
     this.add(lb);

  • weightx, weighty : 컴포넌트 각 영역의 크기 비율

     gc.weightx = 5;
     gridbag.setConstraints(lb, gc);
     this.add(lb);

  • anchor : 영역 내부에서의 컴포넌트 위치

     gc.weightx = 5; gc.weighty = 1;
     gc.anchor = GridBagConstraints.NORTHEAST;
     gridbag.setConstraints(lb, gc);
     this.add(lb);

  • fill : 영역을 채우기 위해 속성 지정

     gc.weightx = 5; gc.weighty = 1;
     gc.fill = GridBagConstraints.BOTH;
     gridbag.setConstraints(lb, gc);
     this.add(lb);

  • insets : 컴포넌트의 영역 내부에서의 여백

     gc.weightx = 5; gc.weighty = 1;
     gc.fill = GridBagConstraints.BOTH;
     Insets inset = new Insets(10,10,10,10);
     gc.insets = inset;
     gridbag.setConstraints(lb, gc);
     this.add(lb);

  • ipadx, ipady : 컴포넌트의 크기 추가

     gc.weightx = 5; gc.weighty = 1;
     gc.ipady = 10;
     gridbag.setConstraints(lb, gc);
     this.add(lb);