-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJava223_gui.java
More file actions
261 lines (221 loc) · 6.41 KB
/
Copy pathJava223_gui.java
File metadata and controls
261 lines (221 loc) · 6.41 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
package java0918_gui;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableColumnModel;
class Research2 extends JFrame implements ActionListener, MouseListener {
JTextField nameTxt;
JRadioButton manRad, womanRad;
JComboBox<String> locBox;
JButton regBtn, saveBtn, openBtn;
JTable table;
// table에서 사용되는 데이터를 관리해주는 객체이다.
DefaultTableModel model;
public Research2() {
nameTxt = new JTextField(10);
// 라디오버튼 생성 및 그룹
manRad = new JRadioButton("남", true);
womanRad = new JRadioButton("여");
ButtonGroup bg = new ButtonGroup();
bg.add(manRad);
bg.add(womanRad);
// 콤보박스에 데이터 삽입
String[] city = new String[] { "seoul", "jeju", "pusan", "daejon" };
locBox = new JComboBox<String>(city);
regBtn = new JButton("정보등록");
saveBtn = new JButton("파일저장");
openBtn = new JButton("파일열기");
JPanel jp1 = new JPanel();
jp1.add(new JLabel("이름"));
jp1.add(nameTxt);
JPanel jp2 = new JPanel();
jp2.add(new JLabel("성별"));
jp2.add(manRad);
jp2.add(womanRad);
JPanel jp3 = new JPanel();
jp3.add(new JLabel("지역"));
jp3.add(locBox);
JPanel jp4 = new JPanel();
jp4.add(regBtn);
jp4.add(saveBtn);
jp4.add(openBtn);
JPanel top = new JPanel(new GridLayout(4, 1));
top.add(jp1);
top.add(jp2);
top.add(jp3);
top.add(jp4);
// 테이블의 컬럼명을 배열에 저장한다.
String[] columnNames = { "이름", "성별", "지역" };
// 테이블의 데이터를 관리해줄 model 을 생성한다.
// 생성자를 호출할때 컬럼명, 행의 갯수를 넘겨준다.
model = new DefaultTableModel(columnNames, 0);
table = new JTable(model);
// 테이블의 사이즈를 고정시킨다.
table.setAutoResizeMode(0);
JTableHeader header = table.getTableHeader();
// 테이블의 컬럼명을 고정시킨다.
header.setReorderingAllowed(false);
// 컬럼모델을 리턴한다.
TableColumnModel columnModel = header.getColumnModel();
// 컬럼별로 크기를 설정한다.
columnModel.getColumn(0).setPreferredWidth(100);
columnModel.getColumn(1).setPreferredWidth(50);
columnModel.getColumn(2).setPreferredWidth(150);
// 테이블의 행의 높이를 설정한다.
table.setRowHeight(25);
// 테이블 편집 불가
table.setEnabled(false);
// JFrame의 Layout을 GridLayout 2행 1열로 변경한다.
setLayout(new GridLayout(2, 1));
// JFrame에 컴포넌트를 연결한다.
add(top);
add(new JScrollPane(table));
// 버튼에 리스너를 연결한다.
regBtn.addActionListener(this);
saveBtn.addActionListener(this);
openBtn.addActionListener(this);
regBtn.addMouseListener(this);
saveBtn.addMouseListener(this);
openBtn.addMouseListener(this);
// 윈도우창 생성
setSize(300, 300);
setLocation(400, 200);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
@Override
public void actionPerformed(ActionEvent e) {
// 이벤트가 발생된 오브젝트를 리턴함
Object obj = e.getSource();
if (obj == saveBtn) {
saveMethod();
} else if (obj == openBtn) {
openMethod();
} else if (obj == regBtn) {
regMethod();
}
}
public void init() {
nameTxt.setText("");
manRad.setSelected(true);
locBox.setSelectedIndex(0);
nameTxt.requestFocus();
}
private void saveMethod() {
JFileChooser save = new JFileChooser();
int chk = save.showSaveDialog(this);
// 취소를 선택 한 경우 에러 없이 취소 할 수 있게 함
if (chk == JFileChooser.CANCEL_OPTION) {
return;
}
File file = save.getSelectedFile();
FileWriter fw = null;
try {
// false : 업데이트 , true : append , 기본 값은 false
fw = new FileWriter(file);
for (int i = 0; i < table.getRowCount(); i++) {
String name = (String) table.getValueAt(i, 0);
String gen = (String) table.getValueAt(i, 1);
String loc = (String) table.getValueAt(i, 2);
fw.write(name + "/" + gen + "/" + loc + "\r\n");
}
fw.flush();
JOptionPane.showMessageDialog(this, "저장되었습니다.");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private void openMethod() {
JFileChooser open = new JFileChooser();
int chk = open.showOpenDialog(this);
if (chk == JFileChooser.CANCEL_OPTION) {
return;
}
File file = open.getSelectedFile();
Scanner sc = null;
try {
sc = new Scanner(file);
model.setRowCount(0);
while (sc.hasNextLine()) {
String[] line = sc.nextLine().split("/");
model.addRow(line);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
sc.close();
}
}
private void regMethod() {
String[] line = new String[3];
line[0] = nameTxt.getText();
line[1] = manRad.isSelected() ? "남" : "여";
line[2] = (String) locBox.getSelectedItem();
model.addRow(line);
init();
}
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
Object obj = e.getSource();
if (obj == regBtn) {
regBtn.setText("REGISTER");
} else if (obj == saveBtn) {
saveBtn.setText("SAVE");
} else if (obj == openBtn) {
openBtn.setText("OPEN");
}
}
@Override
public void mouseExited(MouseEvent e) {
Object obj = e.getSource();
if (obj == regBtn) {
regBtn.setText("정보등록");
} else if (obj == saveBtn) {
saveBtn.setText("파일저장");
} else if (obj == openBtn) {
openBtn.setText("파일열기");
}
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
}
public class Java223_gui {
public static void main(String[] args) {
new Research2();
}
}