Java 是否有效更新JLabel ArrayList的所有图标?
因此,我想更新JLabel ArrayList的每个图标,基于外部ArrayList将其更新为自己的单个图标。外部ArrayList称为board,JLabel ArrayList称为tiles。我想将每个图标更新到板中相应的项目,我如何才能有效地做到这一点?我需要它来更新一个巨大列表中的每个图标,我们谈论的是100-200个项目。现在,100个项目的长度大约需要4秒钟才能同时显示所有图标:Java 是否有效更新JLabel ArrayList的所有图标?,java,performance,jlabel,imageicon,Java,Performance,Jlabel,Imageicon,因此,我想更新JLabel ArrayList的每个图标,基于外部ArrayList将其更新为自己的单个图标。外部ArrayList称为board,JLabel ArrayList称为tiles。我想将每个图标更新到板中相应的项目,我如何才能有效地做到这一点?我需要它来更新一个巨大列表中的每个图标,我们谈论的是100-200个项目。现在,100个项目的长度大约需要4秒钟才能同时显示所有图标: for (int i = 0; i < board.size(); i++) { //th
for (int i = 0; i < board.size(); i++) {
//this is all one line:
tiles.get(i).setIcon(resizeIcon(displayTile(board.get(i),
tiles.get(i).getHeight(), tiles.get(i).getHeight()));
}
//this resizes the ImageIcon and returns the risized icon:
public static ImageIcon resizeIcon(ImageIcon i, int x, int y) {
Image image = i.getImage();
Image newimg = image.getScaledInstance(x, y, java.awt.Image.SCALE_SMOOTH);
i = new ImageIcon(newimg);
return i;
}
//this returns an ImageIcon based off of the number that is put in:
public static ImageIcon displayTile(int num) {
if (num != 0) {
ImageIcon i = new ImageIcon("src/resources/" + num + ".png");
return i;
}
ImageIcon i = new ImageIcon("src/resources/0.png");
return i;
}
for(int i=0;i
如何提高效率?将有多少个不同的
num
s?是否可以缓存每个可能性的displayTile(num)
结果?将有多少个不同的大小?如果所有文件大小相同,是否可以缓存缓存displayTile(num)的大小调整结果
?NUM会改变,但我可能会有公共大小的图标,只要访问它们,而不是每次都要调整它们的大小,如果你这么说的话。@Andy Turner dude谢谢!它可以工作,而且速度更快!!