JAVA-AWT/JPanel(layout)

JAVA-AWT-JPanel-04(GridLayout)

IT-개발자 2018. 6. 14. 20:46
반응형




1.JPanel p1=new JPanel(LayoutManager layout);

레이아웃을 지정합니다.

레이아웃의 종류는 BorderLayout, GridLayout, FLowLayout, CardLayout, GridBagLayout, BoxLayout 이 있습니다.

 

레이아웃을 지정할 때는 객체를 생성해서 사용해야됩니다. (예: new GridLayout(x,y);)

x,y를 지정해서 표의 형식의 레이아웃입니다.


import java.awt.Color;

import java.awt.GridLayout;

 

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JPanel;

 

public class JPanel01 {

 

        JFrame f = new JFrame("제목");

        JPanel p1 = new JPanel();

        JButton b1 = new JButton("1");

        JButton b2 = new JButton("2");

        JButton b3 = new JButton("3");

        JButton b4 = new JButton("4");

        JButton b5 = new JButton("5");

        JButton b6 = new JButton("6");

 

        public JPanel01() {

 

               f.add(p1); // 프레임 f 판넬p1 추가한다.

               p1.setLayout(new GridLayout(3,2)); // 판넬의 레이아웃을 GridLayout으로 지정 3 x 2의 표를 지정 

 

               b1.setBackground(Color.PINK); // 버튼의 색깔지정

 

               p1.add(b1);

               p1.add(b2);

               p1.add(b3);

               p1.add(b4);

               p1.add(b5);

               p1.add(b6);

 

               f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 프레임의 x버튼을 활성화하여 닫기버튼이 실행가능해짐

               f.setLocation(600, 300); // 프레임 실행시 위치 지정

               f.setSize(500, 500); // 프레임 사이즈 지정

               f.setVisible(true); // 프레임을 보이게

        }

 

        public static void main(String args[]) {

               JPanel01 panel = new JPanel01();

 

        }

}

 


<결과물>


2. 간격있는 GridLayout 지정

new GridLayout(x,y,가로갭, 세로갭);


 mport java.awt.Color;

import java.awt.GridLayout;

 

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JPanel;

 

public class JPanel01 {

 

        JFrame f = new JFrame("제목");

        JPanel p1 = new JPanel();

        JButton b1 = new JButton("1");

        JButton b2 = new JButton("2");

        JButton b3 = new JButton("3");

        JButton b4 = new JButton("4");

        JButton b5 = new JButton("5");

        JButton b6 = new JButton("6");

 

        public JPanel01() {

 

               f.add(p1); // 프레임 f 판넬p1 추가한다.

               p1.setLayout(new GridLayout(3,2,40,10)); 

// 판넬의 레이아웃을 GridLayout으로 지정 3 x 2의 표를 지정 

 

               b1.setBackground(Color.PINK); // 버튼의 색깔지정

 

               p1.add(b1);

               p1.add(b2);

               p1.add(b3);

               p1.add(b4);

               p1.add(b5);

               p1.add(b6);

 

               f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

                // 프레임의 x버튼을 활성화하여 닫기버튼이 실행가능해짐

               f.setLocation(600, 300); // 프레임 실행시 위치 지정

               f.setSize(500, 500); // 프레임 사이즈 지정

               f.setVisible(true); // 프레임을 보이게 

        }

 

        public static void main(String args[]) {

               JPanel01 panel = new JPanel01();

 

        }

}

 


<결과물>



 자료가 마음에 드셨다면 자주 찾아주세요^^ 글 올리는데 힘이됩니다.

반응형