BoxLayout은 컴포넌트를 다른 컴포넌트 위에 쌓거나 컴포넌트를 하나의 행에 배치할수 있다. BoxLayout은 FlowLayout의 하나의 버전으로 간주할 수 있으나 좀 더 기능이 많다.

import javax.swing.*;
import java.awt.*;

public class MyFrame3 extends JFrame
{

    public MyFrame3()
    {
        this.setTitle("BoxLayoutTest");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       
        JPanel panel = new JPanel();
        panel.setLayout(new BoxLayout(panel,BoxLayout.Y_AXIS));
       
/*        JButton b1 = new JButton("Button1");
        b1.setAlignmentX(JComponent.CENTER_ALIGNMENT);
        panel.add(b1);
       
      

        JButton b2 = new JButton("Button2------------");
        b2.setAlignmentX(JComponent.CENTER_ALIGNMENT);
        panel.add(b2);
       
        JButton b3 = new JButton("Button3-----");
        b3.setAlignmentX(JComponent.CENTER_ALIGNMENT);
        panel.add(b3);
        */
      
        CreateButton("Button1", panel);
        CreateButton("Button2---------------", panel);
        CreateButton("Button3----", panel);
       
       
       
        this.add(panel);
       
        this.pack();//프레임을 패널 크기에 맞추기
        this.setVisible(true);
    }
   
    private void CreateButton(String bt, JPanel panel)
    {
        JButton b = new JButton(bt);
        b.setAlignmentX(JComponent.CENTER_ALIGNMENT);
        panel.add(b);
    }
   
}

'컴퓨터공학부 > 고급객체지향프로그래밍(Java)' 카테고리의 다른 글

FlowLayout  (0) 2010.10.29
CardLayout 클래스  (0) 2010.10.29
GridLayout 클래스  (0) 2010.10.29
BorderLayout 클래스  (0) 2010.10.29
10/22 중간고사  (0) 2010.10.22

+ Recent posts