2016. 09. 06. Tue. 스물한 번째 수업
오늘의 수업내용
- ActionListener를 이용해 메뉴 인식하기
- File Dialog를 이용해 File Open
- File Dialog를 이용해 File Save
예제(메모장 만들기)
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 | package com.javalesson.ch20awt; import java.awt.FileDialog; import java.awt.Font; import java.awt.Frame; import java.awt.Menu; import java.awt.MenuBar; import java.awt.MenuItem; import java.awt.TextArea; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.StringWriter; class TextEditor extends Frame{ TextArea ta; MenuBar mb; Menu menu; Menu menu1; Menu menu2; Menu menu3; Menu menu4; MenuItem miNew, miSaveAs, miOpen, miExit; String fileName; Font f1 = new Font("serif", Font.PLAIN, 20); public TextEditor(String title){ super(title); ta = new TextArea(); mb = new MenuBar(); menu = new Menu("file"); menu1 = new Menu("edit"); menu2 = new Menu("form"); menu3 = new Menu("view"); menu4 = new Menu("help"); miNew = new MenuItem("new"); miSaveAs = new MenuItem("save"); miOpen = new MenuItem("open"); miExit = new MenuItem("exit"); ActionListener mh = new MenuHandler(); miNew.addActionListener(mh); miSaveAs.addActionListener(mh); miOpen.addActionListener(mh); miExit.addActionListener(mh); menu.add(miNew); menu.add(miOpen); menu.add(miSaveAs); menu.addSeparator(); menu.add(miExit); mb.add(menu); mb.add(menu1); mb.add(menu2); mb.add(menu3); mb.add(menu4); this.setMenuBar(mb); this.add(ta); ta.setFont(f1); this.setBounds(550, 300, 500, 400); this.setVisible(true); this.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e) { e.getWindow().dispose();} }); } public void fileOpen(String fileName) {//ch.17.L04SerialInput 참고 this.fileName = fileName; FileReader fr; BufferedReader br=null; StringWriter sw; //sw.write(br.readline()); //ta.setText(sw); try{ fr = new FileReader(fileName); br = new BufferedReader(fr); sw = new StringWriter(); String line; while ((line = br.readLine()) != null) { sw.write(line+"\n"); }//문제해결 br.close(); ta.setText(sw.toString()); }catch(IOException e){e.printStackTrace();} } public void saveAs(String fileName){//ch.17.L04SerialOut 참고 FileWriter fw; BufferedWriter bw; try{ fw = new FileWriter(fileName); bw = new BufferedWriter(fw); bw.write(ta.getText()); bw.close(); }catch(IOException e){e.printStackTrace();} } class MenuHandler implements ActionListener{ @Override public void actionPerformed(ActionEvent e) { String emd = e.getActionCommand(); if(emd.equals("new")){ ta.setText(""); }else if(emd.equals("open")){ FileDialog fd = new FileDialog(TextEditor.this, "file open"); fd.setVisible(true); fileName = fd.getDirectory()+fd.getFile(); fileOpen(fileName); }else if(emd.equals("save")){ FileDialog fd = new FileDialog(TextEditor.this, "save as", FileDialog.SAVE); fd.setVisible(true); fileName = fd.getDirectory()+fd.getFile(); saveAs(fileName); }else if(emd.equals("exit")){ System.exit(0); } } } } public class L03TextEditor { public static void main(String[] args) { new TextEditor("메모장"); }//main }//class | cs |
※출력내용
오픈과 저장 기능을 넣으면서 메모장에 어떻게 구현되는지 이해할 수 있었던 수업이었다.
'IT story > Java' 카테고리의 다른 글
D+23 네트워크통신(Tcp/Ip Server) (0) | 2016.09.08 |
---|---|
D+22 네트워크통신 (Tcp/Ip Client) (0) | 2016.09.08 |
D+20 awt(text & text editor) (0) | 2016.09.06 |
D+19 Thread(쓰레드) (0) | 2016.09.01 |
D+18 InnerClass (0) | 2016.08.31 |