黑马程序员技术交流社区

标题: 简单记事本程序遇到问题了 [打印本页]

作者: ò壞尛孩    时间: 2014-5-8 23:17
标题: 简单记事本程序遇到问题了
本帖最后由 ò壞尛孩 于 2014-5-8 23:20 编辑
  1.     <div class="blockcode"><blockquote>package com.itheima;

  2. import java.awt.FileDialog;
  3. import java.awt.Frame;
  4. import java.awt.Menu;
  5. import java.awt.MenuBar;
  6. import java.awt.MenuItem;
  7. import java.awt.TextArea;
  8. import java.awt.event.ActionEvent;
  9. import java.awt.event.ActionListener;
  10. import java.awt.event.WindowAdapter;
  11. import java.awt.event.WindowEvent;
  12. import java.io.BufferedReader;
  13. import java.io.BufferedWriter;
  14. import java.io.File;
  15. import java.io.FileReader;
  16. import java.io.FileWriter;
  17. import java.io.IOException;

  18. public class MyMenuTest {
  19.         private Frame f=null;
  20.         private TextArea ta=null;
  21.         private FileDialog fileOpenDia=null;
  22.         private FileDialog fileSaveDia=null;
  23.         private MenuBar mb=null;
  24.         private Menu fileMenu=null;
  25.         private MenuItem openItem=null;
  26.         private MenuItem saveItem=null;
  27.         private MenuItem quitItem=null;
  28.         private File file;
  29.        
  30.         MyMenuTest(){
  31.                 init();
  32.         }
  33.        
  34.         private void init(){
  35.                 f=new Frame("My First Program of Text");
  36.                 mb=new MenuBar();
  37.                 fileMenu=new Menu("文件");
  38.                 openItem=new MenuItem("打开");
  39.                 saveItem=new MenuItem("保存");
  40.                 quitItem=new MenuItem("退出");
  41.                 ta=new TextArea();
  42.                
  43.                 f.setBounds(300, 200, 800, 600);
  44.                 fileMenu.add(openItem);
  45.                 fileMenu.add(saveItem);
  46.                 fileMenu.add(quitItem);
  47.                 mb.add(fileMenu);
  48.                
  49.                 f.setMenuBar(mb);
  50.                 f.add(ta);
  51.                
  52.                 fileOpenDia=new FileDialog(f,"打开文件",FileDialog.LOAD);
  53.                 fileSaveDia=new FileDialog(f,"保存文件",FileDialog.SAVE);
  54.                
  55.                 myEvent();
  56.                 f.setVisible(true);
  57.                
  58.                
  59.                
  60.         }
  61.         private void myEvent()
  62.         {
  63.                 f.addWindowListener(new WindowAdapter(){
  64.                         @Override
  65.                         public void windowClosing(WindowEvent e){
  66.                                 System.exit(0);
  67.                         }
  68.                 });
  69.                 quitItem.addActionListener(new ActionListener(){
  70.                         @Override
  71.                         public void actionPerformed(ActionEvent e) {
  72.                                 System.exit(0);
  73.                         }
  74.                 });
  75.                 saveItem.addActionListener(new ActionListener(){
  76.                         String dirPath=null;
  77.                         String fileName=null;       
  78.                         BufferedWriter bufw=null;
  79.                         @Override
  80.                         public void actionPerformed(ActionEvent e) {
  81.                                 if(file==null){
  82.                                         fileSaveDia.setVisible(true);
  83.                                         dirPath=fileSaveDia.getDirectory();
  84.                                         fileName=fileSaveDia.getFile();
  85.                                         if(dirPath==null||fileName==null)
  86.                                                 return;
  87.                                         file=new File(dirPath,fileName);
  88.                                         System.out.println(dirPath+fileName);
  89.                                 }
  90.                                 try {
  91.                                         bufw=new BufferedWriter(new FileWriter(file));
  92.                                         String text=ta.getText();
  93.                                         bufw.write(text);
  94.                                         bufw.flush();
  95.                                 } catch (IOException e1) {
  96.                                         // TODO Auto-generated catch block
  97.                                         e1.printStackTrace();
  98.                                 }
  99.                                 finally{
  100.                                         if(bufw!=null){
  101.                                                 try {
  102.                                                         bufw.close();
  103.                                                 } catch (Exception e2) {
  104.                                                         // TODO: handle exception
  105.                                                         e2.printStackTrace();
  106.                                                 }
  107.                                         }
  108.                                 }
  109.                         }               
  110.                 });
  111.                 openItem.addActionListener(new ActionListener(){
  112.                         String dirPath=null;
  113.                         String fileName=null;       
  114.                         BufferedReader bufr=null;
  115.                         @Override
  116.                         public void actionPerformed(ActionEvent e){
  117.                                 fileOpenDia.setVisible(true);
  118.                                 dirPath=fileOpenDia.getDirectory();
  119.                                 fileName=fileOpenDia.getFile();
  120.                                 if(dirPath==null||fileName==null)
  121.                                         return;
  122.                                 ta.setText("");
  123.                                 file=new File(dirPath,fileName);
  124.                                 System.out.println(dirPath+fileName);
  125.                                 try {
  126.                                         bufr=new BufferedReader(new FileReader(file));
  127.                                         String line=null;
  128.                                         while((line=bufr.readLine())!=null){
  129.                                                 ta.append(line+"\r\n");
  130.                                         }
  131.                                 } catch (IOException ex) {
  132.                                         ex.printStackTrace();
  133.                                 }
  134.                                 finally{
  135.                                         if(bufr!=null){
  136.                                                 try {
  137.                                                         bufr.close();
  138.                                                 } catch (Exception e2) {
  139.                                                         // TODO: handle exception
  140.                                                         e2.printStackTrace();
  141.                                                 }
  142.                                         }
  143.                                 }
  144.                         }
  145.                 });
  146.                 saveItem.addActionListener(new ActionListener(){
  147.                         @Override
  148.                         public void actionPerformed(ActionEvent e){
  149.                                 fileSaveDia.setVisible(true);
  150.                         }
  151.                 });
  152.         }
  153.         public static void main(String[] args) {
  154.                 // TODO Auto-generated method stub
  155.                 new MyMenuTest();
  156.         }

  157. }
复制代码

这里红色代码区!如果文件已经存在 ,或者用打开按钮打开一个文件,那么这个 private File file;绝对不是空的!
可是这里运行的时候为什么总是认为file是空的,总是打开存储对话框!????????
存储的时候 如果文件存在是不打开存储对话框的!求解?怎么回事???

作者: 曹冬明    时间: 2014-5-8 23:44
你的myEvent方法中的saveItem监听添加了两次,每次点保存的时候都是有两个保存窗口,下面那个监听器不管你文件存不存在都会显示出来
作者: ò壞尛孩    时间: 2014-5-9 23:44
曹冬明 发表于 2014-5-8 23:44
你的myEvent方法中的saveItem监听添加了两次,每次点保存的时候都是有两个保存窗口,下面那个监听器不管你 ...

谢谢!  我已经找出问题了! 敲代码的时候  果断不能这个没敲好 去敲别的!
作者: 崔伟明    时间: 2014-11-29 19:30
我都看晕了!




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2