프로그래밍 언어/<Java>영어단어장 project

<Java 영어 단어장 만들기> 4. 완성

창조적생각 2021. 7. 29. 17:18

 

코드 전체 보기
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
package final_test;
import javax.swing.*;
import java.util.*;
import java.awt.event.*;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.awt.*;
 
class Word{
    private String english; 
    private String korean; 
    public Word(String english, String korean) {
        this.english = english;
        this.korean = korean;
    }
    public String getEnglish() { return english; }
    public String getKorean() { return korean; }
}
 
class view_all extends JDialog{
    
    private JLabel show_all = new JLabel("안녕하세요. 미구현이에요");
    private JButton OK = new JButton("OK");
    public view_all(JFrame frame,String title) {
    setLayout(new FlowLayout());
    add(show_all);
    add(OK);
    setSize(500,500);
    
    OK.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            setVisible(false);
        }
    });
    
        
    }
    
}
class addword extends JDialog{
    
    private JLabel enput = new JLabel("영어");
    private JTextField en = new JTextField(30);
    private JLabel koput = new JLabel("뜻");
    private JTextField ko = new JTextField(30);
    private JButton OK = new JButton("OK");
    public addword(JFrame frame,String title) {
    setLayout(new FlowLayout());
    add(enput);
    add(en);
    add(koput);
    add(ko);
    add(OK);
    setSize(400,400);
    
        
    OK.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            setVisible(false);
            Question_W();
        }
    });
        
    }
    void Question_W() {
        OutputStreamWriter out = null;
        FileOutputStream fos = null;
        
        try {
            fos = new FileOutputStream("c:\\Temp\\test.txt",true);
            out = new OutputStreamWriter(fos,"UTF-8");
            
            out.write("/");
            out.write(en.getText());
            out.write("/");
            out.write(ko.getText());
            
            out.close();
        }
        catch(IOException e){
            System.out.println("입출력오류");
        }
    }
}
class Creator extends JDialog{
    
    private JLabel show_all = new JLabel("만든놈: 서성우 2021 7월 말쯤");
    private JButton OK = new JButton("OK");
    public Creator(JFrame frame,String title) {
    setLayout(new FlowLayout());
    add(show_all);
    add(OK);
    setSize(200,100);
    
    OK.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            setVisible(false);
        }
    });
    
        
    }
}
class start extends JDialog{
    Vector<Word> v;
    Vector<String> V_eng;
    Vector<String> V_kor;
    
    int example[];
    int answer1;
    int answerIndex;
    static int cnt = 0;
    String eng;
    String meta ="";
    
    JButton[] answer;
    JLabel Q1;
    
    
    public start(JFrame frame,String title) {
        Question_R();
        v = new Vector<Word>();
        V_eng = new Vector<String>();
        V_kor = new Vector<String>();
        Font f = new Font("맑은 고딕",Font.PLAIN,50);
        setLayout(null);
        addvector();
        makeexam();
        makebutton();
    
        for(int i = 0; i < 4; i++)
            answer[i].addActionListener(new myactionlistener());
    
    answer[0].setLocation(20,180);
    answer[0].setSize(300,50);
    answer[1].setLocation(20,240);
    answer[1].setSize(300,50);
    answer[2].setLocation(20,300);
    answer[2].setSize(300,50);
    answer[3].setLocation(20,360);
    answer[3].setSize(300,50);
    
    for(int i = 0; i < 4; i++)
        add(answer[i]);
    JButton next = new JButton("next");
    next.setLocation(330,180);
    next.setSize(130230);
    next.addActionListener(new myactionlistener());
    add(next);
    
    this.Q1 = new JLabel(eng);
    Q1.setLocation(150,10);
    Q1.setSize(400,100);
    Q1.setFont(f);
    add(Q1);
    
    setSize(500,500);
        
    }
    
    private int makeExample(int ex[], int answerIndex) {
        int n[] = {-1-1-1-1}; // 
        int index;
        for(int i=0; i<n.length; i++) {
            do {
                index = (int)(Math.random()*v.size());
            } while(index == answerIndex || exists(n, index)); 
            n[i] = index;
        }
 
        for(int i=0; i<n.length; i++) ex[i] = n[i];
        return (int)(Math.random()*n.length); 
    }
    
    private boolean exists(int n[], int index) {
        for(int i=0; i<n.length; i++) {
            if(n[i] == index)
                return true;
        }
        return false;
    }
    
    private void makeexam() {
        this.answerIndex = (int)(Math.random()*v.size()); 
        this.eng = v.get(answerIndex).getEnglish();
        this.example = new int[4];
        this.answer1 = makeExample(example,answerIndex);
        example[answer1] = answerIndex;    
    }
    
