import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.filechooser.*;
class GifFilter implements javax.swing.filechooser.FileFilter
{
public booleam accept(File file)
{
return file.getName().toLowerCase().endsWith(".gif")||file.isDirectory;
}
public String getDescription()
{
return "GIF Image";
}
}
public class FileChooserDemo extends JFrame
{
static private final String newline="\n";
public FileChooserDemo()
{
super("FileChooserDemo");
final JTextArea log=new JTextArea(5,20);
log.setEditable(false);
JScrollPane logScrollPane=new JScrollPane(log);
final JFileChooser fc=new JFileChooser();
fc.setCurrentDirectory(new File("."));
fc.setFileFilter(new GifFilter());
ImageIcon openIcon=new ImageIcon("images/open.gif");
JButton openButton=new JButton("Open a File...",openIcon);
openButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
int returnVal=fc.showOpenDialog(FileChooserDemo.this);
if(returnVal==JFileChooser.APPROVE_OPTION){
File file=fc.getSelectedFile();
log.append("opening:"+file.getName()+"."+newline);
}
else{log.append("open command cancelled by user."+newline);}
}
});
ImageIcon saveIcon=new ImageIcon("images/save.gif");
JButton saveButton=new JButton("Save a File...",saveIcon);
saveButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
int returnVal=fc.showSaveDialog(FileChooserDemo.this);
if(returnVal==JFileChooser.APPROVE_OPTION){
File file=fc.getSelectedFile();
log.append("saving:"+file.getName()+"."+newline);
}
else{log.append("save command cancelled by user."+newline);}
}
});
JPanel buttonPanel=new JPanel();
buttonPanel.add(openButton);
buttonPanel.add(saveButton);
openButton.setNextFocusableComponent(saveButton);
saveButton.setNextFocusableComponent(openButton);add(buttonPanel,BorderLayout.NORTH);
add(logScrollPane,BorderLayout.CENTER);
}
public static void main(String args[])
{
JFrame frame=new FileChooserDemo();
frame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
frame.pack();
frame.setVisible(true);
}
}

解决方案 »

  1.   

    setfilefilter无法使用,求高手指点,很急啊
      

  2.   

    import java.io.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.filechooser.FileNameExtensionFilter;public class FileChooserDemo extends JFrame {
    static private final String newline = "\n"; public FileChooserDemo() {
    super("FileChooserDemo");
    final JTextArea log = new JTextArea(5, 20);
    log.setEditable(false);
    JScrollPane logScrollPane = new JScrollPane(log);
    final JFileChooser fc = new JFileChooser();
    fc.setCurrentDirectory(new File("."));

    fc.removeChoosableFileFilter(fc.getChoosableFileFilters()[0]);
    fc.addChoosableFileFilter(new FileNameExtensionFilter("GIF Image", "gif"));

    ImageIcon openIcon = new ImageIcon("images/open.gif");
    JButton openButton = new JButton("Open a File...", openIcon);
    openButton.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    int returnVal = fc.showOpenDialog(FileChooserDemo.this);
    if (returnVal == JFileChooser.APPROVE_OPTION) {
    File file = fc.getSelectedFile();
    log.append("opening:" + file.getName() + "." + newline);
    } else {
    log.append("open command cancelled by user." + newline);
    }
    }
    });
    ImageIcon saveIcon = new ImageIcon("images/save.gif");
    JButton saveButton = new JButton("Save a File...", saveIcon);
    saveButton.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    int returnVal = fc.showSaveDialog(FileChooserDemo.this);
    if (returnVal == JFileChooser.APPROVE_OPTION) {
    File file = fc.getSelectedFile();
    log.append("saving:" + file.getName() + "." + newline);
    } else {
    log.append("save command cancelled by user." + newline);
    }
    }
    });
    JPanel buttonPanel = new JPanel();
    buttonPanel.add(openButton);
    buttonPanel.add(saveButton); add(buttonPanel, BorderLayout.NORTH);
    add(logScrollPane, BorderLayout.CENTER);
    } public static void main(String args[]) {
    JFrame frame = new FileChooserDemo();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);
    }
    }
      

  3.   

    class GifFilter extends javax.swing.filechooser.FileFilter
        {
            public boolean accept(File file)
            {
                return file.getName().toLowerCase().endsWith(".gif")||file.isDirectory();
            }
            public String getDescription()
            {
                return "GIF Image";
            }
        }