簡體   English   中英

圖像不會出現在一個罐子里

[英]Images Won't Appear In A Jar

因此,我正在使用Java開發基本的文字冒險游戲,並且我決定包括圖像。 我用來將圖像加載到程序中的代碼是這樣的:

imagePanel = new JPanel();
imagePanel.setBounds(50, 50, 400, 260);
imagePanel.setBackground(Color.BLACK);
con.add(imagePanel);

imageLabel = new JLabel();

image = new ImageIcon(".//res//towngate.jpg");

imageLabel.setIcon(image);
imagePanel.add(imageLabel);

當我在eclipse中運行該程序時,一切工作都很好,但是當我將其導出為jar時,沒有圖像出現。 如果有人可以幫助我解決此問題,我將不勝感激。 謝謝!

確保圖標包含在jar文件中,並嘗試在運行時使用類加載器進行查找。 (下面的示例)不要使用任何相對或絕對路徑,只需使用文件名即可:

public ImageIcon loadIcon(String iconName) throws IOException {
  ClassLoader loader = this.getClass().getClassLoader();
  BufferedImage icon = 
  ImageIO.read(loader.getResourceAsStream(iconName));
  return new ImageIcon(icon);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM