이번 시간에는 자바 애플릿에서 BorderLayout과 GridLayout을 활용한 예제를 올려봅니다.
일단 결과 사진을 보여주면 아래와 같습니다.
보기에는 단순해보이지만, 생각해보다 머리를 쓰면서 구현을 해야 저 그림이 나옵니다.
먼저 BorderyLayout을 기반으로 하여
패널(Panel)울 두 개 생성하여 중앙, 남쪽에 배치하고
그 각각의 패널에 버튼을 붙입니다.
자 그럼 아래 소스 코드를 보시죠~
public class LayoutTest extends Applet {
Button y1,y2,y3,y4;
Button pp1,pp2;
Panel p1,p2; // 부모 패널 변수
@Override
public void init(){
//애플릿 전체의 레이아웃 설정
BorderLayout b = new BorderLayout();
setSize(400,400);
setLayout(b);
// 패널 1,2를 생성해서 부착
p1 = new MyPanel(); // 자식의 메소드를 오버라이드(상속 받아서 내가 필요한것만 재정의) 10,10,10,10로 여백 지정
p2 = new MyPanel(20,10,20,10); // MyPanel 클래스의 매개변수 4개 받는 생성자로 이동
add(p1, "Center");
add(p2, "South");
p1.setBackground(Color.YELLOW);
p1.setBackground(Color.PINK);
y1=new Button("버튼1");
y2=new Button("버튼2");
y3=new Button("버튼3");
y4=new Button("버튼4");
p1.setLayout(new GridLayout(2,2,10,10));
p1.add(y1);p1.add(y2);
p1.add(y3);p1.add(y4);
pp1=new Button("버튼1");
pp2=new Button("버튼2");
p2.setLayout(new GridLayout(2,1,10,10));
p2.add(pp1);
p2.add(pp2);
}
아래의 소스는 제가 재정의한 MyPanel 클래스입니다.
저 클래스는 애플릿의 안쪽 여백을 지정했습니다.
public class MyPanel extends Panel{
int a,b,c,d;
public MyPanel(){ // 기본 생성자
this(10,10,10,10);
}
// 생성자 오버로드
public MyPanel(int a, int b, int c, int d){
this.a=a;
this.b=b;
this.c=c;
this.d=d;
}
@Override
public Insets getInsets(){
return new Insets(a,b,c,d);
}
}

댓글 없음:
댓글 쓰기