我试图将图像“Pic.png”添加到此JLabel“label1”并将其显示在JFrame“window1”上的JPanel“panel1”上。但是当它运行时它不显示我的图像。任何人帮助? (我读过关于将其添加到源文件或其他内容,但我不确定我在做什么,因为我是Java新手,如果没有源代码中的图像,是否无法访问图片?)试图将图像添加到标签但不起作用?
public class UIForIshidaQuery {
public static void main(String[] args) {
System.out.println("Running...");
JFrame window1 = new JFrame();
window1.setVisible(true);
window1.setSize(1080, 720);
window1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel1 = (JPanel) window1.getContentPane();
JLabel label1 = new JLabel();
panel1.setLayout(null);
ImageIcon image = new ImageIcon("C:\\Users\\BC03\\Pictures\\Saved Pictures\\Other\\Pic.png");
label1.setIcon(image);
label1.setBounds(500, 500, 500, 500);
panel1.add(label1);
}
}
是否有'new File(“C:\\ Users \\ BC03 \\ Pictures \\ Saved Pictures \\ Other \\ Pic.png”)。exists()'return true? – Berger
查看[tag:embedded-resource]标签中引用的示例[here](http://stackoverflow.com/tags/embedded-resource/info)。 – trashgod
是的,它返回true @Berger – Cutter