簡體   English   中英

為什么我的JLabel的圖標沒有出現?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM