http://www.cnblogs.com/dyllove98/archive/2012/03/05/2461895.html
package swing.choose; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; import javax.swing.JButton; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.filechooser.FileFilter; import javax.swing.filechooser.FileNameExtensionFilter; public class JFileChooseDemo extends JFrame { private static final long serialVersionUID = 1L; private JFileChooser fileChooser = new JFileChooser();; public JFileChooseDemo() { JButton button = new JButton("Click"); button.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { int result = fileChooser.showOpenDialog(JFileChooseDemo.this); if (result == JFileChooser.CANCEL_OPTION) { return; } File chooseFile = fileChooser.getSelectedFile(); add(new JLabel("<html><font color=blue>" + chooseFile.getAbsolutePath())); validate(); } }); this.add(button); this.setLayout(new FlowLayout()); this.setSize(400, 200); this.setDefaultCloseOperation(DISPOSE_ON_CLOSE); this.setLocationRelativeTo(null); this.setVisible(true); } public static void main(String[] args) { JFileChooseDemo demo = new JFileChooseDemo(); demo.fileChooser.setSelectedFile(new File("test.xls")); // 设置文件过滤器 // demo.fileChooser.setFileFilter(new FileNameExtensionFilter("Description", "gif", "jpg", "bmp")); demo.fileChooser.setFileFilter(new FileFilter() { @Override public String getDescription() { return "MS-Excel 2003 文件(.xls)|快捷方式(.lnk)"; } @Override public boolean accept(File f) { if (f.isDirectory()) { return true; } return f.getName().toLowerCase().endsWith("") || f.getName().toLowerCase().endsWith(".lnk"); } }); } }