编译时图像问题 [英] Problems with images when compiled

查看:164
本文介绍了编译时图像问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在互联网上试图弄清楚如何在编译成可运行的jar后显示图像图标。我发现这个问题太晚了,我在eclipse中运行了我的程序很多次,每一件事情都有效,现在6个月后,项目完成后,我用eclipse编译了我的程序,没有音频或图像工作。在网上阅读,它说关于图像文件夹的位置应该在jar内,但我的不会放在那里?

I have been all over the internet trying to work out how to get an image icon displayed after compiling into a runnable jar. I discovered this problem way too late, I ran my program many times before in eclipse and every thing has worked, now 6 months later with project finished, I compiled my program with eclipse and no audio or images work. Reading on the net, it says about the location of images folder should be inside jar, but mine doesnt get put there?

我已经玩了图像文件夹移动它在源文件夹内,但它没有工作。我有一种感觉,这可能与资源的路径有关... ebut只是猜测。

I have played around with the images folder moving it inside the source folder, but it didn't work. I have a feeling that it might be something to do with the path of the resource...ebut thats only guessing.

我已经建立了一个简单的程序,具有相同的结果...在eclipse中运行时工作,但编译时不工作。有人可以通过修改下面的代码向我展示一个例子。感谢提前。

I have built a simple program that has the same results... works when ran in eclipse, but not when compiled. Could somebody show me an example by modifying my code below. Thanks in advance.

来源代码:

package ImageIcon;

import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Gui {

public static JLabel c;

public Gui(){
    JFrame f = new JFrame();

    JPanel p = new JPanel();
    p.setBounds(0, 0, 120, 200);
    p.setBackground(Color.black);
    p.setLayout(null);

    JPanel bg = new JPanel(new BorderLayout());
    bg.setBounds(50, 50, 15, 15);
    bg.setBackground(Color.white);

    ImageIcon a = new ImageIcon("images/success.jpg");
    c = new JLabel(a);

    f.setSize(100, 200);
    f.setLayout(null);
    f.setLocationRelativeTo(null);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);

    f.add(p);
    p.add(bg);
    bg.add(c);
}

public static void main(String[] args) {
    new Gui();
}

}

推荐答案

当前的目录设置,图像 dir甚至不会嵌入到jar中。尝试提取它,你很可能会看到它不在那里。

With your current directory setup, the images dir won't even get built into the jar. Try to extract it and you will most likely see that it's not in there.

你可以通过这个事实来告诉它,它在文件夹中没有小的包标记,如下所示:资源

You can tell by the fact that it doesn't have the little package logo in the folder, as seen here with resources

类路径(/ jar)中内置的唯一默认目录是 src 。我们需要将资源加入 src

The only default directory built into the classpath (/jar) is the src. We need to either put the resources into the src

或配置构建包含资源中的文件的路径。一旦我们这样做,我们将看到文件夹图标中的小包图标。这就是我们知道文件在构建路径上的方式

or configure the build path to include the files that are in the resources. Once we do that, we will see the little package icon inside the folder icon. That's how we know the files are on the build path


  • 第一张图片不能,它不会奏效(这是你目前的困境)

  • First Image: Can't, it won't work (this your current predicament)

第二张图片

ImageIcon icon = new ImageIcon(
         getClass().getResource("/resources/stackoverflow.png"));


  • 第三张图片

    ImageIcon icon = new ImageIcon(
             getClass().getResource("/stackoverflow.png"));
    


  • 要配置构建路径以使用第三个选项,请按照 此答案中的示例2 中的说明

    To configure the build path to use the third option, follow the instructions in Example 2 in this answer

    这篇关于编译时图像问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