-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJava222_gui.java
More file actions
170 lines (141 loc) · 3.79 KB
/
Copy pathJava222_gui.java
File metadata and controls
170 lines (141 loc) · 3.79 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
package java0918_gui;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
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.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;
import javax.swing.JTextField;
class Research extends JFrame implements ActionListener {
JTextField nameTxt;
JRadioButton manRad, womanRad;
JComboBox<String> locBox;
JButton saveBtn, openBtn;
JTextArea multiLine;
public Research() {
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);
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(saveBtn);
jp4.add(openBtn);
JPanel top = new JPanel(new GridLayout(4, 1));
top.add(jp1);
top.add(jp2);
top.add(jp3);
top.add(jp4);
multiLine = new JTextArea(10, 20);
// JFrame의 Layout을 GridLayout 2행 1열로 변경한다.
setLayout(new GridLayout(2, 1));
// JFrame에 컴포넌트를 연결한다.
add(top);
add(multiLine);
// 버튼에 리스너를 연결한다.
saveBtn.addActionListener(this);
openBtn.addActionListener(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();
}
}
public void init() {
nameTxt.setText("");
manRad.setSelected(true);
locBox.setSelectedIndex(0);
nameTxt.requestFocus();
}
private void saveMethod() {
String name = nameTxt.getText();
String gen = manRad.isSelected() ? "남" : "여";
String loc = (String) locBox.getSelectedItem();
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, true);
fw.write(name + "/" + gen + "/" + loc + "\r\n");
fw.flush();
init();
} 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;
multiLine.setText("");
try {
sc = new Scanner(file);
while (sc.hasNextLine()) {
multiLine.append(sc.nextLine() + "\r\n");
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
sc.close();
}
}
}
public class Java222_gui {
public static void main(String[] args) {
new Research();
}
}