Java/Excercise

swing ch02 GridLayoutEx

낭구리 2021. 9. 2. 18:00
package ch02;


import java.awt.GridLayout;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JButton;

public class GridLayout_Ex extends JFrame {

	ArrayList<JButton> jButtons = new  ArrayList<>();
	private final int MAX_COUNT = 5;
	
	
	//배열로 변경
//	private JButton button1;
//	private JButton button2;
//	private JButton button3;
//	private JButton button4;
//	private JButton button5;
//	private JButton button6;
//	JButton[] buttons = new JButton[7]; 배열
//	String[] titles = {"가","나","다","라","마","바"}; 배열
	
	
	
	
	public GridLayout_Ex() {
		initData();
		setInitLayout();
	}
	
	
	private void initData() {
		setTitle("GridLayout 연습");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setSize(400, 400);
//		button1= new JButton("가");
//		button2= new JButton("나");
//		button3= new JButton("다");
//		button4= new JButton("라");
//		button5= new JButton("마");
//		button6= new JButton("바");
		
//		for (int i = 0; i < buttons.length; i++) {
//			buttons[i] = new JButton(titles[i]);
//		}
		
		for (int i = 0; i < MAX_COUNT; i++) {
			jButtons.add( new JButton(i + ""));
			
		}
		
	}

	
	private void setInitLayout() {
		setVisible(true);
		setLayout(new GridLayout(2, 3));
		//GridLayout이라는 배치 관리자 2열(가로) 3행(세로)
//		add(button1);
//		add(button2);
//		add(button3);
//		add(button4);
//		add(button5);
//		add(button6);
		
		
		for (int i = 0; i < MAX_COUNT; i++) {
			this.add(jButtons.get(i));
		}
	}





	public static void main(String[] args) {

		new GridLayout_Ex();
		//그리드는 열 row 행 cols
		//this() 생성자호출
	}

}

'Java > Excercise' 카테고리의 다른 글

generic ch01 제네릭의 정의  (0) 2021.09.03
object ch02 StringBuilderTest  (0) 2021.09.02
swing ch02 BorderLayoutEx  (0) 2021.09.02
swing ch02 FlowLayoutEx  (0) 2021.09.02
WrapperEx3  (0) 2021.09.02