I am having a problem with images, as I am trying to load a background for my launcher. It works when I run it in Eclipse, but when I export it to a jar file, it doesn't. I have used the many tutorials Stackoverflow has to offer, like this one. Am I doing anything wrong??
Here is my code:
package initializer;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.lang.Math;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.*;
@SuppressWarnings("serial")
public class MainLauncherInitializer extends JPanel implements ActionListener{
public ErrorMessageInitializer err = new ErrorMessageInitializer();
public String path = "";
File lwjgl = new File("src/");
public String stat;
public String Username = " ";
int num = 0;
JFrame jf = new JFrame();
JPanel jp = new JPanel();
public JButton jb = new JButton("Start");
JLabel jl = new JLabel("Minecraft Launcher");
JLabel credit = new JLabel("Programmed by Pale_Gray");
JLabel user = new JLabel("Username");
JLabel pass = new JLabel("Password");
JLabel status = new JLabel();
JTextField jta = new JTextField("");
JTextField password = new JTextField("");
public static int WIDTH = 300;
public static int HEIGHT = 200;
String name = System.getProperty("user.name");
JLabel backgroundtest = new JLabel();
File dir = new File("C:/Program Files/Java/jre1.8.0_261");
String cmd = "java -Xms1024m -Xmx1024m -cp C:/Users/" + name + "/AppData/Roaming/.minecraft/versions/b1.1_02/b1.1_02.jar;C:\Users\" + name + "\AppData\Roaming\.minecraft\libraries\org\lwjgl\lwjgl\lwjgl\lwjgl-2.4\jar\lwjgl.jar;C:\Users\" + name + "\AppData\Roaming\.minecraft\libraries\org\lwjgl\lwjgl\lwjgl\lwjgl-2.4\jar\lwjgl_util.jar;C:\Users\" + name + "\AppData\Roaming\.minecraft\libraries\net\java\jinput\jinput\2.0.5\jinput-2.0.5.jar;C:/Users/" + name + "/AppData/Roaming/.minecraft/libraries/net/java/jutils/jutils/1.0.0/jutils-1.0.0.jar -Djava.library.path=C:\Users\" + name + "\AppData\Roaming\.minecraft\libraries\org\lwjgl\lwjgl\lwjgl\lwjgl-2.4\native\windows net.minecraft.client.Minecraft " + Username + "9087";
public Timer t = new Timer(1, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int r = (int) (Math.random() * 255);
int g = (int) (Math.random() * 255);
int b = (int) (Math.random() * 255);
Color rgb = new Color(r, g, b);
credit.setForeground(rgb);
credit.repaint();
jta.getText();
if(jta.getText().length() < 3) {
stat = "Username is less than 3 characters!";
status.setBounds(WIDTH/2 - 130, 35, 500, 20);
status.setForeground(Color.RED);
}
if(jta.getText().length() > 16) {
stat = "Username is longer than 16 characters!";
status.setBounds(WIDTH/2 - 135, 35, 500, 20);
status.setForeground(Color.RED);
}
if(jta.getText().length() == 0) {
status.setBounds(WIDTH/2 - 95, 35, 500, 20);
stat = "ERR no username provided";
status.setForeground(Color.RED);
}
if(jta.getText().length() >= 3 && jta.getText().length() <= 16) {
stat = "Username is qualified";
status.setBounds(WIDTH/2 - 80, 35, 200, 20);
status.setForeground(new Color(20,168,0));
}
status.setText("Status: "+stat);
}
});
public void initializeJFrame(){
ImageIcon bg = new ImageIcon("src/background.png");
jb.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (jta.getText().length() < 3 || jta.getText().length() > 16) {
System.exit(-1);
} else {
try {
Runtime.getRuntime().exec(cmd, null, dir);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
Thread.sleep(500);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
System.exit(0);
}
}
});
jf.setSize(WIDTH,HEIGHT);
jf.setTitle("Cracked Launcher");
jf.setLayout(null);
jf.setContentPane(new JLabel(bg));
jf.setResizable(false);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
t.start();
// new InputStream(this.getClass().getClassLoader().getResourceAsStream("src/background.png"));
status.setBounds(WIDTH/2 - 55, 35, 200, 20);
jb.setBounds(WIDTH/2 - 50, 100, 100, 20);
jl.setBounds(WIDTH/2 - 55, 20, 200, 20);
credit.setBounds(WIDTH/2 - 80, 5, 200, 20);
jta.setBounds(WIDTH/2 - 75, 100, 150, 20);
user.setBounds(WIDTH/2 - 135, 80, 150, 20);
jta.setBounds(WIDTH/2 - 75, 80, 150, 20);
password.setBounds(WIDTH/2 - 75, 100, 150, 20);
pass.setBounds(WIDTH/2 - 135, 100, 150, 20);
jf.add(jb);
jf.add(jl);
jf.add(jta);
jf.add(user);
jf.add(status);
jf.add(credit);
jf.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}}
My image is in the src
folder, nothing else. I cannot find what the problem is.
question from:
https://stackoverflow.com/questions/65866102/jframe-setcontentpanenew-jlabelimage-background-image-not-loading-after-b