JAVA FlowLayout Demo

자바 GUI - Layout 중 왼쪽부터 오른쪽으로 차례로 늘어 놓는 FlowLayout Demo이다.

FlowLayout생성자는 세종류이다.
FlowLayout()
FlowLayout(int align) : FlowLayout.RIGHT , FlowLayout.LEFT ,FlowLayout.CENTER 값 중 선택하여 쓸 수 있다. 문단으로 치자면, 어느쪽으로 붙여서 정렬하느냐 이다.

Reference:최종명 외 2인,프로그래머를 위한 JAVA2 4E,홍릉출판사


SixButton.java




  1. import javax.swing.*;
  2. import java.awt.*;
  3. public class SixButtons extends JFrame{
  4.     protected JButton[] b= new JButton[6];
  5.     public SixButtons(){
  6.         super("FlowLayout Demo");
  7.         String[] name={"One","Two","Three","Four","Five","Six"};
  8.        
  9.         /* This interface is implemented by components
  10.          * that have a single JRootPane child:
  11.          *  JDialog,JFrame, JWindow, JApplet, JInternalFrame.
  12.          * The methods in this interface are just covers
  13.          * for the JRootPane properties,
  14.          *  e.g. getContentPane() is generally implemented like this:
  15.          *      public Container getContentPane() {
  16.          *      return getRootPane().getContentPane();
  17.          *  }
  18.          * reference:http://download.oracle.com/javase/7/docs/api/
  19.          * Interface : RootPaneContainer
  20.          */
  21.         getContentPane().setLayout(new FlowLayout());
  22.         for(int i=0; i<b.length;i++){
  23.             b[i]=new JButton(name[i]);
  24.             getContentPane().add(b[i]);
  25.         }
  26.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  27.         setSize(300,200);
  28.         setVisible(true);
  29.        
  30.     }
  31.     public static void main(String args[]){
  32.         SixButtons sb=new SixButtons();
  33.     }
  34.    
  35. }

    중간에 getContentPane() 이해가 안되서, API에서 퍼왔다.

    FlowLayout()으로 생성하면 FlawLayout(FlowLayout.CENTER)로 생성한 결과와 같이 나온다. 

댓글

이 블로그의 인기 게시물

[Win32 API] WINAPI - 함수호출규약

JAVA Frame Icon setting

JAVA Spinner