본문 바로가기

JAVA

[JAVA] 자바 스윙(Java Swing)을 이용한 text파일 회원 가입 프로그램 만들기

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import java.awt.event.*;
import java.io.*;
import java.awt.*;
import javax.swing.*;
 
class Members extends JFrame implements ActionListener{
    
    JPanel panel;
    JLabel lblid, lblpw, lblname, lblemail, lbladdr;
    JButton b1, b2;
    JTextField txtid, txtpw, txtname, txtemail, txtaddr;
    
    Members(){
        setTitle("회원가입");
        setSize(300150);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        panel = new JPanel();
        panel.setLayout(new GridLayout(02));
        
        lblid = new JLabel("ID");
        lblpw = new JLabel("PW");
        lblname = new JLabel("NAME");
        lblemail = new JLabel("EMAIL");
        lbladdr = new JLabel("ADDR");
        
        txtid = new JTextField(10);
        txtpw = new JTextField(10);
        txtname = new JTextField(10);
        txtemail = new JTextField(20);
        txtaddr = new JTextField(30);
        
        b1 = new JButton("가입");
        b2 = new JButton("취소");
        
        panel.add(lblid);
        panel.add(txtid);
        panel.add(lblpw);
        panel.add(txtpw);
        panel.add(lblname);
        panel.add(txtname);
        panel.add(lblemail);
        panel.add(txtemail);
        panel.add(lbladdr);
        panel.add(txtaddr);
        panel.add(b1);
        panel.add(b2);
        
        add(panel);
        
        b1.addActionListener(this);
        b2.addActionListener(this);
        
        setVisible(true);
    }
    
        public void actionPerformed(ActionEvent e) {
            try{
                String s = null;
                boolean isOk = false;
                BufferedWriter bw = new BufferedWriter(new FileWriter("members.txt"true));
                BufferedReader br = new BufferedReader(new FileReader("members.txt"));
                
                
                if(e.getSource() == b1) {
                    while((s = br.readLine()) != null) {
                        
                        // 아이디 중복
                        String[] array = s.split("/");
                        if(array[0].equals(txtid.getText())){
                                isOk = true;
                                break;
                        }
                    }
                            //정보 입력시 중복이 없으면 데이터 보냄
                            if(!isOk) {
                            bw.write(txtid.getText() + "/");
                            bw.write(txtpw.getText() + "/");
                            bw.write(txtname.getText() + "/");
                            bw.write(txtemail.getText() + "/");
                            bw.write(txtaddr.getText() + "\r\n");
                            bw.close();
 
                            JOptionPane.showMessageDialog(null"회원가입을 축하합니다.");
                            dispose();
                            }else {
                                JOptionPane.showMessageDialog(null"회원가입에 실패하였습니다.");
                            }
                    
                }else if(e.getSource() == b2) {
                    txtid.setText("");
                    txtpw.setText("");
                    txtname.setText("");
                    txtemail.setText("");
                    txtaddr.setText("");
                }
            }catch (IOException  ex){
                JOptionPane.showMessageDialog(null"실패");
            }
        }
        
    public static void main(String[] args) {
        new Members();
    }
}