我的我的错误,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());
}
}
}
答案 0 :(得分:1)
FileFilter
是abstract
类(这意味着它无法实例化),您必须扩展它。有关FileFilter
的具体实现,请参阅FileNameExtensionFilter
。您可能还想查看tutorial on how to use FileChooser
s which has a section on custom FileFilter
s
如果您使用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");