게시판 즐겨찾기
편집
드래그 앤 드롭으로
즐겨찾기 아이콘 위치 수정이 가능합니다.
엊그제 질문한 패널 문제 해결했습니당! + 질문 하나만 더 해도 될까요?
게시물ID : programmer_6120짧은주소 복사하기
작성자 :
추천 : 0
조회수 : 508회
댓글수 : 0개
등록시간 : 2014/10/21 23:38:10
옵션
  • 베스트금지
  • 본인삭제금지
package Project;
import javax.swing.*;
import javax.swing.text.MaskFormatter;
import Project.admin_Login.EventHandler;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.ParseException;
public class register {
 JFrame f = new JFrame();
 JLabel headLabel, nameLabel, idLabel, pwLabel1, pwLabel2, postLabel1, postLabel2, addressLabel, telLabel1, telLabel2, handLabel1, handLabel2, juminLabel1, juminLabel2;
 JTextField nameField, idField, postField1, postField2, addressField, telField1, telField2, handField1, handField2, juminField1, juminField2;
 TextField pwField1, pwField2;
 JButton checkButton, searchButton, joinButton, cancelButton;
 String []company ={"SKT","KTF","LG"};
 JComboBox brandComboBox;
 String []number ={"010","011","016","017","018","019"};
 JComboBox numberComboBox;
 JPanel topPanel, centerPanel, clPanel, crPanel, Panel, namePanel, pwPanel1, pwPanel2, addressPanel,
   idPanel, postPanel, telPanel, handPanel, juminPanel, checkPanel;
 JComboBox localComboBox;
 String []local ={"02","031", "032", "033", "041", "042", "043", "044", "051", "052", "053", "054", "055", "061", "062", "063", "064"};
 MaskFormatter fourmatter, sevenmatter, sixmatter = null;
 JFormattedTextField jumin, jumin2, tel, tel2, hand, hand2;
 
 
 register(String title){
  
  try {
   fourmatter = new MaskFormatter("####");
  } catch (ParseException e) {
   e.printStackTrace();
  }
  try{
   sixmatter = new MaskFormatter("######");
  }catch (ParseException e) {
   e.printStackTrace();
  }
  try{
   sevenmatter = new MaskFormatter("#######");
  }catch (ParseException e) {
   e.printStackTrace();
  }
  
  tel = new JFormattedTextField(fourmatter);
  tel2 = new JFormattedTextField(fourmatter);
  jumin = new JFormattedTextField(sixmatter);
  jumin2 = new JFormattedTextField(sevenmatter);
  hand = new JFormattedTextField(fourmatter);
  hand2 = new JFormattedTextField(fourmatter);
  
  headLabel = new JLabel("회원가입");
  nameLabel = new JLabel("이름 : ", JLabel.RIGHT);
  idLabel = new JLabel("아이디 : ", JLabel.RIGHT);
  pwLabel1 = new JLabel("패스워드 : ", JLabel.RIGHT);
  pwLabel2 = new JLabel("패스워드 확인: ",JLabel.RIGHT);
  juminLabel1 = new JLabel("주민번호 : ", JLabel.RIGHT);
  juminLabel2 = new JLabel("-");
  postLabel1 = new JLabel("우편번호 : ", JLabel.RIGHT);
  postLabel2 = new JLabel("-");
  addressLabel = new JLabel("주소 : ", JLabel.RIGHT);
  telLabel1 = new JLabel("전화번호 : ", JLabel.RIGHT);
  telLabel2 = new JLabel("-");
  handLabel1 = new JLabel("핸드폰 : ", JLabel.RIGHT);
  handLabel2 = new JLabel(" - ");
  nameField = new JTextField(10);
  idField = new JTextField(10);
  postField1 = new JTextField(3);
  postField2 = new JTextField(3);
  addressField = new JTextField(23);
  
  
  
  
  
  pwField1 = new TextField(10);
  pwField1.setEchoChar('*');
  pwField2 = new TextField(10);
  pwField2.setEchoChar('*');
  
  
   
  checkButton = new JButton("중복검사");
  searchButton = new JButton("주소검색");
  joinButton = new JButton("가입");
  cancelButton = new JButton("취소");
  
  idField.addActionListener(new EventHandler1());
  pwField1.addActionListener(new EventHandler1());
  pwField2.addActionListener(new EventHandler1());
  joinButton.addActionListener(new EventHandler1());
  checkButton.addActionListener(new EventHandler1());
  
  
  brandComboBox = new JComboBox(company);
  numberComboBox = new JComboBox(number);
  localComboBox = new JComboBox(local);
  
  topPanel = new JPanel(new GridLayout(1,1,5,5));
  centerPanel = new JPanel(new GridLayout(1,2));
  crPanel = new JPanel(new GridLayout(9,1));
  clPanel = new JPanel(new GridLayout(9,1));
  namePanel = new JPanel(new FlowLayout());
  namePanel.setLayout(new FlowLayout(FlowLayout.LEFT));
  pwPanel1 = new JPanel(new FlowLayout());
  pwPanel1.setLayout(new FlowLayout(FlowLayout.LEFT));
  pwPanel2 = new JPanel(new FlowLayout());
  pwPanel2.setLayout(new FlowLayout(FlowLayout.LEFT));
  idPanel = new JPanel(new FlowLayout());
  idPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
  postPanel = new JPanel(new FlowLayout());
  postPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
  addressPanel = new JPanel(new FlowLayout());
  addressPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
  telPanel = new JPanel(new FlowLayout());
  telPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
  handPanel = new JPanel(new FlowLayout());
  handPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
  juminPanel = new JPanel(new FlowLayout());
  juminPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
  checkPanel = new JPanel(new FlowLayout());
  
  Panel = new JPanel(new BorderLayout(0,20));
  
  topPanel.add(headLabel);
  idPanel.add(idField);
  idPanel.add(checkButton);
  postPanel.add(postField1);
  postPanel.add(postLabel2);
  postPanel.add(postField2);
  postPanel.add(searchButton);
  addressPanel.add(addressField);
  telPanel.add(localComboBox);
  telPanel.add(tel);
  telPanel.add(telLabel2);
  telPanel.add(tel2);
  handPanel.add(brandComboBox);
  handPanel.add(numberComboBox);
  handPanel.add(hand);
  handPanel.add(handLabel2);
  handPanel.add(hand2);
  juminPanel.add(jumin);
  juminPanel.add(juminLabel2);
  juminPanel.add(jumin2);
  pwPanel1.add(pwField1);
  pwPanel2.add(pwField2);
  namePanel.add(nameField);
  crPanel.add(namePanel);
  crPanel.add(juminPanel);
  crPanel.add(idPanel);
  crPanel.add(pwPanel1);
  crPanel.add(pwPanel2);
  crPanel.add(postPanel);
  crPanel.add(addressPanel);
  crPanel.add(telPanel);
  crPanel.add(handPanel);
  clPanel.add(nameLabel);
  clPanel.add(juminLabel1);
  clPanel.add(idLabel);
  clPanel.add(pwLabel1);
  clPanel.add(pwLabel2);
  clPanel.add(postLabel1);
  clPanel.add(addressLabel);
  clPanel.add(telLabel1);
  clPanel.add(handLabel1);
  
  centerPanel.add(crPanel);
  checkPanel.add(joinButton);
  checkPanel.add(cancelButton);
  Panel.add(checkPanel, "South");
  Panel.add(centerPanel, "Center");
  Panel.add(topPanel, "North");
  Panel.add(clPanel, "West");
  f.add(Panel);
  
  f.setResizable(false);
  f.setSize(350, 420);
  f.setVisible(true);
 }
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  register r = new register("회원가입");
 }
 class EventHandler1 implements ActionListener{
  public void actionPerformed(ActionEvent e) {
   if(e.getActionCommand().equals("가입")){
    String id = idField.getText();
    String pw = pwField1.getText();
    String name = nameField.getText();
    String post = postField1.getText() + postField2.getText();
    String address = addressField.getText();
    String tel3 =  localComboBox.getSelectedItem()+tel.getText() +tel2.getText();
    String hand3 = (String)brandComboBox.getSelectedItem()+numberComboBox.getSelectedItem()+ hand.getText() + hand2.getText();
    String jumin3 = jumin.getText()+ jumin2.getText();   
   
    Connection conn= null;
    Statement stmt= null;
    ResultSet rs= null;
    String query= null;
    int result;  
    try{
     query= "insert into user values ('" +name +"', '"+jumin3+"', '"+id+"', '"+pw+"', '"+post+"', '"+address+"', '"+tel3+"', '"+hand3+"')";
      
     //1. Loading driver
     Class.forName("com.mysql.jdbc.Driver");
     //2. Connecting to a DB
     conn= DriverManager.getConnection("jdbc:mysql://localhost:3306/project",
      "root", "12345678");
     //3. Creating statement object
     stmt= conn.createStatement();
     //B. For printing table data
     result= stmt.executeUpdate(query);
    
//     if(rs.next()){
//      System.out.println("중복된 아이디가 있습니다.");
//     }else{
//      System.out.println("이 아이디는 사용 가능합니다!");
//     }
    }catch(ClassNotFoundException e1){
     System.out.println("Couldn't load database driver: "+ e1.getMessage());;
    }catch(SQLException e2){
     System.out.println("SQLException caught: "+ e2.getMessage());
    }finally{
    //6. Closing db connection
     try{
      if(conn!= null)
       conn.close();
     }catch(SQLException ignored){}
    }
   }else if(e.getActionCommand().equals("중복검사")){
    Connection conn= null;
    Statement stmt= null;
    ResultSet rs= null;
    String query= null;
    String id = idField.getText();
    try{
     query= "select * from user where id='" + id  + "'";
     //1. Loading driver
     Class.forName("com.mysql.jdbc.Driver");
     //2. Connecting to a DB
     conn= DriverManager.getConnection("jdbc:mysql://localhost:3306/project",
       "root", "12345678");
     //3. Creating statement object
     stmt= conn.createStatement();
     //B. For printing table data
     rs= stmt.executeQuery(query);
     
     if(rs.next()){
      System.out.println("아이디가 중복됩니다. 다른 아이디를 사용해 주세요");      
     }else{
      System.out.println("사용 가능한 아이디 입니다.");
      
     }
    }catch(ClassNotFoundException e1){
      System.out.println("Couldn't load database driver: "+ e1.getMessage());;
    }catch(SQLException e2){
      System.out.println("SQLException caught: "+ e2.getMessage());
    }finally{
     //6. Closing db connection
     try{
      if(conn!= null)
       conn.close();
     }catch(SQLException ignored){}
    }
   }
  }
 }
}
1244.jpg
 
 
엊그제 질문한 패널에 공간이 많이 남는 다고 말씀드린 문제는 위치를 CENTER 에서 WEST로 잡는걸로 해결하였습니다. 감사합니다.
 
그런데 주민 등록 번호와 전화번호 핸드폰 번호에 각각 6자 7자 4자씩의 제한과 문자 입력 방지를 위해
 
MaskForMatter와 JFormattedTextField 를 이용하여 해결하였는데요 패널을 FlowLayout 으로 잡아놔서인지 JFormattedTextField 의 입력 공간이 보기와 같이 너무 작아져서요... 한두자만 보이고 글자가 안보이는 문제가 생겼습니다 ㅜㅜ.
그래서 BorderLayout 으로  잡아봣으나 주민번호 : [텍스트 필드] - [텍스트 필드] 에서 주민번호: [텍스트필드] 로 변하며 앞쪽의 텍스트 필드와 -(바)가 사라지는 문제가 생겼습니다.
 
 
혹시 어떤식으로 해결해야 하는지 방향만이라도 잡아 주실 수 있나요?
 

전체 추천리스트 보기
새로운 댓글이 없습니다.
새로운 댓글 확인하기
글쓰기
◀뒤로가기
PC버전
맨위로▲
공지 운영 자료창고 청소년보호