![](/img/trans.png)
[英]Java - Why doesn't my JLabel icon update instantly when the line of code is executed?
[英]Why doesn't my JLabel's icon appear?
我正在嘗試將圖標添加到我的JLabel
,但是該圖標沒有出現。 該圖標是與我的Frame
類文件位於同一位置的圖像。 為什么沒有出現圖標?
這是我的代碼:
public Passing()
{
initComponents();
ImageIcon imageIcon = new ImageIcon("altis.jpg");
jLabel1.setIcon(imageIcon);
//I have also tried: jLabel1 = new JLabel(new ImageIcon("/res/altis.jpg"));
}
嘗試在src
目錄中創建一個名為res
的文件夾,將圖標放入res
,然后替換此行:
ImageIcon imageIcon = new ImageIcon("altis.jpg");
有了這個:
ImageIcon imageIcon = new ImageIcon(this.getClass().getResource("/res/altis.jpg"));
(或者,如果這樣不起作用):
ImageIcon imageIcon = new ImageIcon(this.getClass().getResource("res/altis.jpg"));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.