fork download
  1. import java.io.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4.  
  5. public class TextMemo {
  6. public static void main(String args[]) {
  7. TextFrame tf = new TextFrame();
  8. }
  9. }
  10.  
  11. class TextFrame implements ActionListener{
  12. Frame frm;
  13. MenuBar mb;
  14. Menu menu1;
  15. MenuItem mi1,mi2,mi3;
  16.  
  17. public TextFrame(){
  18. frm = new Frame("メモ帳");
  19. mb = new MenuBar();
  20. frm.setMenuBar(mb);
  21. menu1 = new Menu("メニュー");
  22. mi1 = new MenuItem("新規");
  23. menu1.add(mi1);
  24. mi1.addActionListener(this);
  25. mi2 = new MenuItem("読込");
  26. menu1.add(mi2);
  27. mi2.addActionListener(this);
  28. mi3 = new MenuItem("保存");
  29. menu1.add(mi3);
  30. mi3.addActionListener(this);
  31. mb.add(menu1);
  32. ta = new TextArea();
  33. frm.add(ta);
  34.  
  35. frm.setSize(400, 200);
  36. frm.setVisible(true);
  37.  
  38. windowexit();
  39. }
  40.  
  41. public void actionPerformed(ActionEvent e){
  42. new FileIO(e, this);
  43. }
  44.  
  45. private void windowexit(){
  46. frm.addWindowListener(new WindowAdapter() {
  47. public void windowClosing(WindowEvent e){
  48. System.exit(0);
  49. }
  50. });
  51. }
  52. }
  53.  
  54. class FileIO {
  55. TextFrame frame;
  56. Frame frmd = new Frame();
  57. String fileName;
  58. public FileIO(ActionEvent s, TextFrame frame) {
  59. if(s.getSource() == frame.mi1) {
  60. frame.ta.setText("");
  61. } else if(s.getSource() == frame.mi2) {
  62. fd = new FileDialog(frmd, "開く", FileDialog.LOAD);
  63. fd.setVisible(true);
  64. fileName = fd.getDirectory() + fd.getFile();
  65. File file = new File(fileName);
  66. BufferedReader br = null;
  67. try {
  68. br = new BufferedReader(new FileReader(file));
  69. for(String line; (line=br.readLine()) != null; ) {
  70. frame.ta.append(line + "\n");
  71. }
  72. } catch(Exception ex) {
  73. ex.printStackTrace();
  74. } finally {
  75. if(br != null) { try { br.close(); } catch(Exception ex) {} }
  76. }
  77. } else if(s.getSource() == frame.mi3) {
  78. fd = new FileDialog(frmd, "保存", FileDialog.SAVE);
  79. fd.setVisible(true);
  80. fileName = fd.getDirectory() + fd.getFile();
  81. File file = new File(fileName);
  82. BufferedWriter bw = null;
  83. try {
  84. bw = new BufferedWriter(new FileWriter(file));
  85. bw.write(frame.ta.getText());
  86. bw.flush();
  87. } catch(Exception ex) {
  88. ex.printStackTrace();
  89. } finally {
  90. if(bw != null) { try { bw.close(); } catch(Exception ex) {} }
  91. }
  92. }
  93. }
  94. }
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty