A memo when you are in trouble because the image is not included when you create a jar file using Eclipse.
The image is not displayed even if I create a jar file. The method to read the image is static. If this is left as it is, the image will not be included in the jar file.
Main.java
public static ImageIcon getImageIcon() {
ImageIcon icon = new ImageIcon("nanase.jpg ");
return icon;
}
Take a class as an argument
Main.java
public static ImageIcon getImageIcon(Main m) {
ClassLoader cl = m.getClass().getClassLoader();
ImageIcon icon = new ImageIcon(cl.getResource("nanase.jpg "));
return icon;
}
Reference → 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 / Entry / 20120113/1350127275 ・ Htps: // ww. Well then. jp / Tsutoria l / Imageikon / Inde x2. html -Creating a jar with 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("title");
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