Swing

Swing 입력값 넣는 방법, 패널추가

alarim 2023. 4. 9. 11:37
package ch02;

import java.awt.FlowLayout;
import java.security.DomainCombiner;

import javax.swing.*;

public class MyComponents extends JFrame{
	private JButton button1;
	private JLabel label;		//글자를 넣어서 화면에 띄울수 있다.
	private JTextField textFieId;	//사용자한테 입력값을 받을 수 있는 컴포넌트
	private JPasswordField jPasswordFirId;
	private JCheckBox checkBox;
	
	public MyComponents() {
		initData();
		setInitLayout();
	}
	private void initData() {
		setTitle("컴포넌트 확인");
		setSize(900,900);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		button1 = new JButton("JButton");
		label = new JLabel("글자를 적는 컴포넌트");
		textFieId = new JTextField("아이디입력",20);
		jPasswordFirId = new JPasswordField("비번입력",10);
		checkBox = new JCheckBox("동의");
		
	}
	private void setInitLayout() {
		add(button1);
		add(label);
		add(textFieId);
		add(jPasswordFirId);
		add(checkBox);
		
		setLayout(new FlowLayout());
		setVisible(true);
		
		
	}
	public static void main(String[] args) {
		new MyComponents();
	}
}

package ch02;

import java.awt.BorderLayout;
import java.awt.Color;

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

public class MyFramePanel extends JFrame{
	private JButton button1;
	private JButton button2;
	private JButton button3;
	private JButton button4;
	private JButton button5;
	private JButton button6;
	private JButton button7;
	private JButton button8;

	//패널 : 컴포넌트들을 그룹화 시킬 수 있다. 즉 각각에 배치관리자를 지정할 수 있다.
	private JPanel panel1;
	private JPanel panel2;

public MyFramePanel() {
	initData();
	setInitLayout();
}
private void initData() {
	setTitle("패널추가 연습");
	setSize(600,400);
	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	panel1 = new JPanel();
	panel1.setBackground(Color.red);
	panel2 = new JPanel();
	panel2.setBackground(Color.yellow);
	
	// 버튼 초기화
	button1 = new JButton("button1");
	button2 = new JButton("button2");
	button3 = new JButton("button3");
	button4 = new JButton("button4");
	button5 = new JButton("button5");
	button6 = new JButton("button6");
	button7 = new JButton("button7");
	button8 = new JButton("button8");
}
private void setInitLayout() {
	add(panel1,BorderLayout.CENTER);
	add(panel2,BorderLayout.SOUTH);
	// 루트 패널 기본 레이아웃 BorderLayout 이다.
	//하지만 추가적으로 만들어 사용하는 Panel은 기본 레이아웃이 FlowLayout이다.
	
	panel1.add(button1);
	panel1.add(button3);
	panel1.add(button4);
	panel1.add(button5);
	//panel1.setLayout(null);
	//panel1.add(button2);
	panel2.add(button2);
	panel2.add(button7);
	panel2.add(button8);
	
	setVisible(true);
}
public static void main(String[] args) {
	new MyFramePanel();
}
}