java swing 中的FileDialog

1.FileDialog使用方法:java

 FileDialog fd=new FileDialog(new Frame(),"測試",FileDialog.LOAD);  FilenameFilter ff=new FilenameFilter(){    public boolean accept(File dir, String name) {     if (name.endsWith("jpg")){      return true;     }     return false;    }   };   fd.setFilenameFilter(ff);  fd.setVisible(true);  System.out.println(fd.getDirectory()+fd.getFile());web

但在Windows中FileDialog + FilenameFilter沒法正常工做, jdoc的原註釋爲:Filename filters do not function in Sun's reference implementation for Microsoft Windows.測試

2.FileDialog + FilenameFilter能夠用JFileChooser + javax.swing.filechooser.FileFilter 來代替,jdoc中的例子以下:this

    JFileChooser chooser = new JFileChooser();     // Note: source for ExampleFileFilter can be found in FileChooserDemo,     // under the demo/jfc directory in the Java 2 SDK, Standard Edition.     ExampleFileFilter filter = new ExampleFileFilter();     filter.addExtension("jpg");     filter.addExtension("gif");     filter.setDescription("JPG & GIF Images");     chooser.setFileFilter(filter);     int returnVal = chooser.showOpenDialog(parent);     if(returnVal == JFileChooser.APPROVE_OPTION) {        System.out.println("You chose to open this file: " +             chooser.getSelectedFile().getName());     }spa

轉應用實例:     JFileChooser filechooser = new JFileChooser();//建立文件選擇器     filechooser.setCurrentDirectory(new File("."));//設置當前目錄     filechooser.setAcceptAllFileFilterUsed(false);     //顯示全部文件     filechooser.addChoosableFileFilter(new javax.swing.filechooser.FileFilter() {       public boolean accept(File f) {         return true;       }       public String getDescription() {         return "全部文件(*.*)";       }     });     //顯示JAVA源文件     filechooser.setFileFilter(new javax.swing.filechooser.FileFilter() {       public boolean accept(File f) { //設定可用的文件的後綴名         if(f.getName().endsWith(".java")||f.isDirectory()){           return true;         }         return false;       }       public String getDescription() {         return "JAVA源程序(*.java)";       }     });     //能夠反覆使用setFileFilter方法設置JFileChooser的選擇類型orm

相關文章
相關標籤/搜索