Java 动态地将图像添加到JLabel

Java 动态地将图像添加到JLabel,java,swing,jpanel,jlabel,Java,Swing,Jpanel,Jlabel,我试图动态地将图像添加到我的JLabel中,然后将JLabel添加到我的面板中。我的代码没有抛出错误,但图像从未显示 public JFrameGamePlay(String playername, String playerselected) { initComponents(); playerimage = "/Users/owner/Downloads/__Pikachu.png"; ImageIcon pimage = new ImageIcon(playeri

我试图动态地将图像添加到我的JLabel中,然后将JLabel添加到我的面板中。我的代码没有抛出错误,但图像从未显示

public JFrameGamePlay(String playername, String playerselected) {
    initComponents();

    playerimage = "/Users/owner/Downloads/__Pikachu.png";
    ImageIcon pimage = new ImageIcon(playerimage);
    JLabel lblPlayer = new JLabel(pimage);
    pnlPlayer.add(lblPlayer);
    pnlPlayer.validate();
    pnlPlayer.repaint();
}

public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new JFrameGamePlay().setVisible(true);
        }
    });
}
编辑
通过进一步的谷歌搜索,我想出了这个语法

JLabel lblPlayer;
lblPlayer = new JLabel(new ImageIcon(getClass().getClassLoader().getResource("__Image1.png")));
pnlPlayer.add(lblPlayer);
pnlPlayer.validate();
pnlPlayer.repaint();
但当我运行代码时,会出现以下调试错误:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
  at javax.swing.ImageIcon.<init>(ImageIcon.java:217)
}

用于读取
图像
。像

File playerimage = new File("/Users/owner/Downloads/__Pikachu.png");
ImageIcon pimage = new ImageIcon(ImageIO.read(playerimage));
JLabel lblPlayer = new JLabel(pimage);
pnlPlayer.add(lblPlayer);

NullPointerException
来自
getResource(“\uu Image1.png”)
返回
null
,因为它没有找到文件。您应该使用类路径中的位置作为前缀(毕竟,类加载器加载文件)。例如,如果图像位于jar(或类路径)中的
res
目录中:

或者您可以直接提供完整的路径:

new JLabel(new ImageIcon("/images/thewall.png"));
例如:

public class JFrameGamePlay extends JFrame {

    private static final long serialVersionUID = 1L;

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setBounds(100, 100, 450, 300);
        JPanel contentPane = new JPanel(new BorderLayout());
        frame.setContentPane(contentPane);
        JLabel label = new JLabel(new ImageIcon("/images/thewall.png"));
        contentPane.add(label, BorderLayout.CENTER);
        JLabel label1 = new JLabel(new ImageIcon(JFrameGamePlay.class.getResource("/javax/swing/plaf/basic/icons/JavaCup16.png")));
        contentPane.add(label1, BorderLayout.NORTH);
        frame.setVisible(true);
    }

}
我的代码没有抛出任何错误,但从未添加图像

是的,不幸的是ImageIcon就是这样工作的。如果无法读取您传递给它的文件名或URL,它不会抛出异常或任何东西。代码运行时没有错误,但没有显示任何内容

playerimage=“/Users/owner/Downloads/_Pikachu.png”

如果您想从这样一个固定位置的文件中读取,那么使用getClassLoader().getResource()是没有帮助的。这可能有助于读取与应用程序一起打包的图像文件,而不是读取用户主目录中的图像。您最初的新ImageIcon(imageLocation)方法适用于此

[注意:我认为像原始代码那样直接将文件名(或URL)发送到ImageIcon是个好主意。其他答案建议使用ImageIO.read(),这有助于了解错误所在,但一旦开始工作,您可以将其更改回ImageIcon(filenameOrUrl)。但这没什么大不了的。]

我试图动态地将图像添加到我的JLabel中,然后将JLabel添加到我的面板中

如果“动态”是指在某个事件发生时,在屏幕上已经可以看到面板后执行此操作,那么我建议不要执行此操作

