A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

本帖最后由 HM朱蛟 于 2013-4-29 09:17 编辑

我的思路在截图上,求大神指点啊,帮忙看看我这样理解是否妥当呢?小弟先谢了{:soso_e100:}

这是代码:
  1. /*
  2. 需求:在文本区域内输入一些字符,点保存后能存到硬盘上,
  3. 注意若文件存在不弹出对话框直接覆盖,若文件不存在直接弹出对话框。

  4. 在原有程序的基础上监听保存条目

  5. */

  6. import java.awt.*;
  7. import java.awt.event.*;
  8. import java.io.*;

  9. class Run
  10. {
  11. private Frame frame=null;
  12. private Menu menu=null,submenu=null;
  13. private MenuItem closeitem =null,subitem=null,openitem=null,saveitem=null;
  14. private MenuBar bar =null;
  15. private TextArea ta= null;
  16. private FileDialog openDia,saveDia;

  17. private File file =null;

  18. Run()
  19. {
  20. init();
  21. }

  22. private void init()
  23. {
  24. frame = new Frame("我的窗");
  25. menu = new Menu("文件菜单");
  26. submenu = new Menu("子菜单");
  27. closeitem = new MenuItem("关闭条目");
  28. subitem = new MenuItem("子条目");
  29. openitem = new MenuItem("打开条目");
  30. saveitem = new MenuItem("保存条目");
  31. bar = new MenuBar();
  32. ta = new TextArea();

  33. openDia = new FileDialog(frame,"我要打开",FileDialog.LOAD);
  34. saveDia = new FileDialog(frame,"我要保存",FileDialog.SAVE);//保存模式

  35. frame.setMenuBar(bar);
  36. frame.add(ta);
  37. bar.add(menu);
  38. menu.add(openitem);
  39. menu.add(saveitem);
  40. menu.add(submenu);
  41. submenu.add(subitem);
  42. menu.add(closeitem);

  43. frame.setBounds(500,50,500,650);
  44. frame.setVisible(true);

  45. myEvent();
  46. }

  47. private void myEvent()
  48. {
  49. //保存
  50. saveitem.addActionListener(new ActionListener(){

  51. public void actionPerformed(ActionEvent ae)
  52. {
  53. if(file==null)//如果为空就弹出对话框,并将来当前文本区域内的文件封装成对象
  54. {
  55. saveDia.setVisible(true);

  56. String dirPath = saveDia.getDirectory();
  57. String fileName = saveDia.getFile();
  58. /*
  59. System.out.println("dirPath:"+dirPath); // dirPath:C:\Documents and Settings\Administrator\桌面\
  60. System.out.println("getFile:"+file); // getFile:null
  61. */
  62. if (dirPath == null || fileName ==null)
  63. return;
  64. //有 null
  65. file = new File(dirPath,fileName);
  66. }

  67. try
  68. {
  69. BufferedWriter bfw = new BufferedWriter(new FileWriter(file));
  70. String text = ta.getText();//获取输入内容
  71. bfw.write(text);
  72. bfw.close();
  73. }
  74. catch (Exception asd)
  75. {
  76. throw new RuntimeException();
  77. }
  78. }
  79. });

  80. //打开
  81. openitem.addActionListener(new ActionListener(){

  82. public void actionPerformed(ActionEvent ae)
  83. {
  84. openDia.setVisible(true);
  85. String dirPath = openDia.getDirectory();
  86. String fileName = openDia.getFile();

  87. if(dirPath==null || fileName==null)
  88. return;

  89. ta.setText("");

  90. file = new File(dirPath,fileName);

  91. try
  92. {
  93. BufferedReader br = new BufferedReader(new FileReader(file));
  94. String text=null;
  95. while((text=br.readLine())!=null)
  96. ta.append(text+"\r\n");
  97. }
  98. catch (IOException e)
  99. {
  100. throw new RuntimeException("读取失败");
  101. }
  102. }
  103. });

  104. closeitem.addActionListener(new ActionListener(){

  105. public void actionPerformed(ActionEvent ae)
  106. {
  107. System.exit(0);
  108. }
  109. });

  110. frame.addWindowListener(new WindowAdapter(){

  111. public void windowClosing(WindowEvent we)
  112. {
  113. System.exit(0);
  114. }
  115. });
  116. }

  117. public static void main(String[] args)
  118. {
  119. new Run();
  120. }
  121. }
复制代码

think.png (30.52 KB, 下载次数: 0)

think.png

评分

参与人数 1技术分 +1 收起 理由
田磊阳 + 1

查看全部评分

2 个回复

倒序浏览
哎  又看了遍  我犯二了 测试代码写错了  74行   fileName写成了file
改对后出现的状况是要么是2个空 要么是name和path都有
不过根据测试结果来看 最终结果应该是我想的那样 因为只有在保存的那个时候在文本框里输入了文件名
不好意思
回复 使用道具 举报

回帖奖励 +2

学习了:)
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马