본문 바로가기
Programming/Java

[Java] GridLayout 예제 #1 (GUI)

by castberry_ 2021. 2. 8.
반응형

GridLayout 

Grid는 한국어로 격자입니다. GridLayout은 격자형 배치가 필요할 때 쓰입니다. 

2차원 격자형태로 배치합니다. 

GridLayout 예제 1 

import javax.swing.*;
import java.awt.*;
 
public class GridLayoutTest{
    GridLayoutTest() {
        setTitle("GridLayout");        
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        GridLayout grid = new GridLayout(42);
        grid.setVgap(5); // 격자 사이 수직 간격 5 픽셀
        setLayout(grid);
        add(new JLabel(" 이름"));
        add(new JTextField(""));
        add(new JLabel(" 학번"));
        add(new JTextField(""));
        add(new JLabel(" 학과"));
        add(new JTextField(""));
        add(new JLabel(" 과목"));
        add(new JTextField(""));
 
        setSize(300200);
        setVisible(true);    }
    public static void main(String[] args) {
        new GridLayouttTest();
    }
}
cs

GridLayout 예제 2

import javax.swing.*;
import java.awt.*;
 
public class GridLayoutTest{
    GridLayoutTest() {
        setTitle("GridLayout");        
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        GridLayout grid = new GridLayout(43);
        grid.setVgap(5);
        grid.setHgap(5);
        setLayout(grid);
        for(int i=1; i<=9; i++)
            add(new JButton(Integer.toString(i)));
 
        add(new JButton("*"));
        add(new JButton("0"));
 
        setSize(300200);
        setVisible(true);
    }
    public static void main(String[] args) {
        new GridLayouttTest();
    }
}
cs

 

 

반응형

댓글