也就是说,不是这样做

JLabel lblPlayer = new JLabel(pimage);
pnlPlayer.add(lblPlayer);
pnlPlayer.validate();
pnlPlayer.repaint();

…您可以在面板显示在屏幕上之前,从一开始就将JLabel添加到面板中(因为没有文本或图像,所以不可见)。然后动态地将ImageIcon添加到已经存在的JLabel中。您只需要调用existingLabel.setIcon(…)。不需要调用validate()或repaint()。

如果Elliott的答案不能解决您的问题,那么您需要发布一个有效的问题,一个小的可编译且可运行的程序,为我们演示您的问题。请阅读链接。也可以使用在线提供的图像以便于测试。在
pnlPlayer
上调用
revalidate
repaint
,或者更好——将该JLabel已经放在pnlPlayer JPanel中,并首先用一个包含空白图像的图像图标填充它,一个是播放器图像的大小。@MadProgrammer-如果我添加pnlPlayer.revalidate();和pnlPlayer.repaint();还是没什么shows@DoctorFord然后我同意气垫船,没有更多的上下文,我们无法为您做更多的事情,当然最好使用资源而不是文件,所以当OP jar的程序。。。。1+使用此无图像显示在我的标签中-与上述问题相同图像位于源文件夹的子文件夹“我的资源”文件夹中。我试着按照你的建议去做,并执行/resources/_Image1.png,但我还是得到了null异常?我编辑代码以读取JLabel lblPlayer=newjlabel(newimageicon(“/resources/_Image1.png”);并且没有抛出错误,但是当窗体loads@DoctorFord使用
新建图像图标(class.getResource(“/path/relative/to/source/folder/image.png”)
从类路径加载,或使用
新建图像图标(“/full/path/to/image.png”)
从硬盘加载。请注意,如果在Windows上运行,路径可能区分大小写。如果图像位于源文件夹的子文件夹中,则
新建JLabel(新建图像图标(JFrameGamePlay.class.getResource(“/resources/\uu Image1.png”))
应该可以工作。@DoctorFord尝试复制粘贴我给出的示例,它可以工作(至少对于第二个图像。使用指向您的完整路径
\uu Image1.png
而不是我使用的
/images/thewall.png
)。我喜欢这种方法。如何将资源文件夹中的图像设置为setIcon?当我尝试时,我发现URL的编译错误无法转换为icon。这是我的语法lblPlayer.setIcon(getClass().getClassLoader().getResource(“/resources/_Pikachu.png”);必须执行类似lblPlayer.setIcon(新图像图标(getClass().getClassLoader().getResource(“/resources/_Pikachu.png”))的操作,以删除编译错误-但我在线程“AWT-EventQueue-0”中遇到异常调试错误java.lang.NullPointerException好吧,我们都必须在某个时候调试NullPointerException。异常消息应该给你一个行号来帮助你追踪它。你可以尝试的一件事是lblPlayer.setIcon(新的BallIcon(60,java.awt.Color.ORANGE))看看标签和图像加载是否分开工作。这是我全部错误的粘贴箱-你能帮我找到它吗?它列出了多个行号
public class JFrameGamePlay extends JFrame {

    private static final long serialVersionUID = 1L;

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setBounds(100, 100, 450, 300);
        JPanel contentPane = new JPanel(new BorderLayout());
        frame.setContentPane(contentPane);
        JLabel label = new JLabel(new ImageIcon("/images/thewall.png"));
        contentPane.add(label, BorderLayout.CENTER);
        JLabel label1 = new JLabel(new ImageIcon(JFrameGamePlay.class.getResource("/javax/swing/plaf/basic/icons/JavaCup16.png")));
        contentPane.add(label1, BorderLayout.NORTH);
        frame.setVisible(true);
    }

}
JLabel lblPlayer = new JLabel(pimage);
pnlPlayer.add(lblPlayer);
pnlPlayer.validate();
pnlPlayer.repaint();