参见JDK自己的例子: C:\j2sdk1.4.2_02\demo\jfc\FileChooserDemo 其中部分代码: 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()); }
C:\j2sdk1.4.2_02\demo\jfc\FileChooserDemo
其中部分代码:
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());
}
JPEG Images (.jpg)
GIF Images (.gif)