Un mémo lorsque vous avez des problèmes car l'image n'est pas incluse lorsque vous créez un fichier jar à l'aide d'Eclipse.
L'image ne s'affiche pas même si je crée un fichier jar. La méthode de lecture de l'image est statique. À ce rythme, le fichier jar ne contiendra aucune image.
Main.java
public static ImageIcon getImageIcon() {
    ImageIcon icon = new ImageIcon("nanase.jpg ");
    return icon;
}
Prenez une classe comme argument
Main.java
public static ImageIcon getImageIcon(Main m) {
    ClassLoader cl = m.getClass().getClassLoader();
    ImageIcon icon = new ImageIcon(cl.getResource("nanase.jpg "));
    return icon;
}
Référence → https://virtualwalk.hatenadiary.org/entry/20121013/1350127275
Main.java
public ImageIcon getImageIcon() {
    ClassLoader cl = this.getClass().getClassLoader();
    ImageIcon icon = new ImageIcon(cl.getResource("nanase.jpg "));
    return icon;
}
・ Htps: // er tsua lk. Hatena Jia ry. Rg / Entrée / 20120113/1350127275 ・ Htps: // ww. Eh bien. jp / Tsutoria l / Imageikon / Inde x2. html -Création d'un pot avec eclipse https://java.keicode.com/lang/how-to-compile-jar-with-eclipse.php
Main.java
package include;
import java.awt.BorderLayout;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Main extends JFrame{
	  public static void main(String[] args){
	    Main frame = new Main();
	    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	    frame.setBounds(10, 10, 150, 150);
	    frame.setTitle("Titre");
	    frame.setVisible(true);
	  }
	  Main(){
//	    ImageIcon icon = this.getImageIcon();
	    ImageIcon icon = Im.getImageIcon(this);
	    JLabel label = new JLabel(icon);
	    JPanel p = new JPanel();
	    p.add(label);
	    getContentPane().add(p, BorderLayout.CENTER);
	  }
//	  private ImageIcon getImageIcon() {
//		  ClassLoader cl = this.getClass().getClassLoader();
//		  ImageIcon icon = new ImageIcon(cl.getResource("nanase.jpg "));
//		  return icon;
//	  }
	}
class Im{
	public static ImageIcon getImageIcon(Main m) {
	  ClassLoader cl = m.getClass().getClassLoader();
	  ImageIcon icon = new ImageIcon(cl.getResource("nanase.jpg "));
	  return icon;
	 }
}
        Recommended Posts