第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如何使 ImageIcon 在所有平臺(tái)上工作

如何使 ImageIcon 在所有平臺(tái)上工作

皈依舞 2023-05-24 15:59:34
我制作了一個(gè)簡(jiǎn)單的用戶界面,但是當(dāng)我在 Mac 上運(yùn)行相同的代碼時(shí),圖像沒(méi)有出現(xiàn)。我已經(jīng)在 Windows 上試過(guò)了;它有效,但在 Mac 上卻無(wú)效。我知道 File.separator,但在 ImageIcon 中它不應(yīng)該是這種情況,因?yàn)橄鄬?duì)路徑正在轉(zhuǎn)換為 URL,如果我沒(méi)記錯(cuò)的話,URL 可以在所有平臺(tái)上使用,因?yàn)樗褂谜备?。我真的很困惑為什么它不能?Mac 上運(yùn)行。    JLabel lblDesigniteLogo = new JLabel();    ImageIcon keyImage = new ImageIcon(this.getClass().getClassLoader().getResource("Images/designite_logo.png"));    lblDesigniteLogo.setIcon(keyImage);    GridBagConstraints gbc_lblDesignitelogo = new GridBagConstraints();    gbc_lblDesignitelogo.fill = GridBagConstraints.HORIZONTAL;    gbc_lblDesignitelogo.insets = new Insets(0, 0, 5, 0);    gbc_lblDesignitelogo.gridx = 2;    gbc_lblDesignitelogo.gridy = 0;    frame.getContentPane().add(lblDesigniteLogo, gbc_lblDesignitelogo);
查看完整描述

1 回答

?
暮色呼如

TA貢獻(xiàn)1853條經(jīng)驗(yàn) 獲得超9個(gè)贊

問(wèn)題實(shí)際上是由于圖像的路徑。從包中加載資源時(shí),您的路徑應(yīng)以/. 而不是"Images/designite_logo.png",應(yīng)該是"/Images/designite_logo.png"。當(dāng)然,圖像應(yīng)該放在正確的包中。你可以自己測(cè)試一下:


public class Main {

    public static void main(String[] args) {

        URL imageUrl = Main.class.getResource("com/test/images/img.jpg");

        System.out.println(imageUrl == null); //Prints true


        imageUrl = Main.class.getResource("/com/test/images/img.jpg");

        System.out.println(imageUrl == null); //Prints false

    }

}

項(xiàng)目結(jié)構(gòu)是:

http://img1.sycdn.imooc.com//646dc3fa0001195602520128.jpg

查看完整回答
反對(duì) 回復(fù) 2023-05-24
  • 1 回答
  • 0 關(guān)注
  • 125 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)