안녕하세요,
[Java/미니프로젝트] 영단어장 만들기 의 소스 분석 포스팅 하려 합니다.
패키지를 총 3개로 나누어서 했으므로, 소스 분석도 3개로 나누어 하려 합니다.
처음 시작할 것은 로그인프레임 입니다.
⭐POINT⭐
public class LogInFrame extends JFrame {
// 회원가입 및 id/pw 찾기 다이얼로그 객체 생성
private JoinDialog join;
private SearchIdPw seaIdPw;
// 네이밍 배열
private String [] nameStr = {"ID", "PW", "LogIn", "search for id/pw", "Join"};
// ID 및 PW Label 배열
private JLabel [] logLabel = new JLabel[2];
// BUTTON 배열
private JButton [] logBtn = new JButton[3];
// ID 및 PW 입력 필드
private JTextField idTxt = new JTextField(10);
private JPasswordField pwTxt = new JPasswordField(10);
// 지정 FONT
private Font sanserifNormal = new Font("SanSerif", Font.BOLD, 40);
private Font sanserifsmall = new Font("SanSerif", Font.BOLD, 25);
private Color darkGray = new Color(127, 127, 127);
private Color lightGray = new Color(242, 242, 242);
// 상단 중앙부분 이미지 (아직)
// private ImageIcon bulbImage = new ImageIcon("images/bulb.png");
// private JLabel bulbImgLabel = new JLabel(bulbImage);
// 로그인 리스너
private LogInListener listener;
private ManagerFrame managerFrame;
private MemberFrame memberFrame;
public LogInFrame() {
setTitle("MiniWord_LogIn");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c = getContentPane();
c.setLayout(null);
c.setBackground(lightGray);
managerFrame = new ManagerFrame(this);
memberFrame = new MemberFrame(this);
listener = new LogInListener(this, managerFrame, memberFrame);
// 회원가입 및 id/pw 찾기 다이얼로그 매개변수 입력.
join = new JoinDialog(this, "Join");
seaIdPw = new SearchIdPw(this, "search for id/password");
// 네이밍 및 폰트 설정
for(int i=0; i<nameStr.length; i++) {
if(i<2) { // ID 및 PW 라벨 설정 & 컨테이너 부착
idTxt.setFont(new Font("SanSerif", Font.BOLD, 30));
pwTxt.setFont(new Font("SanSerif", Font.BOLD, 30));
logLabel[i] = new JLabel(nameStr[i]);
logLabel[i].setFont(sanserifNormal);
logLabel[i].setForeground(darkGray);
if(i==0) {
logLabel[i].setBounds(40, 155, 40, 40);
idTxt.setBounds(115, 155, 210, 40);
idTxt.addKeyListener(listener);
c.add(logLabel[i]);
c.add(idTxt);
} else if(i==1) {
logLabel[i].setBounds(25, 220, 90, 40);
pwTxt.setBounds(115, 225, 210, 40);
pwTxt.addKeyListener(listener);
c.add(logLabel[i]);
c.add(pwTxt);
}
} else if(i>1) { // BUTTON 설정 & 컨테이너 부착
int index = 0;
logBtn[index] = new JButton(nameStr[i]);
logBtn[index].setFont(sanserifsmall);
logBtn[index].setBackground(darkGray);
logBtn[index].setForeground(Color.white);
if(i==2) {
logBtn[index].setBounds(350, 170, 110, 80); // 로그인 버튼
logBtn[index].addActionListener(listener);
} else if(i==3) {
logBtn[index].setBounds(30, 300, 230, 30); // ID 및 PW 찾기 버튼
logBtn[index].addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
seaIdPw.setVisible(true);
}
});
} else if(i==4) {
logBtn[index].setBounds(300, 300, 150, 30); // 회원가입 버튼
logBtn[index].addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
join.setVisible(true);
}
});
}
c.add(logBtn[index]);
index++;
}
}
setLocation(700, 300);
setSize(500, 500);
setVisible(true);
setResizable(false);
}
public ManagerFrame getManagerFrame() {
return managerFrame;
}
public MemberFrame getMemberFrame() {
return memberFrame;
}
public JTextField getIdTxt() {
return idTxt;
}
public JPasswordField getPwTxt() {
return pwTxt;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new LogInFrame();
}
}
배치할 때 반복문을 사용한 이유는, 사실 단순 노가다 작업이 싫은 것이 가장 컸다. 하지만 이것이 추후 유지보수에 좋지 않다는 것은... 이미 프로젝트가 반 이상이 진행된 후 였다. 미리 물어보고 피드백을 빨리 받았다면 참 좋았겠지만.. 혼자 부딪혀보지 않는 이상 얻는게 없다는 마인드인데다, 맨땅에 해딩하면 실제로 얻는게 많아 웬만하면 질문하지 않는 태도를 일관성 있게 유지했다.
⭐POINT⭐
public class LogInListener extends KeyAdapter implements ActionListener {
private DB repDB = new DB();
private ManagerFrame managerFrame;
private MemberFrame memberFrame;
private LogInFrame logInFrame;
private WordAdminPanel adminPanel;
private RequestAdminPanel requestPanel;
private String resId;
private String resPw;
private JTextField idTxt;
private JPasswordField pwTxt;
private String name;
private String grade;
private JLabel nameLabel;
private JTextField gradeTxt;
public LogInListener(LogInFrame logInFrame, ManagerFrame managerFrame, MemberFrame memberFrame) {
this.logInFrame = logInFrame;
this.idTxt = logInFrame.getIdTxt();
this.pwTxt = logInFrame.getPwTxt();
this.managerFrame = managerFrame;
this.memberFrame = memberFrame;
this.nameLabel = memberFrame.getNameLabel();
this.gradeTxt = memberFrame.getGradeTxt();
this.adminPanel = managerFrame.getAdminPanel();
this.requestPanel = managerFrame.getRequestPanel();
}
public void keyPressed(KeyEvent e) {
Object obj = e.getSource();
if(e.getKeyCode() == KeyEvent.VK_ENTER) {
try {
resId = repDB.memberSurf("ID", idTxt.getText());
resPw = repDB.memberSurf("PW", pwTxt.getText());
if((idTxt.getText()).equals("")|| (pwTxt.getText()).equals("")) {
JOptionPane.showMessageDialog(null, "입력된 것이 없습니다.\nID 및 PW를 입력해주세요.", "Warning", JOptionPane.WARNING_MESSAGE);
}
else {
if(resId == null || resPw == null) {
JOptionPane.showMessageDialog(null, "일치하는 회원정보가 없습니다.", "LogIn Fail ;(", JOptionPane.WARNING_MESSAGE);
idTxt.setText("");
pwTxt.setText("");
} else {
// 로그인 후 화면 연결
if(idTxt.getText().equals("ManagerJang")) {
JOptionPane.showMessageDialog(null, "Hello, Manager !", "Manager Mode", JOptionPane.PLAIN_MESSAGE);
logInFrame.setVisible(false);
managerFrame.setVisible(true);
// 요청사항 리스트 다시 불러오기
repDB.selectAllrequest(requestPanel.getWordList().getModel(), requestPanel.getWordList().getTable());
} else {
name = repDB.infoSurfID("name", "id", idTxt.getText());
grade = repDB.infoSurfID("grade", "id", idTxt.getText());
JOptionPane.showMessageDialog(null, name + "님 환영합니다 :)", "LogIn Success !!!", JOptionPane.PLAIN_MESSAGE);
nameLabel.setText(name);
gradeTxt.setText(grade);
logInFrame.setVisible(false);
memberFrame.setVisible(true);
}
idTxt.setText("");
pwTxt.setText("");
}
}
} catch (SQLException e1) {
e1.printStackTrace();
}
}
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
try {
resId = repDB.memberSurf("ID", idTxt.getText());
resPw = repDB.memberSurf("PW", pwTxt.getText());
if((idTxt.getText()).equals("")|| (pwTxt.getText()).equals("")) {
JOptionPane.showMessageDialog(null, "입력된 것이 없습니다.\nID 및 PW를 입력해주세요.", "Warning", JOptionPane.WARNING_MESSAGE);
}
else {
if(resId == null || resPw == null) {
JOptionPane.showMessageDialog(null, "일치하는 회원정보가 없습니다.\n재시도 또는 회원가입이 필요합니다.", "Warning", JOptionPane.WARNING_MESSAGE);
idTxt.setText("");
pwTxt.setText("");
} else {
// 로그인 후 화면 연결
if(idTxt.getText().equals("ManagerJang")) {
JOptionPane.showMessageDialog(null, "Hello, Manager !", "Manager Mode", JOptionPane.PLAIN_MESSAGE);
logInFrame.setVisible(false);
managerFrame.setVisible(true);
// 요청사항 리스트 다시 불러오기
repDB.selectAllrequest(requestPanel.getWordList().getModel(), requestPanel.getWordList().getTable());
} else {
name = repDB.infoSurfID("name", "id", idTxt.getText());
grade = repDB.infoSurfID("grade", "id", idTxt.getText());
JOptionPane.showMessageDialog(null, name + "님 환영합니다 :)", "LogIn Success !!!", JOptionPane.PLAIN_MESSAGE);
nameLabel.setText(name);
gradeTxt.setText(grade);
logInFrame.setVisible(false);
memberFrame.setVisible(true);
}
idTxt.setText("");
pwTxt.setText("");
}
}
} catch (SQLException e1) {
e1.printStackTrace();
}
}
}
⭐POINT⭐
public class JoinDialog extends JDialog {
// 위치 설정 위한 변수설정
private int y=30, widthNx=170;
private int txtHeight=32, pwTxtX=170, pwTxtPlusX=73, pwTxtTmpX = 0;
// 네이밍 배열
private String [] nameStr = {"*Name", "*ID", "*PW", " *└check", "Phone"};
private String [] noticeStr = {"", "", "영문+특수문자+숫자 8자↑", ">> 비밀번호 확인", "* 필수 작성"};
// 라벨 배열
private JLabel [] logLabel = new JLabel[5];
// 텍스트필드 배열
private JTextField [] joinTxt = new JTextField[5];
// PW 필드 배열
private JPasswordField [] pwJoinTxt = new JPasswordField[2];
// 버튼
private JButton dupBtn;
private JButton idDupBtn;
private JButton joinBtn;
private JButton resetBtn;
// 안내문구 라벨
private JLabel [] noticeLabel = new JLabel[5];
// 지정 FONT
private Font sanserifNormal = new Font("SanSerif", Font.BOLD, 25);
private Font sanserifsmall = new Font("SanSerif", Font.BOLD, 15);
private Font sanserifnotice = new Font("SanSerif", Font.ITALIC, 12);
private Color darkGray = new Color(127, 127, 127);
private Color lightGray = new Color(242, 242, 242);
private Color navy = new Color(0, 32, 96);
// 리스너 객체 생성
private ButtonClickListener listener;
private PwListener pwListener = new PwListener(pwJoinTxt, joinTxt, noticeLabel);
private PhoneListener phoneListener = new PhoneListener(joinTxt);
public JoinDialog (JFrame frame, String title) {
super(frame, title, true);
Container c = getContentPane();
c.setLayout(null);
c.setBackground(lightGray);
dupBtn = new JButton("중복확인");
dupBtn.setFont(sanserifsmall);
dupBtn.setForeground(Color.WHITE);
dupBtn.setBackground(darkGray);
dupBtn.setBounds(410, 100, 150, 30);
idDupBtn = new JButton("ID 중복확인");
idDupBtn.setFont(sanserifsmall);
idDupBtn.setForeground(Color.WHITE);
idDupBtn.setBackground(darkGray);
idDupBtn.setBounds(410, 170, 150, 30);
joinBtn = new JButton("JOIN");
joinBtn.setFont(sanserifsmall);
joinBtn.setBackground(navy);
joinBtn.setForeground(Color.WHITE);
joinBtn.setBounds(225, 460, 150, 30);
resetBtn = new JButton("reset");
resetBtn.setFont(new Font("SanSerif", Font.BOLD, 12));
resetBtn.setBackground(navy);
resetBtn.setForeground(Color.WHITE);
resetBtn.setBounds(263, 495, 72, 20);
listener = new ButtonClickListener(this);
dupBtn.addActionListener(listener);
idDupBtn.addActionListener(listener);
joinBtn.addActionListener(listener);
resetBtn.addActionListener(listener);
for(int i=0; i<nameStr.length; i++) {
logLabel[i] = new JLabel(nameStr[i]);
logLabel[i].setFont(sanserifNormal);
logLabel[i].setForeground(darkGray);
logLabel[i].setBounds(0, y += 70, widthNx, 25);
logLabel[i].setHorizontalAlignment(JLabel.CENTER);
c.add(logLabel[i]);
joinTxt[i] = new JTextField(10);
joinTxt[i].setFont(sanserifNormal);
joinTxt[i].setForeground(darkGray);
if(i<2) { // Name 및 ID 텍스트필드
joinTxt[i].setBounds(widthNx, y, widthNx+50, txtHeight);
joinTxt[i].addKeyListener(listener); // 리스너
} else if(i==2||i==3) {
// Phone 1~2 텍스트필드
joinTxt[i].setBounds(pwTxtX += pwTxtTmpX, 380, 68, txtHeight);
joinTxt[i].setHorizontalAlignment(JTextField.CENTER);
pwTxtTmpX += pwTxtPlusX;
// Pw & check 패스워드필드
pwJoinTxt[i-2] = new JPasswordField(10);
pwJoinTxt[i-2].setFont(sanserifNormal);
pwJoinTxt[i-2].setForeground(darkGray);
pwJoinTxt[i-2].setBounds(170, y, widthNx+50, txtHeight);
pwJoinTxt[i-2].addKeyListener(pwListener);
c.add(pwJoinTxt[i-2]);
} else if(i==4) {
// Phone 3번째 텍스트 필드
joinTxt[i].setBounds(pwTxtX += pwTxtPlusX, 380, 68, txtHeight);
joinTxt[i].setHorizontalAlignment(JTextField.CENTER);
}
c.add(joinTxt[i]);
}
// 재사용 위해 y 값 초기화
y = 60;
// 안내라벨 배열
for(int i=0; i<noticeStr.length; i++) {
noticeLabel[i] = new JLabel(noticeStr[i]);
noticeLabel[i].setFont(sanserifnotice);
noticeLabel[i].setForeground(darkGray);
if(i<4) {
if(i<2) {
// name 및 id 중복확인 텍스트라벨
noticeLabel[i].setBounds((widthNx*2)+70, y+=70, 150, 20);
// noticeLabel[i].setVisible(false);
} else {
// 비밀번호 안내 텍스트 라벨
noticeLabel[i].setBounds((widthNx*2)+70, y+=58, 150, 20);
noticeLabel[i].addKeyListener(pwListener);
}
}
if(i==4) {
// 필수작성 텍스트라벨
noticeLabel[i].setFont(sanserifnotice);
noticeLabel[i].setForeground(darkGray);
noticeLabel[i].setBounds(270, 430, 100, 20);
}
c.add(noticeLabel[i]);
}
// phone 텍스트필드 설정 및 리스너 붙이기
joinTxt[2].setText("010");
joinTxt[2].setEditable(false);
joinTxt[3].addKeyListener(phoneListener);
joinTxt[4].addKeyListener(phoneListener);
c.add(dupBtn);
c.add(idDupBtn);
c.add(joinBtn);
c.add(resetBtn);
setLocation(700, 300);
setSize(600, 600);
setResizable(true);
}
public JTextField[] getJoinTxt() {
return joinTxt;
}
public JPasswordField[] getPwJoinTxt() {
return pwJoinTxt;
}
public void setPwJoinTxt(JPasswordField[] pwJoinTxt) {
this.pwJoinTxt = pwJoinTxt;
}
public JLabel[] getNoticeLabel() {
return noticeLabel;
}
public JButton getDupBtn() {
return dupBtn;
}
public JButton getIdDupBtn() {
return idDupBtn;
}
}
회원가입 화면의 배치는 원래 전부 반복문이었으나, 추후 해당 파일에 수정할 부분이 있어 수정하다가 일부만 반복문 없이 배치하는 것으로 수정했다.
⭐POINT⭐
public class ButtonClickListener extends KeyAdapter implements ActionListener{
private DB repDB = new DB();
private Dialog JoinDialog;
private JoinDialog joinDialog;
private String res;
private int yesNo;
private JLabel[] noticeLabel;
private JTextField [] joinTxt;
private JPasswordField [] pwJoinTxt;
private JButton dupBtn;
private JButton idDupBtn;
private boolean flag;
public ButtonClickListener(JoinDialog joinDialog){
this.joinDialog = joinDialog;
this.joinTxt = joinDialog.getJoinTxt();
this.pwJoinTxt = joinDialog.getPwJoinTxt();
this.noticeLabel = joinDialog.getNoticeLabel();
this.dupBtn = joinDialog.getDupBtn();
this.idDupBtn = joinDialog.getIdDupBtn();
}
// key 리스너(적용항목: id 및 name 라벨)
public void keyPressed(KeyEvent e) {
Object obj = e.getSource();
if(e.getKeyCode() == KeyEvent.VK_ENTER) {
if(obj == joinTxt[0]) {
try {
if(joinTxt[0].getText().equals("")) {
JOptionPane.showMessageDialog(null, "입력된 것이 없습니다.\n원하시는 name을 입력해주세요.", "Warning", JOptionPane.WARNING_MESSAGE);
joinTxt[0].setText("");
} else {
if(notSpace(joinTxt[0].getText())) {
res = repDB.memberSurf("name", joinTxt[0].getText());
if(res != null) {
JOptionPane.showMessageDialog(null, "이미 있는 name 입니다.\n 다른 name을 입력해주세요.", "Warning", JOptionPane.WARNING_MESSAGE);
joinTxt[0].setText("");
} else if (res == null){
yesNo = JOptionPane.showConfirmDialog(null, "사용 가능한 name 입니다.\n사용하시겠습니까?", "Correct", JOptionPane.YES_NO_OPTION);
if(yesNo == JOptionPane.NO_OPTION) {
joinTxt[0].setText("");
} else {
noticeLabel[0].setText("중복확인 완료");
joinTxt[0].setEditable(false);
dupBtn.setEnabled(false);
}
}
} else {
JOptionPane.showMessageDialog(null, "공백은 사용불가 입니다.", "Warning", JOptionPane.WARNING_MESSAGE);
joinTxt[0].setText("");
}
}
} catch (SQLException e1) {
e1.printStackTrace();
}
} else {
try {
if(joinTxt[1].getText().equals("")) {
JOptionPane.showMessageDialog(null, "입력된 것이 없습니다.\n원하시는 id를 입력해주세요.", "Warning", JOptionPane.WARNING_MESSAGE);
joinTxt[1].setText("");
} else {
if(onlyEngNum(joinTxt[1].getText())) {
res = repDB.memberSurf("id", joinTxt[1].getText());
if(res != null) {
JOptionPane.showMessageDialog(null, "이미 있는 id 입니다.\n 다른 id를 입력해주세요.", "Warning", JOptionPane.WARNING_MESSAGE);
joinTxt[1].setText("");
} else if (res == null){
yesNo = JOptionPane.showConfirmDialog(null, "사용 가능한 id 입니다\n사용하시겠습니까?", "Correct", JOptionPane.YES_NO_OPTION);
if(yesNo == JOptionPane.NO_OPTION) {
joinTxt[1].setText("");
} else {
noticeLabel[1].setText("ID 중복확인 완료");
joinTxt[1].setEditable(false);
idDupBtn.setEnabled(false);
}
}
} else {
JOptionPane.showMessageDialog(null, "영문과 숫자만 입력해주세요.", "Warning", JOptionPane.WARNING_MESSAGE);
joinTxt[1].setText("");
}
}
} catch (SQLException e1) {
e1.printStackTrace();
}
}
}
}
// action 리스너(적용항목: id 및 name 중복확인 버튼, JOIN 버튼)
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
JButton btn = (JButton) e.getSource();
if(e.getActionCommand()=="중복확인") { // name
try {
if(this.joinTxt[0].getText().equals("")) {
JOptionPane.showMessageDialog(null, "입력된 것이 없습니다.\n원하시는 name을 입력해주세요.", "Warning", JOptionPane.WARNING_MESSAGE);
this.joinTxt[0].setText("");
} else {
if(notSpace(joinTxt[0].getText())) {
res = repDB.memberSurf("name", this.joinTxt[0].getText());
if(res != null) {
JOptionPane.showMessageDialog(null, "이미 있는 name 입니다.\n 다른 name을 입력해주세요.", "Warning", JOptionPane.WARNING_MESSAGE);
this.joinTxt[0].setText("");
} else if (res == null){
yesNo = JOptionPane.showConfirmDialog(null, "사용 가능한 name 입니다.\n사용하시겠습니까?", "Correct", JOptionPane.YES_NO_OPTION);
if(yesNo == JOptionPane.NO_OPTION) {
this.joinTxt[0].setText("");
} else {
noticeLabel[0].setText("중복확인 완료");
joinTxt[0].setEditable(false);
dupBtn.setEnabled(false);
}
}
} else {
JOptionPane.showMessageDialog(null, "공백은 사용불가 입니다.", "Warning", JOptionPane.WARNING_MESSAGE);
joinTxt[0].setText("");
}
}
} catch (SQLException e1) {
e1.printStackTrace();
}
} else if(e.getActionCommand()=="ID 중복확인") { // id
try {
if(this.joinTxt[1].getText().equals("")) {
JOptionPane.showMessageDialog(null, "입력된 것이 없습니다.\n원하시는 id를 입력해주세요.", "Warning", JOptionPane.WARNING_MESSAGE);
this.joinTxt[1].setText("");
} else {
if(onlyEngNum(joinTxt[1].getText())) {
res = repDB.memberSurf("name", this.joinTxt[1].getText());
if(res != null) {
JOptionPane.showMessageDialog(null, "이미 있는 id 입니다.\n 다른 id를 입력해주세요.", "Warning", JOptionPane.WARNING_MESSAGE);
this.joinTxt[1].setText("");
} else if (res == null){
yesNo = JOptionPane.showConfirmDialog(null, "사용 가능한 id 입니다.\n사용하시겠습니까?", "Correct", JOptionPane.YES_NO_OPTION);
if(yesNo == JOptionPane.NO_OPTION) {
this.joinTxt[1].setText("");
} else {
noticeLabel[1].setText("ID 중복확인 완료");
joinTxt[1].setEditable(false);
idDupBtn.setEnabled(false);
}
}
} else {
JOptionPane.showMessageDialog(null, "영문과 숫자만 입력해주세요.", "Warning", JOptionPane.WARNING_MESSAGE);
joinTxt[1].setText("");
}
}
} catch (SQLException e1) {
e1.printStackTrace();
}
} else if(e.getActionCommand() == "JOIN") {
if(essentialValue()==true) {
yesNo = JOptionPane.showConfirmDialog(null, "입력한 정보로 회원가입 하시겠습니까?", "", JOptionPane.YES_NO_OPTION);
if(yesNo == JOptionPane.YES_OPTION) {
repDB.firstValue(joinTxt, pwJoinTxt);
JOptionPane.showMessageDialog(null, "회원가입이 완료 되었습니다.", "Congratulation!!!", JOptionPane.PLAIN_MESSAGE);
joinDialog.setVisible(false);
reset();
}
}
} else if(e.getActionCommand() == "reset") {
reset();
}
}
// reset 버튼 클릭시, 입력칸 초기화 메소드
public void reset() {
joinTxt[0].setEditable(true);
joinTxt[0].setText("");
joinTxt[1].setEditable(true);
joinTxt[1].setText("");
noticeLabel[0].setText("");
noticeLabel[1].setText("");
pwJoinTxt[0].setText("");
pwJoinTxt[1].setText("");
noticeLabel[3].setText(">> 비밀번호 확인");
noticeLabel[3].setForeground(new Color(127, 127, 127));
noticeLabel[3].setForeground(new Color(127, 127, 127));
noticeLabel[3].setFont(new Font("SanSerif", Font.ITALIC, 12));
joinTxt[3].setText("");
joinTxt[4].setText("");
dupBtn.setEnabled(true);
idDupBtn.setEnabled(true);
}
// 올바른 JOIN을 위한 필터
public boolean essentialValue() {
boolean judge = true;
String pw = new String(pwJoinTxt[0].getPassword());
String pwCh = new String(pwJoinTxt[1].getPassword());
Pattern pwPat = Pattern.compile("^(?=.*[a-zA-Z])(?=.*\\d)(?=.*\\W).{8,20}$");
Matcher pwMat = pwPat.matcher(pw);
Matcher pwCheckMat = pwPat.matcher(pwCh);
if(!(noticeLabel[0].getText()).equals("") && !(noticeLabel[1].getText()).equals("")) {
if(!(noticeLabel[3].getText()).equals("비밀번호 일치")) {
JOptionPane.showMessageDialog(null, "비밀번호 재확인이 되지 않았습니다.\n다시 시도해주세요.", "비밀번호 재확인 필요", JOptionPane.WARNING_MESSAGE);
judge = false;
} else if(!pwMat.find() || !pwCheckMat.find()){
JOptionPane.showMessageDialog(null, "PW는 영문+특수문자+숫자 8~20자로 구성되어야 합니다.\n다시 입력해주세요.", "WARN", JOptionPane.WARNING_MESSAGE);
pwJoinTxt[0].setText("");
pwJoinTxt[1].setText("");
judge = false;
}
} else {
JOptionPane.showMessageDialog(null, "중복확인이 되지 않았습니다.\n다시 시도해주세요.", "중복확인 필요", JOptionPane.WARNING_MESSAGE);
judge = false;
}
return judge;
}
// 영어랑 숫자만 받기
public boolean onlyEngNum(String id) {
char rep;
for(int i=0; i<id.length(); i++) {
rep = id.charAt(i);
if(rep >= 0x61 && rep <=0x7A) { // 영어 소문자
flag = true;
} else if (rep >=0x41 && rep <=0x5A) { // 영어 소문자
flag = true;
} else if (rep >= 0x30 && rep <= 0x39) { // 숫자
flag = true;
} else { // 그 외
flag = false;
}
}
return flag;
}
// 공백 제외한 값만 받기
public boolean notSpace(String name) {
char rep;
for(int i=0; i<name.length(); i++) {
rep = name.charAt(i);
if(rep == 0x20) {
flag = false;
} else {
flag = true;
}
}
return flag;
}
}
⭐POINT⭐
public class PwListener extends KeyAdapter {
private DB repDB = new DB();
private JPasswordField [] repPw;
private JTextField [] repTxt;
private JLabel[] noticeLabel;
private int yesNo;
private String repPwStr;
private String repCheStr;
private Color darkGray = new Color(127, 127, 127);
public PwListener(JPasswordField [] repPw, JTextField [] repTxt, JLabel[] noticeLabel){
this.repPw = repPw;
this.repTxt = repTxt;
this.noticeLabel = noticeLabel;
}
public void keyReleased(KeyEvent e) {
repPwStr = new String(repPw[0].getPassword());
repCheStr = new String(repPw[1].getPassword());
if(repPwStr.equals("") && repCheStr.equals("")) {
noticeLabel[3].setText(">> 비밀번호 확인");
noticeLabel[3].setForeground(darkGray);
} else {
if(repPwStr.equals(repCheStr)) {
noticeLabel[3].setText("비밀번호 일치");
noticeLabel[3].setForeground(Color.GREEN);
} else {
noticeLabel[3].setText("비밀번호 불일치");
noticeLabel[3].setForeground(Color.RED);
}
}
}
}
public class PhoneListener extends KeyAdapter {
private DB repDB = new DB();
private JTextField [] repTxt;
private int yesNo;
public PhoneListener(JTextField [] repTxt) {
this.repTxt = repTxt;
}
public boolean checkNum(String txt) {
char chRepTxt;
boolean judge = true;
for (int i=0; i<txt.length(); i++) {
chRepTxt = txt.charAt(i);
if(!(chRepTxt >= 0x30 && chRepTxt <= 0x39)) {
judge = false;
break;
} else {
judge = true;
}
}
return judge;
}
public boolean checkNumLength(String txt) {
boolean judge = true;
if (txt.length() != 4 || txt.equals("")) {
judge = false;
}
return judge;
}
public void keyPressed(KeyEvent e) {
Object obj = e.getSource();
if(e.getKeyCode() == KeyEvent.VK_TAB || e.getKeyCode() == KeyEvent.VK_ENTER) {
String phoneTxt1 = repTxt[3].getText();
String phoneTxt2 = repTxt[4].getText();
if(obj == repTxt[3] && (checkNum(phoneTxt1) == false || checkNumLength(phoneTxt1) == false)) {
JOptionPane.showMessageDialog(null, "숫자 4개를 입력해주세요.", "WARN", JOptionPane.WARNING_MESSAGE);
repTxt[3].setText("");
}
if(obj == repTxt[4]) {
if(checkNum(phoneTxt2) == false || checkNumLength(phoneTxt2) == false) {
JOptionPane.showMessageDialog(null, "숫자 4개를 입력해주세요.", "WARN", JOptionPane.WARNING_MESSAGE);
repTxt[4].setText("");
} else{
yesNo = JOptionPane.showConfirmDialog(null, "입력하신 번호를 사용하시겠습니까?", "OK", JOptionPane.YES_NO_OPTION);
if(yesNo == JOptionPane.NO_OPTION) {
repTxt[3].setText("");
repTxt[4].setText("");
}
}
}
}
}
}
⭐POINT⭐
public class SearchIdPw extends JDialog {
// 리스너 객체 생성
private SearchIdPwListener listener;
private SearchInfoListener infoListener;
// 네이밍 문자 배열
private String[] nameStr = {" * 찾는 정보 선택: ", " - name? ", " - ID? ", "-",/**/ " PW ", " ID ",/**/ "search", "close"};
// nameStr[0]~[3]
private JLabel [] QnALabel = new JLabel[4];
// nameStr[4]~[5] 및 버튼 그룹
private JRadioButton [] selIdPwBtn = new JRadioButton[2];
private ButtonGroup radioBtnGroup = new ButtonGroup();
// name 및 id 입력할 텍스트필드 배열
private JTextField [] wantTxt = new JTextField[2];
// nameStr[6]~[7]
private JButton [] btn = new JButton[2];
// 위치 조정 위한 변수 (추후.. 보완 해보기...)
private int fixX = 40, laY = 0, fixW = 150, fixH = 20;
private int fixBtnX = 210, fixBtnY = 40, fixBtnW = 80, fixBtnH = 20;
private int fixTxtX = 130, fixTxtY = 83, fixTxtW = 150, fixTxtH = 20;
private int chX = 90, chY = 40, chTxtY = 39, ch = 43;
private int index = 0;
// 지정 FONT
private Font sanserifNormal = new Font("SanSerif", Font.BOLD, 17);
private Font sanserifsmall = new Font("SanSerif", Font.BOLD, 15);
private Font sanserifnotice = new Font("SanSerif", Font.ITALIC, 10);
private Color darkGray = new Color(127, 127, 127);
private Color lightGray = new Color(242, 242, 242);
private Color navy = new Color(0, 32, 96);
public SearchIdPw(JFrame frame, String title) {
super(frame, title, true);
Container c = getContentPane();
c.setLayout(null);
c.setBackground(lightGray);
listener = new SearchIdPwListener(selIdPwBtn, QnALabel, wantTxt, btn, this);
infoListener = new SearchInfoListener(QnALabel, wantTxt);
for(int i=0; i<nameStr.length; i++) {
// *name 라벨 및 텍스트필드 y값 변경 필요 (리스너를 통해 위치변경 필요 << id visible이 true가 될 경우 위로 조금 움직여줘야 함.)
// *id 라벨 및 텍스트필드는 setVisible(false)로 설정 필요 (리스너를 통해 true로 변경 필요)
if(i<4) {
QnALabel[i] = new JLabel(nameStr[i]);
QnALabel[i].setFont(sanserifNormal);
QnALabel[i].setForeground(darkGray);
if(i==3) {
// 찾는 값 나타내는 라벨 (리스너 부착후 리스너 내에서 setText 하여 내용변경 필요!)
QnALabel[i].setBounds(40, laY += chY, 360, fixH);
QnALabel[i].setHorizontalAlignment(JLabel.CENTER);
} else {
// Q 라벨(찾는정보?, name?, id?)
QnALabel[i].setBounds(fixX, laY += chY, fixW, fixH);
// name 라벨 위치: 40, 80, 150, 20
// id 라벨 위치: 40, 120, 150, 20
}
c.add(QnALabel[i]);
} else if(i<6) {
// id 및 pw 라디오버튼
selIdPwBtn[index] = new JRadioButton(nameStr[i]);
selIdPwBtn[index].setFont(sanserifNormal);
selIdPwBtn[index].setForeground(darkGray);
selIdPwBtn[index].addActionListener(infoListener);
// name 및 id 입력 텍스트필드
wantTxt[index] = new JTextField(10);
wantTxt[index].setFont(sanserifsmall);
wantTxt[index].setForeground(darkGray);
// 위치 설정
if(i==4) {
selIdPwBtn[index].setBounds(fixBtnX, fixBtnY, fixBtnW, fixBtnH);
selIdPwBtn[index].setSelected(true);
// name 텍스트필드 ( ★★★★★ 추후 리스너로 위치 조정 필요 ★★★★★ )
wantTxt[index].setBounds(fixTxtX, fixTxtY, fixTxtW, fixTxtH); // 130, 83, 150, 20
} else {
selIdPwBtn[index].setBounds(fixBtnX += chX, fixBtnY, fixBtnW, fixBtnH);
// id 텍스트필드 ( ★★★★★ 추후 리스너로 visible(false) 등 필요 ★★★★★ )
wantTxt[index].setBounds(fixTxtX, fixTxtY += chTxtY, fixTxtW, fixTxtH); // 130, 161, 150, 20
}
radioBtnGroup.add(selIdPwBtn[index]);
c.add(selIdPwBtn[index]);
c.add(wantTxt[index]);
index++;
// 초기화
if(index > 1) {
index = 0;
fixTxtY = 83;
}
} else {
// search 및 close 버튼
btn[index] = new JButton(nameStr[i]);
btn[index].setFont(sanserifsmall);
btn[index].setForeground(Color.white);
btn[index].setBackground(darkGray);
btn[index].addActionListener(listener);
if(i==7) {
// close btn
btn[index].setBounds(180, laY += chY, fixBtnW, fixBtnH);
} else {
// search btn
btn[index].setBounds(fixBtnX, 88, 90, 45);
}
c.add(btn[index]);
}
}
setLocation(725, 400);
setSize(450, 300);
setResizable(false);
}
}
// 한 클래스 내에 ActionListener 두 개 사용 안되서 하나 만듦..
class SearchInfoListener implements ActionListener{
private JLabel [] QnALabel;
private JTextField [] wantTxt;
public SearchInfoListener(JLabel [] QnALabel, JTextField [] wantTxt) {
this.QnALabel = QnALabel;
this.wantTxt = wantTxt;
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
JRadioButton radioBtn = (JRadioButton) e.getSource();
if(radioBtn.isSelected() == true) {
if((radioBtn.getText()).equals(" ID ")) {
QnALabel[1].setBounds(40, 100, 150, 20);
wantTxt[0].setBounds(130, 104, 150, 20);
QnALabel[2].setVisible(false);
wantTxt[1].setVisible(false);
} else {
QnALabel[1].setBounds(40, 80, 150, 20);
wantTxt[0].setBounds(130, 83, 150, 20);
QnALabel[2].setVisible(true);
wantTxt[1].setVisible(true);
}
}
}
}
⭐POINT⭐
public class SearchIdPwListener extends KeyAdapter implements ActionListener {
private DB repDB = new DB();
private JRadioButton [] selIdPwBtn;
private JLabel [] QnALabel;
private JTextField [] wantTxt;
private JButton [] btn;
private SearchIdPw searchIdPw;
private String resName;
private String resId;
public SearchIdPwListener(JRadioButton [] selIdPwBtn, JLabel [] QnALabel, JTextField [] wantTxt, JButton [] btn, SearchIdPw searchIdPw) {
this.selIdPwBtn = selIdPwBtn;
this.QnALabel = QnALabel;
this.wantTxt = wantTxt;
this.btn = btn;
this.searchIdPw = searchIdPw;
}
// 버튼 클릭시 작동
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
JButton btn = (JButton)e.getSource();
if((btn.getText()).equals("search")) {
if(selIdPwBtn[1].isSelected()) { // 라디오버튼이 ID 에 선택되어 있음
if(!(wantTxt[0].getText()).equals("")){
try {
resName = repDB.memberSurf("name", wantTxt[0].getText());
if(resName != null && !resName.equals("관리자")) {
String ID = repDB.infoSurfID("id", "name", wantTxt[0].getText());
QnALabel[3].setText(ID);
} else {
JOptionPane.showMessageDialog(null, "일치하는 회원정보가 없습니다.\n다시 시도해주세요.", "Search failed", JOptionPane.WARNING_MESSAGE);
}
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
} else {
JOptionPane.showMessageDialog(null, "입력된 정보가 없습니다.", "Please input information", JOptionPane.WARNING_MESSAGE);
}
} else if (selIdPwBtn[0].isSelected()) { // 라디오버튼이 PW 에 선택되어 있음
if(!(wantTxt[0].getText()).equals("") && !(wantTxt[1].getText()).equals("")) {
try {
resName = repDB.memberSurf("name", wantTxt[0].getText());
resId = repDB.memberSurf("id", wantTxt[1].getText());
if(resName != null && resId != null && !resName.equals("관리자") && !resId.equals("ManagerJang")) {
String ID = repDB.infoSurfPW("pw", "name", "id", wantTxt[0].getText(), wantTxt[1].getText());
QnALabel[3].setText(ID);
} else {
JOptionPane.showMessageDialog(null, "일치하는 회원정보가 없습니다.\n다시 시도해주세요.", "Search failed", JOptionPane.WARNING_MESSAGE);
}
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
} else {
JOptionPane.showMessageDialog(null, "입력된 정보가 없습니다.", "Please input information", JOptionPane.WARNING_MESSAGE);
}
}
} else {
searchIdPw.setVisible(false);
reset();
}
}
// 텍스트 필드 창 엔터
public void keyPressed(KeyEvent e) {
// 나중에..^^..........
}
// return(true): pw X & id O
// return(false): pw O & id X
public boolean selectedRaBtn(JRadioButton [] selIdPwBtn) {
boolean judge = true;
if(selIdPwBtn[0].isSelected() == true) {
// PW 라디오버튼이 선택되어 있으면(= ID 라디오버튼은 선택X) false 리턴.
judge = false;
} // else { // PW 라디오버튼이 선택되어 있지 않으면(= ID 라디오버튼은 선택O) true 리턴.}
return judge;
}
// 화면 초기화
public void reset() {
wantTxt[0].setText("");
wantTxt[1].setText("");
selIdPwBtn[0].setSelected(true);
QnALabel[1].setBounds(40, 80, 150, 20);
wantTxt[0].setBounds(130, 83, 150, 20);
QnALabel[2].setVisible(true);
wantTxt[1].setVisible(true);
QnALabel[3].setText("-");
}
}
개인프로젝트 후 팀프로젝트를 거의 바로 시작해서 오류수정 못한상태... 비밀번호 찾기 정보 오입력시 알림창 떠야 하는데 전혀 다른 값이 나옴.
엔터키 리스너도 안함.. ^^;