![](/img/trans.png)
[英]JLabel unexpected Text-Alignment behaviour when Icon is Null
[英]getting null error when creating icon in JLabel
我有以下 package:projectname/src/main/java.net/is/lms/project/frames 在 frame 文件夾中有一個 java class 稱為 testframe 和一個名為 icontest.png 的圖標(它是 87x84px)
我有以下代碼片段:
package net.is.lms.project.frames;
import javax.swing.*;
public class testframe {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(530, 600);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
String url = "src/main/java/net/is/lms/project/frames/icontest.png";
ImageIcon icon = new ImageIcon(url);
JLabel label = new JLabel();
label.setIcon(icon);
frame.add(label);
}
}
創建框架時,沒有顯示任何圖標
我嘗試了不同的路徑並像這樣添加它: URL url = testframe.class.getResource(".net/is/lms/project/frames/icontest.png");
我沒有足夠的聲譽來發表評論。
您是否嘗試過將圖像放在src/main/resources
文件夾中並像本例中那樣加載它們: 鏈接
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.