我試圖將圖像「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