마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

버튼의 위치들을 붙여놓고 싶어요.

0 추천

안녕하세요. 스윙을 공부하고 있는 허접입니다.

아래의 이메일 텍스트 필드 다음에 확인과 취소, 다시작성 세가지 버튼이 있는데

이 버튼들을 가운데 옹기종기 모여있게 하고 싶은데 그 방법을 잘 모르겠습니다.

 

그리드백 레이아웃을 써서 그런지, c.gridx와 c.gridy의 값을 제데로 준것 같은데 왜 저렇게 서로

띄엄띄엄 띄어져서 생성이 되는지 잘 모르겠습니다.


import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;
import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

public class Exam2 extends JFrame {
   public void go() {


      JPanel panel = new JPanel(new GridBagLayout());
      GridBagConstraints c = new GridBagConstraints();
     
      JLabel label = new JLabel("아이디");
      JTextField idField = new JTextField(5);
      JButton btn = new JButton("중복확인");

      btn.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
            if(idField.getText().equals(""))
            {
               JOptionPane.showMessageDialog(null, "아이디를 입력해주세요.");
            }
            else{
               JOptionPane.showMessageDialog(null, "사용 가능한 아이디 입니다.");
            }
         }
      });
     
      JLabel l = new JLabel("회원가입");
     
      c.gridx = 1;
      c.gridy = 0;
      c.weightx = 0.3;
     
      panel.add(l, c);
     
     
      c.gridx = 0;
      c.gridy = 1;  
      c.anchor = GridBagConstraints.LINE_END;  
     
      panel.add(label, c);
     
      c.gridx = 1;
      c.gridy = 1;
     
      c.anchor = GridBagConstraints.CENTER;
      c.fill = GridBagConstraints.HORIZONTAL;
      panel.add(idField, c);
     
     
      c.gridx = 2;
      c.gridy = 1;
      c.anchor = GridBagConstraints.LINE_START;
      c.fill = GridBagConstraints.NONE;
      panel.add(btn, c);
      // btn 부분에 이벤트 발생시키는거 여기다 하기

     
      JLabel pw = new JLabel("비밀번호");
      JPasswordField pwField = new JPasswordField(5);
      c.gridx = 0;
      c.gridy = 2;
      c.anchor = GridBagConstraints.LINE_END;
      panel.add(pw, c);
     
      c.gridx = 1;
      c.gridy = 2;
      c.fill = GridBagConstraints.HORIZONTAL;
      c.anchor = GridBagConstraints.CENTER;
      panel.add(pwField, c);
     
      JLabel pwConfirm = new JLabel("비밀번호 확인");
      JPasswordField confirmField = new JPasswordField(5);
      c.gridx = 0;
      c.gridy = 3;
      c.fill = GridBagConstraints.NONE;
      c.anchor = GridBagConstraints.LINE_END;
      panel.add(pwConfirm, c);
     
      c.gridx = 1;
      c.gridy = 3;
      c.fill = GridBagConstraints.HORIZONTAL;
      c.anchor = GridBagConstraints.CENTER;
      panel.add(confirmField, c);
     
      JLabel name = new JLabel("이름");
      JTextField nameField = new JTextField(5);
      c.gridx = 0;
      c.gridy = 4;
      c.fill = GridBagConstraints.NONE;
      c.anchor = GridBagConstraints.LINE_END;
      panel.add(name, c);
     
      c.gridx = 1;
      c.gridy = 4;
      c.fill = GridBagConstraints.HORIZONTAL;
      c.anchor = GridBagConstraints.CENTER;
      panel.add(nameField, c);
     
      JLabel birth = new JLabel("생년월일");
      JTextField birthField = new JTextField(5);
      c.gridx = 0;
      c.gridy = 5;
      c.fill = GridBagConstraints.NONE;
      c.anchor = GridBagConstraints.LINE_END;
      panel.add(birth, c);
     
      c.gridx = 1;
      c.gridy = 5;
      c.fill = GridBagConstraints.HORIZONTAL;
      c.anchor = GridBagConstraints.CENTER;
      panel.add(birthField, c);
     
      JLabel phone = new JLabel("전화번호");
      JTextField phoneField = new JTextField(5);
      c.gridx = 0;
      c.gridy = 6;
      c.fill = GridBagConstraints.NONE;
      c.anchor = GridBagConstraints.LINE_END;
      panel.add(phone, c);
     
      c.gridx = 1;
      c.gridy = 6;
      c.fill = GridBagConstraints.HORIZONTAL;
      c.anchor = GridBagConstraints.CENTER;
      panel.add(phoneField, c);
     
      JLabel address = new JLabel("주소");
      JTextField addressField = new JTextField(5);
      c.gridx = 0;
      c.gridy = 7;
      c.fill = GridBagConstraints.NONE;
      c.anchor = GridBagConstraints.LINE_END;
      panel.add(address, c);
     
      c.gridx = 1;
      c.gridy = 7;
      c.fill = GridBagConstraints.HORIZONTAL;
      c.anchor = GridBagConstraints.CENTER;
      panel.add(addressField, c);
     
     
      JLabel mail = new JLabel("E-mail");
      JTextField mailField = new JTextField(5);
      c.gridx = 0;
      c.gridy = 8;
      c.fill = GridBagConstraints.NONE;
      c.anchor = GridBagConstraints.LINE_END;
      panel.add(mail, c);
     
      c.gridx = 1;
      c.gridy = 8;
      c.fill = GridBagConstraints.HORIZONTAL;
      c.anchor = GridBagConstraints.CENTER;
      panel.add(mailField, c);
     
      JButton regist = new JButton("확인");
      regist.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
            if (idField.getText().equals("")) {
               JOptionPane.showMessageDialog(null, "아이디가 비어있습니다.");
            } else if (String.valueOf(pwField.getPassword()).equals("")) {
               JOptionPane.showMessageDialog(null, "비밀번호가 비어있습니다.");
            } else if (String.valueOf(confirmField.getPassword()).equals("")) {
               JOptionPane.showMessageDialog(null, "비밀번호 확인 부분이 비어있습니다.");
            } else if (nameField.getText().equals("")) {
               JOptionPane.showMessageDialog(null, "이름 부분이 비어있습니다.");
            } else if (phoneField.getText().equals("")) {
               JOptionPane.showMessageDialog(null, "전화번호 부분이 비어있습니다.");
            } else {
               JOptionPane.showMessageDialog(null, "회원가입이 완료되었습니다.");
               dispose();
            }
         }
      });

      c.gridx = 0;
      c.gridy = 9;
      c.weightx = 0.3;
      c.fill = GridBagConstraints.NONE;
      panel.add(regist, c);
     
      JButton cancel = new JButton("취소");
      cancel.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
            dispose();
         }
      });
     
      c.gridx = 1;
      c.gridy = 9;
      c.weightx = 0.3;
      c.fill = GridBagConstraints.NONE;
      panel.add(cancel, c);
     
      JButton reset = new JButton("다시작성");
      reset.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
            // 모든 텍스트필드에 공백을 넣으면 될듯 ㅇㅇ..
            idField.setText("");
            pwField.setText("");
            confirmField.setText("");
            nameField.setText("");
            birthField.setText("");
            phoneField.setText("");
            addressField.setText("");
            mailField.setText("");

         }
      });
     
      c.gridx = 2;
      c.gridy = 9;
      c.weightx = 0.3;
      c.fill = GridBagConstraints.NONE;
      panel.add(reset, c);
  
     
      add(panel, BorderLayout.NORTH);
     
      setSize(500, 500);
      setDefaultCloseOperation(EXIT_ON_CLOSE);   // 여기 닫기버튼 질문 올려야함..
      setVisible(true);
   }

   public static void main(String[] args) {
      Exam2 t1 = new Exam2();
      t1.go();
   }
}

 

 

익명사용자 님이 2016년 5월 5일 질문

답변 달기

· 글에 소스 코드 보기 좋게 넣는 법
· 질문에 대해 추가적인 질문이나 의견이 있으면 답변이 아니라 댓글로 달아주시기 바랍니다.
표시할 이름 (옵션):
개인정보: 당신의 이메일은 이 알림을 보내는데만 사용됩니다.
스팸 차단 검사:
스팸 검사를 다시 받지 않으려면 로그인하거나 혹은 가입 하세요.
...