【发布时间】:2015-03-28 15:24:17
【问题描述】:
好吧,我在编码方面几乎是全新的,但我正在努力。
我需要创建一个 ImageIcon,它将使用 switch 语句来确定要显示的图像。
我看了这里的帖子:Change image with if statement
它建议使用 switch 语句而不是 if 语句,所以我认为它会有所帮助。但是,当我编辑我的代码并从以前的代码中更改它时,我收到一条错误消息,提示“无法将 ImageIcon 解析为变量”。我尝试了各种大写组合,但没有一个有效。我将最初拥有的代码留在了第一种情况下。该代码没有给我任何错误消息,但我认为我不应该为每种情况创建一个新的 ImageIcon,因为我需要从程序中的一个中提取并让 switch 语句确定要显示的图像。
修改后的代码
public ImageIcon dieImage(String string)
{
ImageIcon dieImage = new ImageIcon("");
switch (faceValue){
case 1: dieImage = new ImageIcon ("src/1.jpg");
break;
case 2: dieImage = new ImageIcon("src/2.jpg");
break;
case 3: dieImage = new ImageIcon("src/3.jpg");
break;
case 4: dieImage = new ImageIcon("src/4.jpg");
break;
case 5: dieImage = new ImageIcon("src/5.jpg");
break;
case 6: dieImage = new ImageIcon("src/6.jpg");
break;
}
return dieImage;
}
}
任何帮助将不胜感激。
【问题讨论】:
标签: java eclipse image switch-statement