我真的不知道我做错了什么。我正在制作一款状态游戏,所以我可以帮助人们学习,但是当我运行它时它可以工作,但它不会将图像返回到屏幕上。这是代码的主要位,我有我的阵列...我不断收到一个字符串返回null ...
public void checkCorrectValue(String guess){
if(guess.equalsIgnoreCase(current_state)){
correct_value = true;
}else{
correct_value = false;
}
}
public void setRandomState(){
r = new Random();
int i;
i = r.nextInt(50);
System.out.println(i);
current_state = states[i];
System.out.println(current_state + " was randomly selected");
}
public String getCurrentState(){
return current_state;
}
public boolean isStateCorrect(){
return correct_value;
}
我添加了我的数组称为状态并填充所有状态。它确实成功输出正确的状态,所以我知道这个工作。这是我的面板代码...
States_Images us_img;
USA_States us_state;
public Menu(){
setLayout(null);
setBackground(Color.WHITE);
us_img = new States_Images();
us_state = new USA_States();
us_state.setRandomState();
System.out.println(us_state.getCurrentState());
JLabel l = new JLabel();
l.setIcon(us_img.getCurrentStateImage());
l.setBounds(0,0,500,500);
add(l);
}
它所做的就是把我的东西面板然后尝试了通过将随机状态首先调用setRandomState()把图像,它出来把正确那里。但是当us_img.getCurrentStateImage()运行时...
private ImageIcon currentstate_image;
private String current_state;
USA_States us_states;
public States_Images(){
us_states = new USA_States();
}
public ImageIcon getCurrentStateImage(){
setStateName();
currentstate_image = new ImageIcon("graphics\\" + current_state + ".png");
System.out.println(current_state + " image loaded");
return currentstate_image;
}
private void setStateName(){
current_state = us_states.getCurrentState();
}
它打印出“null image loaded”。然后在我的框架上没有任何显示我真的不知道我做错了什么,因为我以前做过这样的事情。我知道这是基本的Java所以任何帮助将真正appriciated!提前致谢。
这取决于图像的存储位置。从你的代码中,它说图像存储在磁盘上的'。\ graphics \ {name} .png'上。 1-它们存在吗? 2-他们是否想嵌入? – MadProgrammer 2013-02-19 04:34:20
我知道图像存在,因为我已经在菜单代码中将它们“手动”直接称为JLabel。所以我知道图像可以加载和显示。 – tyty5949 2013-02-19 04:41:17
然后,我会确保'String'' current_state'与图像名称的匹配,并且变量设置正确 – MadProgrammer 2013-02-19 05:04:47