Getting jLabel image url in Java

假装没事ソ 提交于 2019-12-12 01:57:29

问题


I'll make it short.

I've put a jLbl_show and in a if condition I've set an image for it.

if(int c==1){
  jLbl_show0.setIcon(new ImageIcon(getClass().getResource("/img/wrong.png")));
}
else{
  jLbl_show0.setIcon(new ImageIcon(getClass().getResource("/img/ok.png")));
}

But, as now the image is set i need to take the image URL to another condition. which is like;

if(imageURL is "/img/ok.png"){ do somthing }
elseif (imgURL is "/img/wrong.png"){ do something }

is there a way to do this?


回答1:


do something like this:

boolean flag = false;

if(someBoolean){
  jLbl_show0.setIcon(new ImageIcon(getClass().getResource("/img/wrong.png")));
  flag = true;
}
else{
  jLbl_show0.setIcon(new ImageIcon(getClass().getResource("/img/ok.png")));
}

if(flag){something}
else{something}


来源:https://stackoverflow.com/questions/13188874/getting-jlabel-image-url-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!