import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;
public class Test extends JFrame
{
public Test()
{
JFileChooser jfc = new JFileChooser("d:\\");
FileFilter filterJpeg = new FileNameExtensionFilter("Jpeg file", "JPG", "jpeg");
FileFilter filterGif = new FileNameExtensionFilter("Gif file", "gif");
// 移除默认的过滤器
jfc.removeChoosableFileFilter(jfc.getFileFilter());
jfc.addChoosableFileFilter(filterJpeg);
jfc.addChoosableFileFilter(filterGif);
// jfc.showDialog(this, "选择");
jfc.showDialog(this, null);
// jfc.showOpenDialog(this);
// jfc.showSaveDialog(this);
String path = jfc.getSelectedFile().getAbsolutePath();
Icon icon = new ImageIcon(path);
JLabel jl = new JLabel(icon, JLabel.CENTER);
JScrollPane jsp = new JScrollPane(jl);
this.add(jsp);
this.setTitle("图片加载演示");
this.setBounds(100, 100, 640, 540);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main (String[] args)
{
new Test();
}
}