Java/Excercise

swing ch03 MyFrame2

낭구리 2021. 9. 3. 17:07
package ch03;

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridBagLayout;
import java.awt.GridLayout;

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

public class MyFrame2 extends JFrame{
	
	JPanel panel1;
	JPanel panel2;
	
	JButton button1;
	JButton button2;
	JButton button3;
	JButton button4;
	JButton button5;
	JButton button6;
	JButton button7;
	JButton button8;
	//버튼 4개 더만들기 2 -> p1, 2 --> p2
	
	public MyFrame2() {
		initData();
		setInitLayout();
	}
	private void initData() {
		setTitle("패널연습");
		setSize(400,400);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		
		panel1 = new JPanel();
		panel1.setBackground(Color.yellow);
		
		panel2 = new JPanel();
		panel2.setBackground(Color.blue);
		
		button1 =new JButton("버튼 1");
		button2 =new JButton("버튼 2");
		button3 =new JButton("버튼 3");
		button4 =new JButton("버튼 4");
		button5 =new JButton("버튼 5");
		button6 =new JButton("버튼 6");
		button7 =new JButton("버튼 7");
		button8 =new JButton("버튼 8");
	}
	private void setInitLayout() {
		setVisible(true);
		this.setLayout(new GridLayout(2,1));//2열1행
		this.add(panel1);
		this.add(panel2);
	
		//panel에 각각 배치관리자 가능 
		//첫번째는 왼쪽끝 두번째는 오른쪽끝
		panel1.setLayout(new FlowLayout(FlowLayout.LEFT,100,50));
//		panel1.setLayout(new FlowLayout(FlowLayout.RIGHT));
//		panel1.setLayout(new FlowLayout(FlowLayout.CENTER));
		
//		panel2.setLayout(new FlowLayout(FlowLayout.LEFT));
		panel2.setLayout(new FlowLayout(FlowLayout.RIGHT,100,50));
		//생성자는 모양 맞추기(좌표에서 줄어들게 되면 밑으로 간다.)
//		panel2.setLayout(new FlowLayout(FlowLayout.CENTER));
		//Flow는 기차처럼 정렬
		
		
		panel1.add(button1);
		panel1.add(button2);
		panel1.add(button3);
		panel1.add(button4);
		
		panel2.add(button5);
		panel2.add(button6);
		panel2.add(button7);
		panel2.add(button8);
		
	}
	public static void main(String[] args) {

		new MyFrame2();
		
	}
}

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

swing ch04 MainTest2  (0) 2021.09.03
swing ch04 MainTest  (0) 2021.09.03
swing ch03 Jpanel  (0) 2021.09.03
generic ch04 Material  (0) 2021.09.03
generic ch03 GenericPrinterTest  (0) 2021.09.03