자바 폼과프레임 AWT 레이아웃 매니저
FlowLayout Manager
정의 : 컴포넌트를 프레임상에 원래의 크기대로 차례차례 배치하는 레이아웃 매니저 생성자 : FlowLayout(), FlowLayout(int pos)// pos = CENTER, LEFT, RIGHT
ex )
1 2 3 4 5 6 7 8 9 10 11 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(); this .setLayout(flow); this .add(lb); this .add(lb1); this .add(lb2); }
FlowLayout은 컴포넌트의 원래 크기를 그대로 나타낼 수 있는 레이아웃 메니져이다. 또한 FlowLayout의 default 생성자를 이용하면 중앙부가 기본배치의 기준점이되고 매개변수를 가지는 생성자를 이용하면 중앙, 좌측 우측 등을 기준점으로 잡을 수 있다는 것
GridLayout Manager
정의 : 프레임을 그리드(Grid)로 나누어 컴포넌트를 차례대로 배치하는 레이아웃 매니저 생성자 : GridLayout(int x, int y), GridLayout(int x, int y, int xgap, int ygap); ex )
1 2 3 4 5 6 7 8 9 10 11 12 13 private Label lb = new Label("A" );private Label lb1 = new Label("B" );private Label lb2 = new Label("C" );private Label lb3 = new Label("D" );public void init () { GridLayout Grid = new GridLayout(2 ,2 );<- 2 행 2 열의 구조 this .setLayout(grid); this .add(lb); this .add(lb1); this .add(lb2); this .add(lb2); }
*. Grid Layout은 프레임을 그리드로 분할하여 관리하는 레이아웃 매니져 따라서 행과 열로 구분해 주는 생성자가 있으며 또한 하나의 영역과 다른 영역사이에 간격을 지정할 수 있는 생성자도 있다. 그 간격도 x축 y축 간격을 서로 다르게 지정할수 있다
BorderLayout Manager
정의 : 프레임을 5개 방향(Center, North, South, West, East)으로 나누고 컴포넌트 를 특정 방향으로 배치하는 레이아웃 메니저 생성자 : BorderLayout(), BorderLayout(int xgap, int ygap);
ex )
1 2 3 4 5 6 7 8 9 10 11 12 13 14 private Label lb = new Label("A" );private Label lb1 = new Label("B" );private Label lb2 = new Label("C" );private Label lb3 = new Label("D" );public void init () { BorderLayout border = new BorderLayout(); this .setLayout(border); this .add("Center" , lb); this .add("North" , lb1); this .add("South" , lb2); this .add("West" , lb3); this .add("East" , lb4); }
*.각 방향을 표시하는 방법에는 위치를 지정하는 Center, North, South, West, East와 같은 문자열로 지정해준다
CardLayout Manager
. 정의 : 프레임에 카드를 엎어 놓은 듯이 여러개의 Container를 상속받은 Panel등과 같은 객체를 포개 놓은 듯한 배치의 레이아웃 매니저이다. 생성자 : CardLayout(), VardLayout(int hgap, int vgap) ex )1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 private Panel p1 = new Panel();private Panel p2 = new Panel();private Panel p3 = new Panel();private CardLayout card = new CardLayout();public 생성자(){ this .init(); this .setVisible(true ); try { Thread.sleep(5000 ); }catch (InterruptedException ee){} card.show(this , "aaa" ); try { Thread.sleep(5000 ); }catch (InterruptedException ee){} card.show(this , "bbb" ); try { Thread.sleep(5000 ); }catch (InterruptedException ee){} card.show(this , "ccc" ); } public void init () { p1.setBackground(Color.red); p2.setBackground(Color.green); p3.setBackground(Color.blue); this .setLayout(card); this .add("aaa" , p1); this .add("bbb" , p2); this .add("ccc" , p3); }
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)
. GridBagConstraints의 지정값
1 . gridx, gridy 컴포넌트의 x ,y 위치
2 . gridwidth, gridheight 컴포넌트의 디폴트 크기에 대한 폭과 높이의 소속 배율
3 . weightx, weighty 컴포넌트 각 영역의 크기 비율
4 . achor 영역 내부에서의 컴포넌트 위치
5 . fill 영역을 채우기 위한 속성 지정
6 . insets 컴포넌트의 영역 내부에서의 여백
7 . ipadx, ipady 컴포넌트의 크기추가
. GridBagLayout은 단독적으로 사용해서는 아무런 효과도 볼수가 없다 GridBagConstraints라는 클래스의 도움을 받아야 제대로 프레임을 분할해서 사용할수 있다
ex )
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 import java.awt.*;public class Round17_Ex04 { public static void main (String[] ar) { Round17_Ex04_Sub round = new Round17_Ex04_Sub(); } } class Round17_Ex04_Sub extends Frame { private Dimension dimen, dimen1; private int xpos, ypos; private Label lb1 = new Label("AAA" ); private Label lb2 = new Label("BBB" ); private Label lb3 = new Label("CCC" ); private Label lb4 = new Label("DDD" ); private Label lb5 = new Label("EEE" ); private Label lb6 = new Label("FFF" ); private Label lb7 = new Label("GGG" ); private Label lb8 = new Label("HHH" ); private Label lb9 = new Label("III" ); private Label lb10 = new Label("JJJ" ); public Round17_Ex04_Sub () { super (); this .init(); this .start(); this .setSize(300 , 200 ); dimen = Toolkit.getDefaultToolkit().getScreenSize(); dimen1 = this .getSize(); xpos = (int ) (dimen.getWidth() / 2 - dimen1.getSize() / 2 ); ypos = (int ) (dimen.getHeight() / 2 - dimen1.getHeight() / 2 ); this .setLocation(xpos, ypos); this .setVisible(true ); } public void init () { lb1.setBackground(Color.yellow); lb2.setBackground(Color.yellow); lb3.setBackground(Color.yellow); lb4.setBackground(Color.yellow); lb5.setBackground(Color.yellow); lb6.setBackground(Color.yellow); lb7.setBackground(Color.yellow); lb8.setBackground(Color.yellow); lb9.setBackground(Color.yellow); lb10.setBackground(Color.yellow); GridBagLayout gridbag = new GridBagLayout(); GridBagConstraints gc = new GridBagConstraints(); this .setLayout(gridbag); gc.gridx = 0 ; gc.gridy = 0 ; gridbag.setConstraints(lb1, gc); this .add(lb1); gc.gridx = 1 ; gc.gridy = 1 ; gridbag.setConstraints(lb2, gc); this .add(lb2); gc.gridx = 2 ; gc.gridy = 1 ; gridbag.setConstraints(lb3, gc); this .add(lb3); gc.gridx = 1 ; gc.gridy = 2 ; gridbag.setConstraints(lb4, gc); this .add(lb4); gc.gridx = 2 ; gc.gridy = 2 ; gridbag.setConstraints(lb5, gc); this .add(lb5); gc.gridx = 2 ; gc.gridy = 3 ; gridbag.setConstraints(lb6, gc); this .add(lb6); gc.gridx = 3 ; gc.gridy = 2 ; gridbag.setConstraints(lb7, gc); this .add(lb7); gc.gridx = 0 ; gc.gridy = 0 ; gc.gridwidth = 1 ; gc.gridheight = 1 ; gridbag.setConstraints(lb1, gc); this .add(lb1); gc.gridx = 1 ; gc.gridy = 1 ; gc.gridwidth = 2 ; gc.gridheight = 2 ; gridbag.setConstraints(lb2, gc); this .add(lb2); gc.gridx = 2 ; gc.gridy = 1 ; gc.gridwidth = 1 ; gc.gridheight = 1 ; gridbag.setConstraints(lb3, gc); this .add(lb3); gc.gridx = 1 ; gc.gridy = 2 ; gc.gridwidth = 1 ; gc.gridheight = 1 ; gridbag.setConstraints(lb4, gc); this .add(lb4); gc.gridx = 2 ; gc.gridy = 2 ; gc.gridwidth = 1 ; gc.gridheight = 1 ; gridbag.setConstraints(lb5, gc); this .add(lb5); gc.weightx = 5 ; gridbag.setConstraints(lb1, gc); this .add(lb1); gc.weightx = 2 ; gridbag.setConstraints(lb2, gc); this .add(lb2); gc.weightx = 1 ; gridbag.setConstraints(lb3, gc); this .add(lb3); gc.weightx = 5 ; gc.weighty = 1 ; gc.anchor = GridBagConstraints.NORTHEAST; gridbag.setConstraints(lb1, gc); this .add(lb1); gc.weightx = 2 ; gc.weighty = 1 ; gc.anchor = GridBagConstraints.WEST; gridbag.setConstraints(lb2, gc); this .add(lb2); gc.weightx = 1 ; gc.weighty = 1 ; gc.anchor = GridBagConstraints.SOUTHWEST; gridbag.setConstraints(lb2, gc); this .add(lb2); gc.weightx = 5 ; gc.weighty = 1 ; gc.fill = GridBagConstraints.BOTH; gridbag.setConstraints(lb1, gc); this .add(lb1); gc.weightx = 5 ; gc.weighty = 1 ; gc.fill = GridBagConstraints.VERTICAL; gridbag.setConstraints(lb2, gc); this .add(lb2); gc.weightx = 5 ; gc.weighty = 1 ; gc.fill = GridBagConstraints.HORIZONTAL; gridbag.setConstraints(lb3, gc); this .add(lb3); } public void start () { } }