源代码:
package 文件选择器;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.filechooser.FileView;
public class ImageFileView extends FileView {
ImageIcon jpgIcon =shift("E:\\PPT素材库\\通用型素材库\\图标\\位图图标(不可修改)\\填充图标\\文件类型图标\\JPG图标.png");
ImageIcon gifIcon =shift("E:\\PPT素材库\\通用型素材库\\图标\\位图图标(不可修改)\\填充图标\\文件类型图标\\GIF图标.png");
ImageIcon pdfIcon=shift("E:\\PPT素材库\\通用型素材库\\图标\\位图图标(不可修改)\\填充图标\\文件类型图标\\PDF图标.png");
ImageIcon docIcon=shift("E:\\PPT素材库\\通用型素材库\\图标\\位图图标(不可修改)\\填充图标\\文件类型图标\\DOC图标.png");
ImageIcon exeIcon=shift("E:\\PPT素材库\\通用型素材库\\图标\\位图图标(不可修改)\\填充图标\\文件类型图标\\EXE图标.png");
ImageIcon mp3Icon=shift("E:\\PPT素材库\\通用型素材库\\图标\\位图图标(不可修改)\\填充图标\\文件类型图标\\MP3图标.png");
ImageIcon pptIcon=shift("E:\\PPT素材库\\通用型素材库\\图标\\位图图标(不可修改)\\填充图标\\文件类型图标\\PPT图标.png");
ImageIcon rarIcon=shift("E:\\PPT素材库\\通用型素材库\\图标\\位图图标(不可修改)\\填充图标\\文件类型图标\\RAR图标.png");
ImageIcon txtIcon=shift("E:\\PPT素材库\\通用型素材库\\图标\\位图图标(不可修改)\\填充图标\\文件类型图标\\TXT图标.png");
ImageIcon xslIcon=shift("E:\\PPT素材库\\通用型素材库\\图标\\位图图标(不可修改)\\填充图标\\文件类型图标\\XSL图标.png");
ImageIcon zipIcon=shift("E:\\PPT素材库\\通用型素材库\\图标\\位图图标(不可修改)\\填充图标\\文件类型图标\\ZIP图标.png");
ImageIcon pngIcon = shift("E:\\PPT素材库\\通用型素材库\\图标\\位图图标(不可修改)\\填充图标\\文件类型图标\\PNG图标.png");
//把大图标转换为小尺寸的图标
public ImageIcon shift(String imagePath) {
ImageIcon icon=new ImageIcon(imagePath);
Image image=icon.getImage();
Image smallImage=image.getScaledInstance(50, 50, Image.SCALE_AREA_AVERAGING);
ImageIcon smallIcon=new ImageIcon(smallImage);
return smallIcon;
}
public String getName(File f) {
return null;
}
public String getDescription(File f) {
return null;
}
public Boolean isTraversable(File f) {
return null;
}
public String getTypeDescription(File f) {
String name=f.getName();
String extension="."+name.substring(name.lastIndexOf(".")+1);
String type=null;
if(extension!=null) {
if(extension.equals(".jpeg" )||(extension.equals(".jpg"))) {
type="JPEG File";
}else if(extension.equals(".gif")) {
type="GIF File";
}else if(extension.equals(".pdf")) {
type="PDF File";
}else if(extension.equals(".doc")||extension.equals(".docx")) {
type="DOC File";
}else if(extension.equals(".exe")) {
type="EXE File";
}else if(extension.equals(".mp3")) {
type="MP3 File";
}else if(extension.equals(".ppt")) {
type="PPT File";
}else if(extension.equals(".rar")) {
type="RAR File";
}else if(extension.equals(".txt")) {
type="TXT File";
}else if(extension.equals(".xsl")||extension.equals(".xslx")) {
type="XSL File";
}else if(extension.equals(".zip")) {
type="ZIP File";
}else if(extension.equals(".png")) {
type="PNG File";
}
}
return type;
}
public Icon getIcon(File f) {
String name=f.getName();
String extension="."+name.substring(name.lastIndexOf(".")+1);
Icon icon=null;
if(extension!=null) {
if(extension.equals(".jpeg")||(extension.equals(".jpg"))){
icon=jpgIcon;
}else if(extension.equals(".gif")) {
icon=gifIcon;
}else if(extension.equals(".pdf")) {
icon=pdfIcon;
}else if(extension.equals(".doc")||extension.equals(".docx")) {
icon=docIcon;
}else if(extension.equals(".exe")) {
icon=exeIcon;
}else if(extension.equals(".mp3")) {
icon=mp3Icon;
}else if(extension.equals(".ppt")) {
icon=pptIcon;
}else if(extension.equals(".rar")) {
icon=rarIcon;
}else if(extension.equals(".txt")) {
icon=txtIcon;
}else if(extension.equals(".xsl")||extension.equals(".xslx")) {
icon=xslIcon;
}else if(extension.equals(".zip")) {
icon=zipIcon;
}else if(extension.equals(".png")) {
icon=pngIcon;
}
}
return icon;
}
public static void main(String[] args) {
JFrame jf=new JFrame();
jf.setLocation(200, 200);
jf.setSize(400,400);
jf.setLayout(null);
JButton jb=new JButton("打开文件");
jb.setBounds(100, 100, 100, 40);
jf.add(jb);
jb.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JFileChooser jfc=new JFileChooser();
ImageFileView jfv=new ImageFileView();
jfc.setFileView(jfv);
jfc.showOpenDialog(null);
}
});
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setVisible(true);
}
}