Getting jLabel image url in Java

2019-09-02 01:42发布

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条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-09-02 02:20

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}
查看更多
登录 后发表回答