简体   繁体   English

将图像加载到JLabel中不起作用

[英]Load Image into JLabel not working

I try to display an image using a JLabel . 我尝试使用JLabel显示图像。 This is my project navigator: 这是我的项目导航器: 在此处输入图片说明

From SettingsDialog.java I want to display an image using following code: 我想从SettingsDialog.java使用以下代码显示图像:

        String path = "/images/sidebar-icon-48.png";
        File file = new File(path);
        Image image;
        try {
            image = ImageIO.read(file);

            JLabel label = new JLabel(new ImageIcon(image));
            header.add(label); // header is a JPanel
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

The code throws an exception: Can't read input file! 该代码引发异常: 无法读取输入文件!

Is the path of the image is wrong? 图像的路径是否错误?

Don't read from a file, read from the class path 不要从文件中读取,而是从类路径中读取

image = ImageIO.read(getClass().getResource(path));
-or-
image = ImageIO.read(MyClass.class.getResource(path));

When you use a File object, you're telling the program to read from the file system, which will make your path invalid. 当使用File对象时,您要告诉程序从文件系统读取,这将使您的路径无效。 The path you are using is correct though, when reading from the class path, as you should be doing. 从类路径读取时,您正在使用的路径正确的,但是应该这样做。

See the wiki on embedded resource . 请参阅有关嵌入式资源的Wiki。 Also see getResource() 另请参见getResource()


UPDATE Test Run 更新测试运行

在此处输入图片说明

package org.apache.openoffice.sidebar;

import javax.swing.*;

public class SomeClass {
    public SomeClass() {
        ImageIcon icon = new ImageIcon(
              SomeClass.class.getResource("/images/sidebar-icon-48.png"));
        JLabel label = new JLabel(icon);

        JFrame frame = new JFrame("Test");
        frame.add(label);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable(){
            public void run() {
                new SomeClass();
            }
        });
    }
}

"/images/sidebar-icon-48.png" is root path. “ /images/sidebar-icon-48.png”是根路径。 On windows would be c:\\images\\sidebar-icon-48.png or d:\\images\\sidebar-icon-48.png depending on current drive (java converts the / to \\ - not an issue). 在Windows上,取决于当前驱动器,驱动器为c:\\ images \\ sidebar-icon-48.png或d:\\ images \\ sidebar-icon-48.png(java将/转换为\\-不是问题)。 Linux images would a child of root /images/sidebar-icon-48.png Need to load relative to class or relative to the jar that had the class (if you do not want to store images inside jar. Linux映像是根目录/images/sidebar-icon-48.png的子级。需要相对于类或相对于具有该类的jar进行加载(如果您不希望将图像存储在jar中。

In big projects its nice to have images and other resources outside the jar so the jar is smaller and more importantly its easy to change the resources without fiddling with jars/ wars. 在大型项目中,最好将图像和其他资源保存在jar之外,因此jar较小,更重要的是,它易于更改资源,而无需摆弄jar / wars。

Since you seem to be making a add on for open office, you will have to keep everything in jar and so peeskillet answer is right. 由于您似乎要为开放式办公室增加附加功能,因此您必须将所有内容保存在罐子中,因此peeskillet答案是正确的。 But make sure your images folder is being packed in the jar. 但是请确保将图像文件夹包装在罐子中。 Extract the jar ising the jar command or rename the file to zip and extract. 使用jar命令解压缩jar或将文件重命名为zip并解压缩。

Or check and fix project settings. 或检查并修复项目设置。 How to make a jar in eclipse ... latest one has a wizard that makes an ant script or this SO 如何使Eclipse的一个罐子...最新的一个有向导使Ant脚本或本SO

try to use this directly : 尝试直接使用此:

    JLabel label = new JLabel(new ImageIcon(path));

and delete these line : 并删除这些行:

File file = new File(path);

image = ImageIO.read(file);

if error still exist paste the following error 如果错误仍然存​​在,请粘贴以下错误

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM