简体   繁体   English

JLabel 不显示图像

[英]JLabel doesn't show image

I'm not sure where the problem is.我不确定问题出在哪里。 Is it in the path?它在路径中吗? The image doesn't show although there are no errors at all in the code syntax.尽管代码语法中根本没有错误,但图像并未显示。 Should I provide the whole path or just place the image in the directory and call its name?我应该提供整个路径还是只是将图像放在目录中并调用它的名称? Thank you.谢谢你。

public class NetworkingGame {

private JFrame jfrm;

NetworkingGame(){
    jfrm = new JFrame("Angry Painters");
    jfrm.setSize(800, 480);
    jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    jfrm.setVisible(true);

}// end of constructor

public void ImageLoading(){
    ImageIcon i = new ImageIcon("C:/Users/TOSHIBA/Documents/NetBeansProjects/NetworkingGame/build/classes/angry-painters.jpeg");
    JLabel jl = new JLabel(i);
    jfrm.add(jl);
}

public static void main(String[] args) throws Exception{

    SwingUtilities.invokeLater(new Runnable(){

        @Override
        public void run(){

            NetworkingGame ng = new NetworkingGame();
            ng.ImageLoading();
        } // end of run method
    }); // end of Runnable

   }//end of main method
}//end of class NetworkingGame

Do not use the file path as icon location as this will only work on your computer.不要使用文件路径作为图标位置,因为这仅适用于您的计算机。 You really cannot expect all machines in the world to have C:/Users/TOSHIBA ...angry-painters.jpeg in the right place!你真的不能指望世界上所有的机器都有 C:/Users/TOSHIBA ...angry-painters.jpeg 在正确的位置!

Copy the file next to the source code (.java) class you use and then call复制您使用的源代码 (.java) 类旁边的文件,然后调用

 new ImageIcon(getClass().getResource("angry-painters.jpeg"));

The builder should copy the image resource to the class folder itself.构建器应该将图像资源复制到类文件夹本身。

Try to use path something like this :尝试使用类似这样的路径:

jLabel1.setIcon(new ImageIcon("c:\\Users\\xyz\\Documents\\NetBeansProjects\\game\\src\\images.jpg"));

Update更新

If dis also does not work, then如果 dis 也不起作用,那么

    jLabel.setIcon(new ImageIcon(getClass().getResource("/image.jpg")));
    jLabel.setText("jLabel");

should.The image.jpg should be inside your project folder.应该image.jpg应该在您的项目文件夹中。

I think your code doesn't like the way your binding the image.我认为您的代码不喜欢您绑定图像的方式。

assumes that your put the images/icons in the source folder假设您将图像/图标放在源文件夹中

ImageIcon i=new javax.swing.ImageIcon(getClass().getResource("myimage.jpeg"))

or if you create a folder in the source folder或者如果您在源文件夹中创建一个文件夹

InputStream stream = this.getClass().getClassLoader().getResourceAsStream("/images/image.jpg");
BufferedImage bufferedImage=ImageIO.read(stream);
ImageIcon icon= new ImageIcon(bufferedImage);

in IntellijIdea.在 IntellijIdea 中。 2021 In my case it just didn't read image files. 2021就我而言,它只是没有读取图像文件。 To fix that:要解决这个问题:

1. make res folder in project root('src's folder sibling) 1.在项目根目录下创建res文件夹('src的文件夹兄弟)

2. Handle artifact in File->Project Structure->artifacts by + , - 2.在 File->Project Structure->artifacts 中通过 + , - 处理 artifact

3. File->Project Structure->artifacts->output layout tab make structure of final jar as 'res' folder , app root package folder(say 'com'), and 'META-INF' folder. 3. File->Project Structure->artifacts->output layout 选项卡将最终 jar 的结构设为“res”文件夹、应用程序根包文件夹(比如“com”)和“META-INF”文件夹。 Fill out them with content from yourapp/output folder and 'res's content from mentioned in 1. step 'res'.用您的应用程序/输出文件夹中的内容和1.步骤“res”中提到的“res”内容填写它们。

4. After that in 'project structure' 'Modules' rightclick on 'res' and mark it as resources. 4.然后在“项目结构”“模块”中右键单击“res”并将其标记为资源。

5. Declare images like this: 5.像这样声明图像:

ImageIcon loadingLcl = new ImageIcon(this.getClass().getResource("/res/32.gif")); before putting into JLabel.在放入 JLabel 之前。

Also it s preferable to read image once when initializing the app.此外,最好在初始化应用程序时读取一次图像。 Otherwise sometimes it can throw IOException.否则有时它会抛出 IOException。 So if possible handle that at start.因此,如果可能,请在开始时处理它。 Like they do in gamemaking.就像他们在游戏制作中所做的那样。

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

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