[Java] JFrame 회원가입창 구현

Jeini·2022년 12월 14일
0

📌 Code list

목록 보기
37/55

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

public class HumanResult extends JFrame {
	private JLabel lblId;
	private JLabel lblName;
	private JLabel lblPassword;
	private JLabel lblReInfo;
	private JLabel lblGender;
	private JLabel lblSelf;

	private JTextField tfId;
	private JTextField tfName;
	private JPasswordField tfPassword;
	private JTextField tfReInfo;
	private JTextArea tfSelf;

	private JButton btnDup;
	private JButton btnJoin;
	private JButton btnCancel;

	private JRadioButton radiMan;
	private JRadioButton radiWoman;

	// 레이블의 크기를 고정시킬 수 있다.
	private Dimension sizeOfLabel = new Dimension(130, 20);
	private int widthOfInput = 10;

	public HumanResult() {
		init();
		setDisplay();
		showFrame();

	}

	private JLabel getLable(String text) {
		JLabel lbl = new JLabel(text, JLabel.LEFT);
		lbl.setPreferredSize(sizeOfLabel);
		return lbl;
	}

	// 구성요소 초기화
	private void init() {
		lblId = getLable("아이디");
		lblName = getLable("이름");
		lblPassword = getLable("비밀번호");
		lblReInfo = getLable("재입력");
		lblGender = new JLabel("성별", JLabel.LEFT);
		lblSelf = getLable("자기소개");

		tfId = new JTextField(widthOfInput);
		tfName = new JTextField(widthOfInput);
		tfPassword = new JPasswordField(widthOfInput);
		tfReInfo = new JTextField(widthOfInput);
		tfSelf = new JTextArea(5, 30);

		radiMan = new JRadioButton("남자");
		radiWoman = new JRadioButton("여자", true);

		btnDup = new JButton("중복검사");
		btnJoin = new JButton("가입");
		btnCancel = new JButton("취소");
	}

	// 배치
	private void setDisplay() {

		JPanel id = new JPanel(new FlowLayout(FlowLayout.LEFT));
		id.add(lblId);
		id.add(tfId);
		id.add(btnDup);

		JPanel name = new JPanel(new FlowLayout(FlowLayout.LEFT));
		name.add(lblName);
		name.add(tfName);

		JPanel password = new JPanel(new FlowLayout(FlowLayout.LEFT));
		password.add(lblPassword);
		password.add(tfPassword);

		JPanel reInfo = new JPanel(new FlowLayout(FlowLayout.LEFT));
		reInfo.add(lblReInfo);
		reInfo.add(tfReInfo);

		JPanel gender = new JPanel(new FlowLayout(FlowLayout.LEFT));
		gender.add(lblGender);
		lblGender.setPreferredSize(new Dimension(115, 20));
		gender.add(radiMan);
		gender.add(radiWoman);

		JPanel self = new JPanel(new FlowLayout(FlowLayout.LEFT));
		self.add(lblSelf);

		// 위에꺼 한꺼번에 묶음
		JPanel pnl1 = new JPanel(new GridLayout(0, 1));
		pnl1.add(id);
		pnl1.add(name);
		pnl1.add(password);
		pnl1.add(reInfo);
		pnl1.add(gender);
		pnl1.add(self);

		// NORTH
		JPanel pnlNorth = new JPanel();
		pnlNorth.add(pnl1);

		// CENTER
		JPanel area = new JPanel();
		JScrollPane scroll = new JScrollPane(tfSelf);
		area.add(scroll);

		// SOUTH
		JPanel button = new JPanel();
		button.add(btnJoin);
		button.add(btnCancel);
		button.setBorder(new EmptyBorder(10, 0, 10, 0));

		// 마지막 결과 배치
		add(pnlNorth, BorderLayout.NORTH);
		add(area, BorderLayout.CENTER);
		add(button, BorderLayout.SOUTH);

	}

