자바 메모장 만들기

코드양이 좀 많네요. 궁금하신건 댓글달아 주시면 됩니다.

import java.io.*;
import java.awt.*;
import java.awt.event.*;

class SimpleNotePad extends WindowAdapter implements ActionListener{
 private Frame f;
 private TextArea pad;

 private FileDialog loadDl;
 private FileDialog saveDl;

 private MenuBar mb;
 private Menu file;
 private MenuItem newItem;
 private MenuItem open;
 private MenuItem save;
 private MenuItem saveAs;
 private MenuItem setPage;
 private MenuItem print;
 private MenuItem exit;
 private Menu edit;
 private MenuItem cancel;
 private MenuItem cut;
 private MenuItem copy;
 private MenuItem paste;
 private MenuItem delete;
 private MenuItem search;
 private MenuItem searchNext;
 private MenuItem change;
 private MenuItem move;
 private MenuItem selectAll;
 private MenuItem time;
 private Menu form;
 private MenuItem nextln;
 private MenuItem font;
 private Menu view;
 private MenuItem statusBar;
 private Menu help;
 private MenuItem helpMenu;
 private MenuItem info;

 private Dimension dim1;
 private Dimension dim2;
 private int xpos;
 private int ypos;

 private File ffile;
 private PrintWriter out;
 private BufferedReader in;
 private String filePath;
 private String str;
 private String loadedStr;
 private String saveStr;

 public SimpleNotePad(){
  this.f = new Frame("제목 없음 - 메모장");
  this.pad = new TextArea("", 1, 1, pad.SCROLLBARS_BOTH);

  this.loadDl = new FileDialog(f, "열기", loadDl.LOAD);
  this.saveDl = new FileDialog(f, "다른 이름으로 저장", saveDl.SAVE);

  this.mb = new MenuBar();
  this.file = new Menu("파일");
  this.newItem = new MenuItem("새로 만들기");
  this.open = new MenuItem("열기");
  this.save = new MenuItem("저장");
  this.saveAs = new MenuItem("다른 이름으로 저장");
  this.setPage = new MenuItem("페이지 설정");
  this.print = new MenuItem("인쇄");
  this.exit = new MenuItem("끝내기");
  this.edit = new Menu("편집");
  this.cancel = new MenuItem("실행 취소");
  this.cut = new MenuItem("잘라내기");
  this.copy = new MenuItem("복사");
  this.paste = new MenuItem("붙여넣기");
  this.delete = new MenuItem("삭제");
  this.search = new MenuItem("찾기");
  this.searchNext = new MenuItem("다음 찾기");
  this.change = new MenuItem("바꾸기");
  this.move = new MenuItem("이동");
  this.selectAll = new MenuItem("모두 선택");
  this.time = new MenuItem("시간/날짜");
  this.form = new Menu("서식");
  this.nextln = new MenuItem("자동 줄 바꿈");
  this.font = new MenuItem("글꼴");
  this.view = new Menu("보기");
  this.statusBar = new MenuItem("상태표시줄");
  this.help = new Menu("도움말");
  this.helpMenu = new MenuItem("도움말 항목");
  this.info = new MenuItem("메모장 정보");

  f.setSize(600, 400);
  this.dim1 = Toolkit.getDefaultToolkit().getScreenSize();
  this.dim2 = f.getSize();
  this.xpos = (int)(dim1.getWidth() / 2 - dim2.getWidth() / 2);
  this.ypos = (int)(dim1.getHeight() / 2 - dim2.getHeight() / 2);

  init();
  start();
  f.setLocation(xpos, ypos);
  f.setVisible(true);

  this.out = null;
  this.in = null;
 }

 public void init(){
  //set TextArea
  pad.setFont(new Font("Fixedsys", Font.PLAIN, 15));
  f.add(pad);

  //set MenuBar
  f.setMenuBar(mb);
  mb.add(file);
  file.add(newItem);
  file.add(open);
  file.add(save);
  file.add(saveAs);
  file.addSeparator();
  file.add(setPage);
  file.add(print);
  file.addSeparator();
  file.add(exit);
  mb.add(edit);
  edit.add(cancel);
  edit.addSeparator();
  edit.add(cut);
  edit.add(copy);
  edit.add(paste);
  edit.add(delete);
  edit.addSeparator();
  edit.add(search);
  edit.add(searchNext);
  edit.add(change);
  edit.add(move);
  edit.addSeparator();
  edit.add(selectAll);
  edit.add(time);
  mb.add(form);
  form.add(nextln);
  form.add(font);
  mb.add(view);
  view.add(statusBar);
  mb.add(help);
  help.add(helpMenu);
  help.addSeparator();
  help.add(info);
 }

 public void start(){
  f.addWindowListener(this);
  newItem.addActionListener(this);
  open.addActionListener(this);
  save.addActionListener(this);
  saveAs.addActionListener(this);
  exit.addActionListener(this);
 }

 public void windowClosing(WindowEvent we){
  f.dispose();
 }

 public void actionPerformed(ActionEvent ae){
  if(ae.getSource() == newItem){
   pad.setText("");
   f.setTitle("제목 없음 - 메모장");
  }else if(ae.getSource() == open){
   loadDl.setVisible(true);
   filePath = loadDl.getDirectory() + loadDl.getFile();
   loadFromFile();
   pad.setText(saveStr);
   saveStr = "";
   f.setTitle(filePath + " - 메모장");
  }else if(ae.getSource() == save){
   saveDl.setVisible(true);
   filePath = saveDl.getDirectory() + saveDl.getFile();
   str = pad.getText();
   saveToFile();
   f.setTitle(filePath + " - 메모장");
  }else if(ae.getSource() == saveAs){
   saveDl.setVisible(true);
   filePath = saveDl.getDirectory() + saveDl.getFile();
   str = pad.getText();
   saveToFile();
   f.setTitle(filePath + " - 메모장");
  }else if(ae.getSource() == exit){
   f.dispose();
  }
 }

 //File I/O
 public void saveToFile(){
  ffile = new File(filePath);

  try{
   out = new PrintWriter(new BufferedWriter(new FileWriter(ffile)));
   out.write(str);
   out.close();
  }catch(Exception e){
   e.printStackTrace();
  }
 }

 public void loadFromFile(){
  ffile = new File(filePath);
  saveStr = "";

  try{
   in = new BufferedReader(new FileReader(ffile));

   while((loadedStr = in.readLine()) != null){
    saveStr += loadedStr + "\n";
   }

   in.close();
  }catch(Exception e){
   e.printStackTrace();
  }
 }

 public static void main(String[] args){
  new SimpleNotePad();
 }
}

 


저작자 표시
신고

티스토리 툴바