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 类的新类,并创建新的字段字符串名称和覆盖等于方法来检查名称是否相等。
像这样:
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
}