【问题标题】:Not displaying image jframe不显示图像jframe
【发布时间】:2018-11-20 22:26:27
【问题描述】:

我一直试图让图像出现,但没有任何反应。我查找了各种方法并应用了它们,但我一直无法实现。我开始认为也许我放在了错误的文件中。在小程序中,我只需要将图像放在构建文件夹中。至于JFrame,我不知道在哪里。我尝试将它放在构建中,没有图像。我试着把它放在src,仍然没有。以下是我迄今为止一直在测试的一些内容。也许是图像位置或代码。

谁能告诉我我做错了什么?

import javax.swing.*;
import java.awt.*;

public class Changey extends javax.swing.JFrame {
JPanel panel = (JPanel) this.getContentPane();
//JLabel label = new JLabel();

public Changey() {
    initComponents();
    panel.setLayout(null);

    //ImageIcon icon = new ImageIcon("Cookie.png");
    //JLabel label = new JLabel(icon);
    //panel.add(label);

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

    //Icon icon = new ImageIcon("Cookie.png");
    //JLabel label = new JLabel( icon );
    //frame.add(label);

    //label.setIcon(new ImageIcon("Cookie.png"));
    //panel.add(label);

    //JLabel img = new JLabel(new ImageIcon("Cookie.png"));
    //img.setBounds(200, 300, 100, 100); // x, y, width, height
    //panel.add(img);

    //ImageIcon image = new ImageIcon("Cookie.png");
    //label.setBounds(10, 10, 400, 400);
    //panel.add(label);
    //image.getImage();

    //label.setIcon(new ImageIcon("Cookie.PNG"));
    //label.setBounds(10, 10, 400, 400);
    //panel.add(label);

    setSize(1130,380);
    setTitle("Image");        
}

【问题讨论】:

标签: java image swing jframe embedded-resource


【解决方案1】:

在 ImageIcon() 构造函数之后,您可以调用 image.getImageLoadStatus() 来查看加载情况。

要检查代码是否可以看到文件,请使用 File myFile = new File("Cookie.png"); System.out.println(myFile.exists());

如果您将文件用作资源,它应该在类路径中。

如果您使用具有相对路径的文件,它应该是相对于应用程序的起始文件夹的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多