[英]Add image to JPanel through JLabel
我想將圖像從imgur添加到JPanel。 圖像未出現。
UTC = new JLabel("test");
utcImg = new ImageIcon("http://i.imgur.com/pkBtKC5.png");
UTC.setIcon(utcImg);
add(UTC);
僅顯示文本“ test”。 我做錯了嗎?
謝謝
編輯:從本地驅動器添加映像以減輕延遲問題。 現在沒有加載。 文件位於C:\\Users\\chg1024\\Test\\src\\images
JLabel utc = new JLabel("test");
ImageIcon utcImg = new ImageIcon("images/UTC.png");
utc.setIcon(utcImg);
add(utc);
revalidate();
ImageIcon(String)
將其構造函數參數解釋為磁盤上的文件。 你可以做
URL url = new URL("http://i.imgur.com/pkBtKC5.png");
Image image = ImageIO.read(url);
JLabel label = new JLabel(new ImageIcon(image));
但是請注意,由於網絡延遲和/或資源可用性,從URL加載圖像可能會產生問題。 例如,應首選嵌入式資源 。
JLabel label = new JLabel(new ImageIcon(getClass().getResource("/images/UTC.png")));
我想你可以用這個
ImageIcon(new URL("write your URL here"));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.