08
09


GridLayoutT.java


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

//GridLayout을 사용하면 컴포넌트를 행렬의 모양으로 배치할 수 있다
 
import java.awt.*;
import java.awt.event.*;
 
public class GridLayoutT{
    
    //프레임변수, 패널변수, 버튼변수를 선언한다
    private Frame f;
    private Panel p;
    private Button b1,b2,b3,b4;
    
    //메인에서 호출할 함수를 작성
    public GridLayoutT(){
        //프레임 생성
        f=new Frame("GridLayout Test");
        //버튼 생성
        b1 = new Button("btn1");
        b2 = new Button("btn2");
        b3 = new Button("btn3");
        b4 = new Button("btn4");
        //윈도우 종료
        f.addWindowListener(new WindowAdapter(){
            public void windowClosing(WindowEvent e){
                System.exit(0);
            }
        });
        //패널 생성
        p = new Panel();
        
        //GridLayout으로 행령 2x2 모양으로 컴포넌트를 배치한다
        p.setLayout(new GridLayout(2,2));
        p.add(b1);
        p.add(b2);
        p.add(b3);
        p.add(b4);
        
        //생성한 패널을 프레임에 배치
        f.add(p, "Center");
        
        //프레임 사이즈 및 표시
        f.setSize(300200);
        f.setVisible(true);
    }
    
    //메인 함수
    public static void main(String[] args){
        GridLayoutT gt = new GridLayoutT();
    }
}
cs


<실행결과>


반응형
COMMENT