我有 ImageIcon 数组列表,它有很多大小的图标,所以我需要将它们设置为特定的 (85*100) 大小。那么我该怎么做呢?我的代码如下:
File file = new File("C://Users/ks/Desktop/DB");
if (file != null) {
File[] files = file.listFiles(new FilenameFilter() {
public boolean accept(File file, String fileName) {
if (fileName.endsWith(".png")) {
return true;
} else {
System.out.println("No jpg files");
return false;
}
}
});
System.out.println("Current dir : " + file.getCanonicalPath());
List<ImageIcon> images = new ArrayList<ImageIcon>();// Array list
for (int fileInList = 0; fileInList < files.length; fileInList++) {
System.out.print(files[fileInList].toString() + " ");
System.out.println("this is forloop" + fileInList);
if (files[fileInList].isFile()) {
images.add(0, new ImageIcon(ImageIO.read(files[fileInList])));// add all imagaes
}
} }
FlowLayout f = new FlowLayout();
JPanel p = new JPanel(f);
f.setAlignment(FlowLayout.LEFT);
p.setSize(700, 100);
for (int x = 0; x < images.size(); x++) {
p.add(new JLabel(images.get(x)));
}
jPanel1.setPreferredSize(p.getSize());
jPanel1.add(p);
jPanel1.revalidate();
jPanel1.repaint();
} else {
System.out.println("No file Found"); // Array index out of bound display insted of this
}
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("Success");