private void Table1ButtonActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
ImageIcon i = new ImageIcon("images\\img.png");
if(Table1Button.getIcon() == i){
MakeABooking mab = new MakeABooking();
mab.setVisible(true);
}
}
嗨,有人可以帮助我,以便我可以检查我的jButton是否有特定的imageIcon吗?感谢
答案 0 :(得分:0)
据我所知,目前没有默认方法。你可以做的是创建一个扩展ImageIcon类的新类,并创建新字段String name并覆盖equals方法以检查名称是否相等。
类似的东西:
class MyImageIcon extends ImageIcon {
private String name;
public MyImageIcon(String filename, String imageName){
super(filename);
name = imageName;
}
// Override equals method and check if names are equal
@Override
public boolean equals(Object other){
MyImageIcon otherImage = (MyImageIcon)other;
if (other == null) return false;
return name.equals(otherImage.name);
}
}
然后检查你会做类似的事情:
MyImageIcon myButtonIcon = (MyImageIcon)myButton.getIcon();
MyImageIcon myImageIcon = new MyImageIcon(imageFile, imageName);
if (myButtonIcon.equals(myImageIcon)) {
// Do stuff
}