我正在尝试使 xpic 等于 vpic,就像下面的示例一样。当我尝试编译此代码时出现错误:“局部变量 xpic 可能尚未初始化”
ImageIcon xpic;
ImageIcon vpic;
vpic = new ImageIcon(getClass().getResource("Images/picture.png"));
vpic = xpic;
最佳答案
我认为您有错字,因为您的代码设置了 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;
关于Java:将一个ImageIcon设置为等于另一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24582046/