通常、Java Jar ファイル内に画像を埋め込む方法は、srcすべての画像ファイルとResource. クラス コードは次のようになります。
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
public class Resource{
public static BufferedImage loadImage(String imageFileName){
URL url = Resource.class.getResource(imageFileName);
if(url == null) return null;
try {
return ImageIO.read(url);
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
public static ImageIcon loadIcon(String imageFileName){
BufferedImage i = loadImage(imageFileName);
if(i == null) return null;
return new ImageIcon(i);
}
}
クラスResourceとすべての画像ファイルが同じパッケージにある場合、 を呼び出しJLabelて返された を使用して新しい を作成するだけです。これは、IDE で実行しているか、Jar ファイルから実行しているかに関係なく機能します。ImageIconloadIcon([simple filename])