2013-02-19 78 views
0

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

+0

這取決於圖像的存儲位置。從你的代碼中,它說圖像存儲在磁盤上的'。\ graphics \ {name} .png'上。 1-它們存在嗎? 2-他們是否想嵌入? – MadProgrammer 2013-02-19 04:34:20

+0

我知道圖像存在,因爲我已經在菜單代碼中將它們「手動」直接稱爲JLabel。所以我知道圖像可以加載和顯示。 – tyty5949 2013-02-19 04:41:17

+0

然後,我會確保'String'' current_state'與圖像名稱的匹配,並且變量設置正確 – MadProgrammer 2013-02-19 05:04:47

回答

0

看起來您在創建ImageIcon的新實例時沒有傳遞有效的文件名。

currentstate_image = new ImageIcon("graphics\\" + current_state + ".png"); 

按照ImageIcon的javadoc,您需要通過您要創建的ImageIcon的文件名(路徑)。

您可能希望確保該文件確實存在下./graphics/{current_state}.png

另外,javadoc中說,儘可能使用斜槓。

編輯

System.out.println(current_state + " image loaded"); 

你檢查,如果你的current_state不爲空在這一點? 如果您正在獲取「空圖像加載」,這意味着current_state變量是null。檢查你的us_states.getCurrentState();方法。

發佈您的USA_States類也可能有幫助。

如果您嘗試使用相同的us_states實例從Menu類以上, 你將不得不從Menu類的us_states傳遞給States_Images類。

下面是修改後的代碼: 菜單類:

public Menu(){ 
    setLayout(null); 
    setBackground(Color.WHITE); 

    us_state = new USA_States(); 
    us_state.setRandomState(); 

    us_img = new States_Images(us_state); //Pass us_state to States_Images 
    System.out.println(us_state.getCurrentState()); 

    JLabel l = new JLabel(); 
    l.setIcon(us_img.getCurrentStateImage()); 
    l.setBounds(0,0,500,500); 
    add(l); 
} 

States_Images類:

private ImageIcon currentstate_image; 
private String current_state; 
USA_States us_states; 

public States_Images(USA_States us_state){ 
    this.us_states = us_state 
} 

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(); 
} 
+0

我這樣做,當我運行它仍然說在getCurrentStateImage()方法中加載的空圖像。我得到了圖像部分的權利,因爲我可以「手動」將圖像加載到菜單類中的jlabel – tyty5949 2013-02-19 04:46:55

相關問題