我正在使用 Swing 创建一个图像托盘。它所做的是在文件夹或驱动器中查找所有图像文件,然后将它们添加到实际上是 JPanel 的托盘中。代码如下:
public void findAllPhotos(File f ) {
File[] files = f.listFiles();
for (File file : files) {
if(file.isFile()) {
String path = file.getAbsolutePath();
for(String s : extensions) {
if(path.endsWith(s)) {
addImageToTray(file);
break;
}
}
}
else {
findAllPhotos(file);
}
}
}
void addImageToTray(File fname) {
try {
BufferedImage img = ImageIO.read(fname);
if(img == null) return;
double width = 0, height = 0;
if(iconView) {
width = Math.min(img.getWidth(), iconWidth);
height = Math.min(img.getHeight(), iconHeight);
}
else {
width = Math.min(img.getWidth(), tileWidth);
height = Math.min(img.getHeight(), tileHeight);
}
AffineTransform af = new AffineTransform(AffineTransform.getScaleInstance(width/img.getWidth(), (height/img.getHeight())));
img = new AffineTransformOp(af, AffineTransformOp.TYPE_NEAREST_NEIGHBOR).filter(img, null);
JLabel jl = new JLabel(new ImageIcon(img));
if(iconView) {
jl.setText(" " +fname.getName());
jl.setFont(new java.awt.Font("Microsoft JhengHei UI Light", 1, 11));
}
addActionListner(jl, fname.getAbsolutePath());
jl.setBorder(new BevelBorder(BevelBorder.LOWERED));
tray.add(jl);
} catch (IOException ex) {
JOptionPane.showMessageDialog(null, "unknown problem, with tray creation!!!");
}
}
我的问题是大型文件夹和扫描驱动器时速度很慢。请建议提高速度的方法