Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
249 views
in Technique[技术] by (71.8m points)

java - Jframe.setContentPane(new JLabel([image])); background image not loading after built .jar

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.

image image2

question from:https://stackoverflow.com/questions/65866102/jframe-setcontentpanenew-jlabelimage-background-image-not-loading-after-b

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
ImageIcon bg = new ImageIcon("src/background.png");

This constructor takes a filename; it will not load resources from a jar. You want to pass a URL generated by a classloader, like so:

ImageIcon bg = new ImageIcon(getClass().retResource("background.png"));

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...