使用:
jLabel1.getIcon().toString();
它仅返回对象“javax.swing.ImageIcon@2982ccce
”;
不是绝对路径,那么我怎么能返回该图标的绝对路径?
例如:C:/File/imageicon.jpg
答案 0 :(得分:-1)
JLabel jLabel1 = new JLabel();
jLabel1.setIcon(new ImageIcon(getClass().getResource("/lk/edu/ijse/examsimulator/img/Hand With Pen_36px.png")));
if (jLabel1.getIcon() != null) {
Icon iconPath = jLabel1.getIcon();
System.out.println(iconPath.toString());
/* print
* file:/E:/Quick%20access/Documents/NetBeansProjects/ExamSimulator/ExmClientAdmin/build/classes/lk/edu/ijse/examsimulator/img/Hand%20With%20Pen_36px.png
*/
String absolutePath = iconPath.toString().replaceFirst("file:/", "");
absolutePath = absolutePath.replaceAll("%20", " ");
System.out.println(absolutePath);
/* print
*
* E:/Quick access/Documents/NetBeansProjects/ExamSimulator/ExmClientAdmin/build/classes/lk/edu/ijse/examsimulator/img/Hand With Pen_36px.png
*
* it's my image absolute path */
}