我不知道如果我指到正確的位置,此代碼,我試圖訪問的圖像標題Flower0.png
等的Java的getClass()的getResource在PNG返回NULL指針
它們位於與我的這個項目的其他代碼在同一個目錄中。 該課程位於名爲hangman.ui
的src文件夾中,而.png
文件位於名爲Resources
的目錄文件夾中。
也許getClass().getResource
是不正確的?
這是我第一次嘗試將圖像放入GUI中。
非常感謝幫助!
public WiltingFlowerRendererRemix(HangmanLogic logic)
{
panel = new JPanel();
panel.setLayout(new BorderLayout());
imageLabel = new JLabel();
panel.add(imageLabel, BorderLayout.CENTER);
int numberOfImages = 10;
images = new ImageIcon[numberOfImages];
for (int i = 0; i < numberOfImages; i++)
{
images[i] = new ImageIcon(getClass().getResource("Flower"+Integer.toString(i) + ".png"));
}
}
資源路徑必須以'/'開頭。嘗試'「/花」'。 – fge
謝謝,現在我無法看到圖像。我把它設置爲可見,我只是不知道... – jessicaeden
你也可以看看.. http://stackoverflow.com/questions/2343187/loading-resources-using-getclass-getresource – awsome