我的日食检测到错误

时间:2011-05-04 03:12:42

标签: swing filefilter

我的我的错误,FileFilter说它无法实例化

import javax.swing.*; 
import javax.swing.filechooser.FileFilter;

import java.io.File;    

public class fileChooser2 extends Object {
public fileChooser2() {
JFileChooser chooser = new JFileChooser();
   // Note: source for ExampleFileFilter can be found in FileChooserDemo,
   // under the demo/jfc directory in the JDK.
   FileFilter filter = new FileFilter();
   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());


}
}
}

1 个答案:

答案 0 :(得分:1)

FileFilterabstract类(这意味着它无法实例化),您必须扩展它。有关FileFilter的具体实现,请参阅FileNameExtensionFilter。您可能还想查看tutorial on how to use FileChoosers which has a section on custom FileFilters


如果您使用FileNameExtensionFilter,则可以更改此

FileFilter filter = new FileFilter();
filter.addExtension("jpg");
filter.addExtension("gif");
filter.setDescription("JPG & GIF Images");

到这个

FileNameExtensionFilter filter = new FileNameExtensionFilter("JPG & GIF Images", "jpg", "gif");