[실행 결과]
[생각하기]
이 프로그램을 작성하기 위해서는 버튼 객체를 50개 출력해야 합니다. 만약 배열을
사용하지 않는다면 소스 코드 양이 많을 것입니다. 배열을 사용해 간단하게
만들어 보세요
[소스]
/* Button3.java */
import java.awt.*;
public class Button3 extends Frame{
Label[] lbl = new Label[50]; //Label과 Button 배열을
Button[] btn = new Button[50]; //생성합니다.
public Button3(String str){
super(str);
setLayout(new FlowLayout());
for(int i=0; i<50; i++){ //50번 반복하면서 Label과
lbl[i] = new Label(i+"레이블"); //Button 객체를 생성합니다.
btn[i] = new Button(i+"버튼");
add(lbl[i]); //레이블과 버튼을 프레임에
add(btn[i]); //등록합니다.
}
setSize(500,500);
setVisible(true);
}
public static void main(String[] args) {
new Button3("버튼 예제 3");
}
}