我真的不知道我做錯了什麼。我正在製作一款狀態遊戲,所以我可以幫助人們學習,但是當我運行它時它可以工作,但它不會將圖像返回到屏幕上。這是代碼的主要位,我有我的陣列...我不斷收到一個字符串返回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