Repository navigation
Expand file tree
/
Copy pathContactMain.java
More file actions
350 lines (281 loc) · 9.3 KB
/
Copy pathContactMain.java
File metadata and controls
350 lines (281 loc) · 9.3 KB
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
package edu.java.contact06;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableModel;
import javax.swing.JTextArea;
public class ContactMain {
private static ContactDAO dao;
private static int size;
private JFrame frame;
private JTextField txtName;
private JTextField txtSearch;
private JTextField txtPhone;
private JTextField txtEmail;
private JTextField txtIndex;
private JTextArea txtAreaResult;
private JTable table;
private String[] colNames = {"No", "이름", "전화번호", "이메일"};
private Object[] records = new Object[colNames.length];
private DefaultTableModel tableModel;
/**
* Launch the application.
*/
public static void main(String[] args) {
dao = ContactDAOImple.getInstance();
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ContactMain window = new ContactMain();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public ContactMain() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 1000, 653);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JLabel lblConPro = new JLabel("Contact Program");
lblConPro.setHorizontalAlignment(SwingConstants.CENTER);
lblConPro.setFont(new Font("굴림", Font.PLAIN, 30));
lblConPro.setBounds(268, 10, 415, 48);
frame.getContentPane().add(lblConPro);
JLabel lblConManage = new JLabel("연락처 관리");
lblConManage.setFont(new Font("굴림", Font.PLAIN, 24));
lblConManage.setHorizontalAlignment(SwingConstants.CENTER);
lblConManage.setBounds(89, 59, 197, 33);
frame.getContentPane().add(lblConManage);
JLabel lblName = new JLabel("성 명");
lblName.setHorizontalAlignment(SwingConstants.CENTER);
lblName.setFont(new Font("굴림", Font.PLAIN, 18));
lblName.setBounds(42, 102, 108, 36);
frame.getContentPane().add(lblName);
JLabel lblPhone = new JLabel("전화번호");
lblPhone.setHorizontalAlignment(SwingConstants.CENTER);
lblPhone.setFont(new Font("굴림", Font.PLAIN, 18));
lblPhone.setBounds(42, 148, 108, 36);
frame.getContentPane().add(lblPhone);
JLabel lblEmail = new JLabel("이 메 일");
lblEmail.setHorizontalAlignment(SwingConstants.CENTER);
lblEmail.setFont(new Font("굴림", Font.PLAIN, 18));
lblEmail.setBounds(42, 194, 108, 36);
frame.getContentPane().add(lblEmail);
txtName = new JTextField();
txtName.setBounds(180, 102, 255, 36);
frame.getContentPane().add(txtName);
txtName.setColumns(10);
txtPhone = new JTextField();
txtPhone.setColumns(10);
txtPhone.setBounds(180, 148, 255, 36);
frame.getContentPane().add(txtPhone);
txtEmail = new JTextField();
txtEmail.setColumns(10);
txtEmail.setBounds(180, 196, 255, 36);
frame.getContentPane().add(txtEmail);
JButton btnRegister = new JButton("등 록");
btnRegister.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
insertContact();
}
});
btnRegister.setFont(new Font("굴림", Font.PLAIN, 18));
btnRegister.setBounds(42, 240, 393, 42);
frame.getContentPane().add(btnRegister);
JLabel lblIndex = new JLabel("수정/삭제할 인덱스");
lblIndex.setHorizontalAlignment(SwingConstants.CENTER);
lblIndex.setFont(new Font("굴림", Font.PLAIN, 18));
lblIndex.setBounds(42, 292, 197, 36);
frame.getContentPane().add(lblIndex);
txtIndex = new JTextField();
txtIndex.setColumns(10);
txtIndex.setBounds(250, 293, 185, 36);
frame.getContentPane().add(txtIndex);
JButton btnModify = new JButton("수 정");
btnModify.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
modifyContact();
}
});
btnModify.setFont(new Font("굴림", Font.PLAIN, 18));
btnModify.setBounds(42, 339, 197, 42);
frame.getContentPane().add(btnModify);
JButton btnDelete = new JButton("삭 제");
btnDelete.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
deleteContact();
}
});
btnDelete.setFont(new Font("굴림", Font.PLAIN, 18));
btnDelete.setBounds(250, 339, 185, 42);
frame.getContentPane().add(btnDelete);
JLabel lblResult = new JLabel("실 행 결 과");
lblResult.setHorizontalAlignment(SwingConstants.CENTER);
lblResult.setFont(new Font("굴림", Font.PLAIN, 14));
lblResult.setBounds(26, 391, 108, 22);
frame.getContentPane().add(lblResult);
JScrollPane sclPaneResult = new JScrollPane();
sclPaneResult.setBounds(42, 423, 393, 182);
frame.getContentPane().add(sclPaneResult);
txtAreaResult = new JTextArea();
sclPaneResult.setViewportView(txtAreaResult);
JLabel lblConSearch = new JLabel("연락처 검색");
lblConSearch.setFont(new Font("굴림", Font.PLAIN, 24));
lblConSearch.setHorizontalAlignment(SwingConstants.CENTER);
lblConSearch.setBounds(629, 59, 175, 33);
frame.getContentPane().add(lblConSearch);
txtSearch = new JTextField();
txtSearch.setBounds(500, 102, 253, 33);
frame.getContentPane().add(txtSearch);
txtSearch.setColumns(10);
JButton btnSearch = new JButton("검 색");
btnSearch.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
searchContact();
}
});
btnSearch.setBounds(788, 102, 147, 36);
frame.getContentPane().add(btnSearch);
JScrollPane sclPaneSearch = new JScrollPane();
sclPaneSearch.setBounds(500, 160, 435, 445);
frame.getContentPane().add(sclPaneSearch);
tableModel = new DefaultTableModel(colNames, 0);
table = new JTable(tableModel);
table.setFont(new Font("굴림", Font.PLAIN, 15));
sclPaneSearch.setViewportView(table);
}
protected void insertContact() {
txtAreaResult.append("연락처 정보 등록\n");
String name = txtName.getText();
String phone = txtPhone.getText();
String email = txtEmail.getText();
if(name.equals("") || phone.equals("") || email.equals("")) {
txtAreaResult.append("정보 중 하나라도 비어있으면 안됩니다.\n");
return;
}
ContactVO vo = new ContactVO(name, phone, email);
int result = dao.insert(vo);
if(result == 1) {
txtAreaResult.append("연락처 등록 성공\n");
} else {
txtAreaResult.append("연락처 등록 실패\n");
}
}
protected void modifyContact() {
size = ((ContactDAOImple)dao).getSize();
if (size == 0) {
txtAreaResult.append("연락처 등록을 먼저 해주세요\n");
return;
}
String index = txtIndex.getText();
try {
int idx = Integer.parseInt(index);
if (!(idx >= 1 && idx < size)) {
txtAreaResult.append("올바르지 않은 인덱스입니다.\n");
return;
}
String name = txtName.getText();
String phone = txtPhone.getText();
String email = txtEmail.getText();
if(name.equals("") || phone.equals("") || email.equals("")) {
txtAreaResult.append("정보 중 하나라도 비어있으면 안됩니다.\n");
return;
}
ContactVO vo = new ContactVO(name, phone, email);
int result = ((ContactDAOImple)dao).modify(idx, vo);
if (result == 1) {
txtAreaResult.append("연락처 [" + idx + "] 수정 성공\n");
} else {
txtAreaResult.append("연락처 [" + idx + "] 수정 실패\n");
}
} catch (Exception e2) {
System.out.println(e2);
}
}
protected void deleteContact() {
size = ((ContactDAOImple)dao).getSize();
if (size == 0) {
txtAreaResult.append("연락처 등록을 먼저 해주세요\n");
return;
}
String index = txtIndex.getText();
try {
int idx = Integer.parseInt(index);
if (!(idx >= 0 && idx < size)) {
txtAreaResult.append("올바르지 않은 인덱스입니다.\n");
return;
}
int result = ((ContactDAOImple)dao).delete(idx);
if (result == 1) {
txtAreaResult.append("연락처 [" + idx + "] 삭제 성공\n");
} else {
txtAreaResult.append("연락처 [" + idx + "] 삭제 실패\n");
}
} catch (Exception e2) {
System.out.println(e2);
}
}
protected void searchContact() {
size = ((ContactDAOImple)dao).getSize();
if (size == 0) {
txtAreaResult.append("연락처 등록을 먼저 해주세요\n");
return;
}
if(txtSearch.getText().equals("")) {
txtAreaResult.append("Total search\n");
tableModel.setNumRows(0);
ArrayList<ContactVO> list = dao.search();
for (int i = 0; i < size; i++) {
records[0] = i + 1;
records[1] = list.get(i).getName();
records[2] = list.get(i).getPhone();
records[3] = list.get(i).getEmail();
tableModel.addRow(records);
}
} else {
txtAreaResult.append("Detail search\n");
String index = txtSearch.getText();
try {
int idx = Integer.parseInt(index);
if (!(idx >= 1 && idx < size)) {
txtAreaResult.append("올바르지 않은 인덱스입니다.\n");
return;
}
tableModel.setNumRows(0);
ContactVO vo = dao.search(idx);
records[0] = idx;
records[1] = vo.getName();
records[2] = vo.getPhone();
records[3] = vo.getEmail();
tableModel.addRow(records);
} catch (Exception e2) {
System.out.println(e2);
}
}
}
}