    private void makebutton() {
        this.answer= new JButton[4];
        answer[0= new JButton(v.get(example[0]).getKorean());
        answer[1= new JButton(v.get(example[1]).getKorean());
        answer[2= new JButton(v.get(example[2]).getKorean());
        answer[3= new JButton(v.get(example[3]).getKorean());
        
    }
    
    void Question_R() {
        InputStreamReader in = null;
        FileInputStream fin = null;
        try {
            fin = new FileInputStream("c:\\Temp\\test.txt");
            in = new InputStreamReader(fin,"UTF-8");
            int c;
            
            while((c = in.read()) != -1) {
                meta += (char)c;
            }
        
            fin.close();
            in.close();
        }
        catch(IOException e) {
            System.out.println("입출력오류");
        }
    }
    
    void addvector() {
        String [] tokens = meta.split("/");
        for(int i =0; i<tokens.length;i++) {
            if(i%2 == 0)
                V_eng.add(tokens[i]);
            else V_kor.add(tokens[i]);
                
        }
        for(int i =0; i < V_kor.size();i++) {
            String Kor = V_kor.get(i);
            String Eng = V_eng.get(i);
            v.add(new Word(Eng,Kor));
            
            
            
        }
    }
    
    class myactionlistener implements ActionListener{
        
        public void actionPerformed(ActionEvent e) {
            
            JButton b = (JButton)e.getSource();
            for(int i = 0; i<4; i++) {
            if (b.getText().equals(v.get(example[answer1]).getKorean())) {
            b.setText("맞았어.");
            cnt++;
            }
            }
            
            JButton N = (JButton)e.getSource();
            if(N.getText().equals("next")) {
                makeexam();
                Q1.setText(eng);
                for(int j = 0; j<4; j++) {
                answer[j].setText(v.get(example[j]).getKorean());}
                }
            
            
            }
    
}
}
cs

너무 길어서 2개로 나눴습니다.

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
 
public class TestFrame extends JFrame{
    view_all v_a;
    Creator ct;
    addword aw;
    start st;
    
    JMenuBar mb;
    JMenuItem Start;
    JMenuItem View_all;
    JMenuItem Add_words;
    JMenuItem creator;
    
    String i = String.valueOf(start.cnt);
    
    TestFrame(){
        v_a = new view_all(this,"안녕하세요");
        ct = new Creator(this,"만든놈");
        aw = new addword(this,"단어를 추가해주세요.");
        st = new start(this,"테스트 시작!");
        
        setTitle("영어 단어 공부!");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Container c = getContentPane();
        c.setLayout(null);
        
        createMenu();
        
        JLabel help = new JLabel("서성우의 영어 단어장에 오신것을 환영합니다.");
        JLabel help2 = new JLabel ( "시험 시작을 누르시면 메모장에 저장된 단어들로 시험을 칠수 있습니다.");
        JLabel help3 = new JLabel ("단어를 추가하려면 단어 추가하기를 눌러주세요.");
        
        help.setSize(300,40);
        help2.setSize(400,40);
        help3.setSize(300,40);
        help.setLocation(40200);
        help2.setLocation(40250);
        help3.setLocation(40300);
        
        c.add(help);
        c.add(help2);
        c.add(help3);
        
        JLabel count = new JLabel (String.valueOf(start.cnt));
        count.setSize(300,100);
        count.setLocation(4090);
        c.add(count);
        
        setSize(500,500);
        setVisible(true);
    }
    
    private void createMenu() {
        mb = new JMenuBar();
        View_all = new JMenuItem("전체 단어 보기");
        Add_words = new JMenuItem("단어 추가하기");
        creator = new JMenuItem("만드신 분");
        Start = new JMenuItem("시험 시작");
        mb.add(Start);
        Start.addActionListener(new MenuActionListener());
        mb.add(View_all);
        View_all.addActionListener(new MenuActionListener());
        mb.add(Add_words);
        Add_words.addActionListener(new MenuActionListener());
        mb.add(creator);
        creator.addActionListener(new MenuActionListener());
        setJMenuBar(mb);
        }
    
    class MenuActionListener implements ActionListener{
        public void actionPerformed(ActionEvent e) {
            String cmd = e.getActionCommand();
            switch(cmd) {
            case "전체 단어 보기":
                v_a.setVisible(true);
                break;
            case "만드신 분":
                ct.setVisible(true);
                break;
            case "단어 추가하기":
                aw.setVisible(true);
                break;
            case "시험 시작" :
                st.setVisible(true);
                break;
            }
                
        }
    }
 
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        new TestFrame();
    }
 
}
cs

 

코드는 바로 위에 코드 전체 보기를 누르시면 나옵니다.

1. 메뉴를 통해 단어 테스트, 들어있는 단어 전체보기, 단어 추가하기, 만든 사람 을 선택할 수 있습니다.

2. 단어 테스트는 총 4개의 객관식으로 이뤄져서 추가된 전체 단어들을 외부 메모장에서 읽어와 자동으로 문제를 만들어 냅니다.

3. 단어 추가하기는 메모장에 단어를 추가해줘 단어 테스트 및 단어 전체보기에 추가 됩니다.

4. 단어 전체보기는 아직 미 구현 단계입니다.

5. 만든 사람을 누르면 만든 사람이 나옵니다. 

 

728x90