package com.bank;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintStream;
import java.io.StringWriter;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
//JFrame 기본 레이아웃은 BorderLayout
public class Memo extends JFrame {
JTextArea ta;
String fileName;
public Memo(String title) {
setTitle(title);
JMenu mfile = new JMenu("파일");
JMenuItem mOpen = new JMenuItem("열기");
JMenuItem mNew = new JMenuItem("새로만들기");
JMenuItem mSave = new JMenuItem("저장");
JMenuItem mSaveAs = new JMenuItem("다른이름으로 저장");
JMenuItem mExit = new JMenuItem("끝내기");
mfile.add(mOpen);
mfile.add(mNew);
mfile.add(mSave);
mfile.add(mSaveAs);
mfile.addSeparator();
mfile.add(mExit);
JMenuBar mb = new JMenuBar();
mb.add(mfile);
setJMenuBar(mb); //메뉴부착
ta = new JTextArea();
//JScrollPane jsp = new JScrollPane(ta);
JScrollPane jsp = new JScrollPane();
jsp.setViewportView(ta);
add(jsp);
// 새로만들기
mNew.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
setTitle("제목없음");
ta.setText("");
}
});
// 열기
mOpen.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JFileChooser fc = new JFileChooser();
fc.showOpenDialog(Memo.this);
//파일을 읽어서 ta에 출력
File f = fc.getSelectedFile();
fileName = f.getPath();
setTitle(f.getName());
//System.out.println("getPath : " + fileName);
fileRead(fileName); //실제로 파일을 읽어오는 메소드
}
});
// 저장
mSave.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (getTitle().equals("제목없음")) {
JFileChooser fc = new JFileChooser();
int a = fc.showSaveDialog(Memo.this);
if(a==JFileChooser.CANCEL_OPTION) {return;}
File f = fc.getSelectedFile();
fileName = f.getPath();
setTitle(f.getName());
} else {
fileSave(fileName);
}
}
});
// 다른 이름으로 저장
mSaveAs.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JFileChooser fc = new JFileChooser();
int a = fc.showSaveDialog(Memo.this);
//if(a==1) {return;}
if (a==JFileChooser.CANCEL_OPTION) {return;}
File f = fc.getSelectedFile();
fileName = f.getPath();
System.out.println("저장파일 : " + fileName);
//ta에 있는 내용 파일로 출력
fileSave(fileName);
setTitle(f.getName());
}
});
// 끝내기
mExit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
setSize(500, 400);
setVisible(true);
}
//파일읽기
private void fileRead(String fileName) {
try {
FileReader fr = new FileReader(fileName);
StringWriter sw = new StringWriter();
while(true) {
int ch = fr.read();
if (ch==-1) break;
sw.write(ch);
}
sw.close();
ta.setText(sw.toString());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private void fileSave (String fileName) {
try {
PrintStream ps = new PrintStream(fileName);
ps.println(ta.getText());
ps.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new Memo("제목없음");
}
}
'IT > Java' 카테고리의 다른 글
java - JFrame 예제 (list에 입력, 출력, 파일출력, 이벤트 처리) (0) | 2018.11.25 |
---|---|
Java - 크롤링, crawling (0) | 2018.11.24 |
Java - 싱글톤 (0) | 2018.11.24 |
Java - 오버로딩, 오버라이딩, 인터페이스 (0) | 2018.11.24 |
응용SW기초기술활용(윈도우 명령 프롬프트(DOS)용 명령어, 리눅스(유닉스) 콘솔 명령어) (0) | 2018.11.23 |