package test;
import java.awt.*;
public class Memo extends JFrame implements ActionListener {
Menu file;
MenuBar bar;
MenuItem fileSave, fileOpen, fileExit;
TextArea ta;
FileDialog fd;
FileWriter fw;
String text;
public Memo() {
super("Menu example");
file = new Menu("파일");
fileSave = new MenuItem("저장하기");
file.add(fileSave);
fileOpen = new MenuItem("불러오기");
file.add(fileOpen);
fileExit = new MenuItem("종료");
file.add(fileExit);
fileSave.addActionListener(this);
fileOpen.addActionListener(this);
fileExit.addActionListener(this);
bar = new MenuBar();
setMenuBar(bar);
bar.add(file);
ta = new TextArea();
add(ta);
getContentPane();
setSize(640, 480);
setLocation(500, 300);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand() == "저장하기") {
System.out.println("저장하기");
fd = new FileDialog(this, "저장하기");
fd.setMode(FileDialog.SAVE);
fd.setVisible(true);
text = ta.getText();
try {
char intxt[] = new char[text.length()];
text.getChars(0, text.length(), intxt, 0);
fw = new FileWriter(fd.getDirectory() + fd.getFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(intxt);
bw.close();
} catch (IOException e1) {
e1.printStackTrace();
}
} else if (e.getActionCommand() == "불러오기") {
System.out.println("불러오기");
fd = new FileDialog(this, "불러오기");
fd.setMode(FileDialog.LOAD);
fd.setVisible(true);
try {
FileReader fr = new FileReader(fd.getDirectory() + fd.getFile());
BufferedReader br = new BufferedReader(fr);
ta.setText("");
for (String str; (str = br.readLine()) != null;) {
ta.append(str + "\n");
}
br.close();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
} else if (e.getActionCommand() == "종료") {
System.out.println("종료");
System.exit(0);
}
}
public static void main(String[] args) {
Memo memo = new Memo();
memo.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}