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 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(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();
}
}