打开一个保存文件的FileDialog,怎么判断鼠标点击的是保存还是取消?

解决方案 »

  1.   

    建议你使用JFileChooser类
    更方便简单 
      

  2.   

    如果点击的是cancel,那么getFile()方法返回类型就是null
    你可以更具这一点来识别
      

  3.   

        import     java.io.*;  
        import     java.awt.*;  
        public     class     FileDialog1{  
        String     path;  
        Frame     f     =     new     Frame("test     for     filedialog");  
        public     FileDialog1(){  
        FileDialog     file     =     new     FileDialog(f,"new",1);  
        file.setVisible(true);  
        path     =     file.getDirectory()     +     file.getFile();  
        System.out.println(path);  
        try{  
        FileInputStream     fin     =     new     FileInputStream("d:/100x30_Logo.gif");  
        FileOutputStream     fout     =     new     FileOutputStream(path);  
        int     c;  
        while((c=fin.read())!=-1){  
        fout.write(c);  
        }  
        fin.close();  
        fout.close();  
        }catch(FileNotFoundException     e){  
        System.out.println("File     Copy1:"     +     e);  
        }catch(IOException     e){  
        System.out.println("File     Copy2:"     +     e);  
        }  
        }  
        public     static     void     main(String     args[]){  
        new     FileDialog1();  
        }  
        }
      

  4.   

    那些东西不用判断的,你用JFileChooser,就不用这么判断。jfc2.setDialogTitle("还原数据库");
    int returnVal=jfc2.showOpenDialog(LoginSys.mymain);
    if(returnVal==jfc2.APPROVE_OPTION){
    int s=JOptionPane.showConfirmDialog(LoginSys.mymain, "你确实要还原数据库么?", "系统提示", JOptionPane.YES_NO_CANCEL_OPTION);
    if(s==JOptionPane.YES_OPTION){
    String s1=jfc2.getSelectedFile().getPath();
    BckResData.ResData(s1);
    }else if(s==JOptionPane.NO_OPTION){
    return;
    }else if(s==JOptionPane.CANCEL_OPTION){
    return;
    }
    }
      

  5.   

    我现在对GUI应该说是比较熟吧,以前参与开发过两个基于J2SE的项目
      

  6.   

    虽然JFileChooser可以不用那么判断,但在性能方面,我觉得还是用FileDialog好,JFileChooser对中文支持不够好,很难看,速度慢,但FileDialog就克服了这个问题