我无法让ImageIcon正常显示

问题描述 投票:0回答:1

一切正常,除了当我按下运行时,它不会在 JFrame 中显示标签的图标。有谁知道如何解决这个问题吗?

public class Frame extends JFrame
{
    private final int speed=5;
    JLabel label;
    ImageIcon icon;
    public void create() {
        this.setSize(240,228);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLocationRelativeTo(null);
        this.addKeyListener(this);
        this.setLayout(null);
        icon=new ImageIcon("rocket.png");
        label=new JLabel();
        label.setBounds(0,0,100,100);
        label.setIcon(icon);
        this.add(label);
        this.setVisible(true);
    }
}

我尝试过重命名该文件,将其放在代码中的几个不同位置,甚至查找了一两个教程,但没有一个有帮助。

java jframe jpanel imageicon
1个回答
0
投票

更改此:

icon=new ImageIcon("rocket.png");

至:

icon=new ImageIcon("src\\rocket.png");
© www.soinside.com 2019 - 2024. All rights reserved.