행이나 열 방향으로 일렬로 배치하는 레이아웃입니다. FlowLayout 과는 달리 가로 또는 세로로 배치하며 프레임의 끝을 만나도 줄바꿈을 하지 않습니다.


매개변수:

new BoxLayout(arg0, arg1)
컨테이너 와 x/y 축

Box.createHorizontalStrut(int Width)
수평 방향 Width 크기 만큼 빈 컴포넌트를 삽입

Box.createHorizontalGlue()
수평 방향 빈 컴포넌트 삽입

Box.createHorizontalBox()
수평 방향으로 여러 개의 컴포넌트를 하나로 묶습니다.

Box.createVerticalStrut(int Height)
수직 방향 Height 크기 만큼 빈 컴포넌트를 삽입

Box.createVerticalGlue()
수직 방향 빈 컴포넌트를 삽입

Box.createVerticalBox()
수직 방향으로 여러 개의 컴포넌트를 하나로 묶습니다.


// ex.1)
import java.awt.Container;
 
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
 
 
public class Ex04 extends JFrame{
    public Ex04() {
        Container con = getContentPane();
         
        // 수평 정렬: BoxLayout.X_AXIS
        // 수직 정렬: BoxLayout.Y_AXIS
        con.setLayout(new BoxLayout(con, BoxLayout.Y_AXIS));
         
        con.add(new JButton("버튼 1"));       
        con.add(new JButton("버튼 2"));   
        con.add(new JButton("버튼 3"));       
        con.add(new JButton("버튼 4"));       
        con.add(new JButton("버튼 5"));       
        con.add(new JButton("버튼 6"));
         
         
        setSize(370, 300);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
        new Ex04();
    }
}


다음은 빈 컴포넌트를 삽입한 예입니다.


// ex.2)
import java.awt.Container;
 
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import java.awt.Component;
 
 
public class Ex04 extends JFrame{
    public Ex04() {
        Container con = getContentPane();
         
        // 수평 정렬: BoxLayout.X_AXIS
        // 수직 정렬: BoxLayout.Y_AXIS
        con.setLayout(new BoxLayout(con, BoxLayout.Y_AXIS));
         
        con.add(new JButton("버튼 1"));
        // 가로 5 만큼 빈 컴포넌트 삽입
        con.add(Box.createHorizontalStrut(5));
        JButton button = new JButton("버튼 2");
        con.add(button);
        con.add(new JButton("버튼 3"));
        // 세로 10 인 빈 컴포넌트 삽입 
        con.add(Box.createVerticalStrut(10));
        JButton button_1 = new JButton("버튼 4");
        con.add(button_1);      
        con.add(new JButton("버튼 5"));
        // 버튼 5 와 버튼 6 사이의 간격을 최대한 벌리기 위해
        // 빈 컴포넌트를 삽입합니다.
        con.add(Box.createVerticalGlue());
        con.add(new JButton("버튼 6"));
         
        setSize(370, 300);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
        new Ex04();
    }
}


다음은 수직이나 수평 방향으로 컨포넌트를 여러 개 묶어 표현합니다.

 

// ex.3)
import java.awt.Container;
 
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
 
import java.awt.Panel;
 
 
public class Ex04 extends JFrame{
    public Ex04() {
         
        Box tableBox = Box.createHorizontalBox();
        Box mainBox = Box.createVerticalBox();
        Box descBox = Box.createHorizontalBox();
         
        mainBox.add(new JButton("버튼 1"));
        mainBox.add(new JButton("버튼 2"));
        mainBox.add(new JButton("버튼 3"));
         
        descBox.add(new JButton("버튼 4"));
        descBox.add(new JButton("버튼 5"));
        descBox.add(new JButton("버튼 6"));
         
        tableBox.add(new JButton("버튼 7"));
        tableBox.add(new JButton("버튼 8"));
        tableBox.add(new JButton("버튼 9"));
         
         
        JPanel pan = new JPanel();
        pan.setLayout(new BoxLayout(pan, BoxLayout.Y_AXIS));
        pan.add(descBox);
        pan.add(mainBox);
        pan.add(tableBox);
         
        add(pan);
         
         
        setSize(370, 300);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
        new Ex04();
    }
}

 

0 댓글