zoukankan      html  css  js  c++  java
  • java swing 中的FileDialog

    1.FileDialog使用方法:

     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());

    但在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中的例子如下:

        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());     }

    转应用实例:     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的选择类型

     

  • 相关阅读:
    104.Maximum Depth of Binary Tree
    103.Binary Tree Zigzag Level Order Traversal
    102.Binary Tree Level Order Traversal
    101.Symmetric Tree
    100.Same Tree
    99.Recover Binary Search Tree
    98.Validate Binary Search Tree
    97.Interleaving String
    static静态初始化块
    serialVersionUID作用
  • 原文地址:https://www.cnblogs.com/huangyibo/p/3670282.html
Copyright © 2011-2022 走看看