	// 프레임 설정 및 가시성 확보
	private void showFrame() {
		setTitle("회원가입");
		pack();
		setLocation(100, 0);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setVisible(true);
	}

	public static void main(String[] args) {
		new HumanResult();

	}

}

✏️ 코드 리팩토링 후


import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
import javax.swing.text.JTextComponent;
import java.awt.*;

public class JoinForm extends JFrame {
    public static final int INPUT_SIZE = 10;
    public static final int LABEL_WIDTH = 50;

    public JoinForm() {
        init();
        setDisplay();
        showFrame();
    }

    private JButton btnCheck;
    private JRadioButton rbtnMale;
    private JRadioButton rbtnFemale;
    private JTextArea texIntroduce;
    private JButton btnJoin;
    private JButton btnCancle;

    private Dimension sizeOfLabel = new Dimension(100, 15);

    public void init() {
        btnCheck = new JButton("중복검사");
        rbtnMale = new JRadioButton("남자");
        rbtnFemale = new JRadioButton("여자", true);

        ButtonGroup group = new ButtonGroup();
        group.add(rbtnMale);
        group.add(rbtnFemale);

        texIntroduce = new JTextArea(4, 30);
        btnJoin = new JButton("가입");
        btnCancle = new JButton("취소");
    }
    private JPanel getPanel(String text) {
        return getPanel(text, false);
    }
    private JPanel getPanel(String text, boolean isPassword) {
        JPanel pnl = new JPanel(new FlowLayout(FlowLayout.LEFT));

        JLabel lbl = new JLabel(text, JLabel.LEFT);

        JTextComponent input;
        if(isPassword) {
            input = new JPasswordField(INPUT_SIZE);
        } else {
            input = new JTextField(INPUT_SIZE);
        }
        lbl.setPreferredSize(sizeOfLabel);
        pnl.add(lbl);
        pnl.add(input);
        return pnl;
    }
    private void setDisplay() {
        JPanel pnlMain = new JPanel(new BorderLayout());

        JPanel pnlNorth = new JPanel(new GridLayout(0, 1));

        JPanel pnlId = getPanel("아이디");
        pnlId.add(btnCheck);

        JPanel pnlGender = new JPanel(new FlowLayout(FlowLayout.LEFT));
        JLabel lblGender = new JLabel("성별", JLabel.LEFT);
        lblGender.setPreferredSize(sizeOfLabel);
        pnlGender.add(lblGender);
        pnlGender.add(rbtnMale);
        pnlGender.add(rbtnFemale);

        JPanel pnlIntroduce = new JPanel(new FlowLayout(FlowLayout.LEFT));
        JLabel lblIntroduce = new JLabel("자기소개", JLabel.LEFT);
        pnlIntroduce.add(lblIntroduce);

        pnlNorth.add(pnlId);
        pnlNorth.add(getPanel("이름"));
        pnlNorth.add(getPanel("비밀번호", true));
        pnlNorth.add(getPanel("재입력", true));
        pnlNorth.add(pnlGender);
        pnlNorth.add(pnlIntroduce);
        //====================================================
        JPanel pnlCenter = new JPanel();
        pnlCenter.add(new JScrollPane(texIntroduce));

        JPanel pnlSouth = new JPanel();
        pnlSouth.add(btnJoin);
        pnlSouth.add(btnCancle);

        pnlMain.add(pnlNorth, BorderLayout.NORTH);
        pnlMain.add(pnlCenter, BorderLayout.CENTER);
        pnlMain.add(pnlSouth, BorderLayout.SOUTH);

        pnlMain.setBorder(new EmptyBorder(10, 10, 10, 10));
        add(pnlMain, BorderLayout.CENTER);
    }
    private void showFrame() {
        setTitle("회원가입");
        pack();
        setLocationRelativeTo(null);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);
        setVisible(true);
    }

    public static void main(String[] args) {
        new JoinForm();
    }
}
profile
Fill in my own colorful colors🎨

0개의 댓글