JAVA /Swing

BOX 로 버튼 정렬

Safeguard 2013. 2. 26. 19:14

 private Container con;
 private JButton[] bt = new JButton[6];
 private Box box , box1 ;
 public Exam07_sub(){
      super("Exam07");
      this.init();
      this.start();
      this.setSize(300,300);
      this.setVisible(true);
  
 }
 
 
 public void init(){
     con = this.getContentPane();
     con.setLayout(new FlowLayout());
 for(int i = 0 ; i < 6 ; i ++){
      bt[i] = new JButton(String.valueOf(i + 1));
  }
 box = Box.createHorizontalBox(); // 가로 배열 박스
 box1 = Box.createVerticalBox(); // 세로 배열 박스
 box1.add(Box.createVerticalStrut(50)); //  프레임 내 간격 조정
 
 // 가로 3개
 for(int i = 0 ; i < 3 ; i ++){
      box.add(bt[i]);
      box.add(Box.createHorizontalStrut(10)); // 버튼 간격 조정
 }
 
 // 세로 3개
 for(int i = 3 ; i < 6 ;i ++){ 
      box1.add(bt[i]);
      box1.add(Box.createVerticalStrut(10));
 }
 

 con.add(box);
 con.add(box1);
 
}

 

------------------------------------------

 

 private Container con;
 private JButton[] bt = new JButton[7];
 private Box box , box1 , box2 ;
 
 
 public Exam07_sub(){
  super("Exam07");
  this.init();
  this.start();
  this.setSize(300,300);
  this.setVisible(true);
  
 }
 
 
 public void init(){
     con = this.getContentPane();
     con.setLayout(new FlowLayout());
 for(int i = 0 ; i < 7 ; i ++){
      bt[i] = new JButton(String.valueOf(i + 1));
  }
 
 box = Box.createVerticalBox();
 box1 = Box.createHorizontalBox();


 for(int i = 0 ; i < 3 ; i ++){
      box.add(bt[i]);
 }
 box.add(Box.createGlue()); // 박스간 라인 포인트를 정렬 함
 for(int i = 3 ; i < 7 ; i ++){
      box1.add(bt[i]);
 }
 
 box2 = Box.createHorizontalBox();
 box2.add(box);
 box2.add(Box.createHorizontalStrut(30));
 box2.add(box1);

 con.add(box2);
 
}