가장 간단한 배치 관리자로서 각 컴포넌트들은 하나의 줄에서 차례로 배치되고 더 이상 공간이 없으면 다음 줄에 배치된다. 같은 줄에서 컴포넌트들이 정렬되는 방식도 지정이 가능하다. 기본적인 정렬 방법은 중앙에 정렬하는 방법이다. 다른 정렬 방법을 사용하려면 FlowLayout 생성자에서 지정을 하여야 한다. FlowLayout은 JPanel 클래스의 디폴트 배치 관리자이다. 컨테이너의 크기가 변경되면 자동적으로 각 컴포넌트가 재배치된다.


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

public class MyFrame extends JFrame
{
   
    public MyFrame()
    {
        this.setTitle("FlowLayoutTest");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//        this.setSize(300,200);
       
        JPanel panel = new JPanel();
        panel.setLayout(new FlowLayout());
        JButton b1 = new JButton("Button1");
        JButton b2 = new JButton("Button2");
        JButton b3 = new JButton("Button3");
        JButton b4 = new JButton("Button4");
        JButton b5 = new JButton("Button4");
        panel.add(b1);
        panel.add(b2);
        panel.add(b3);
        panel.add(b4);
        panel.add(b5);
        this.add(panel);
        pack();//프레임을 패널크기에 맞추기
        this.setVisible(true);
    }

 
}

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

흑흑  (0) 2010.11.01
과제2  (0) 2010.10.29
CardLayout 클래스  (0) 2010.10.29
BoxLayout 클래스  (0) 2010.10.29
GridLayout 클래스  (0) 2010.10.29

+ Recent posts