【问题标题】:Java: Setting one ImageIcon equal to anotherJava:设置一个 ImageIcon 等于另一个
【发布时间】:2014-08-26 06:00:42
【问题描述】:
我正在尝试使 xpic 等于 vpic ,就像在下面的示例中一样。当我尝试编译此代码时,出现错误:“局部变量 xpic 可能尚未初始化”
ImageIcon xpic;
ImageIcon vpic;
vpic = new ImageIcon(getClass().getResource("Images/picture.png"));
vpic = xpic;
【问题讨论】:
-
您遇到了什么错误? "It doesn't seem to work" 是什么意思? "... to be able to inherit the same arguments as other ImageIcon variables." 是什么意思?请考虑editing your question,如果您这样做,请考虑到我们的观点:那些不知道您要做什么、您的代码是什么样子以及您可能遇到什么问题的人。
标签:
java
variables
arguments
imageicon
【解决方案1】:
我认为您有一个错字,因为您的代码设置了 vpic 变量的引用,然后完全忽略了您设置的内容,并尝试将其设置为 xpic(可能是 null 引用)。
本质上,你所做的就是这样的:
// both Strings are null
String str1;
String str2;
// assign a String object to str1:
str1 = "Hello";
// but then ignore and in fact discard the String object, and
// re-set str1 to null by assigning it str2
str1 = str2; //????
你可能想改变
vpic = new ImageIcon(getClass().getResource("Images/picture.png"));
vpic = xpic;
到
vpic = new ImageIcon(getClass().getResource("Images/picture.png"));
xpic = vpic;