0
我想知道爲什麼我的比較中檢查兩個JButton是否具有相同的ImageIcon?這裏是我的類比較兩個JButton中的ImageIcons
public class Card extends JButton{
// Instance Variables
private ImageIcon icon;
private static final int CARD_SIZE = 165;
public Card(ImageIcon icon){
setIcon(ResizeIcon(icon));
this.icon = icon;
setOpaque(true);
setBorder(new EmptyBorder(0,0,0,0));
// Preferred card size
setPreferredSize(new Dimension(CARD_SIZE, CARD_SIZE));
}
public boolean SameIcon(Card card){
System.out.println(((ImageIcon)this.getIcon()).getDescription());
return getIcon() == card.getIcon();
}
// Resize the image to fit into JButton regardless of its original dimensions
private ImageIcon ResizeIcon(ImageIcon imagIcon){
Image img = imagIcon.getImage();
Image newimg = img.getScaledInstance(CARD_SIZE - 5, CARD_SIZE - 5, java.awt.Image.SCALE_SMOOTH);
return new ImageIcon(newimg);
}
}
我的問題基本上是爲什麼我得到空當我這樣做(的ImageIcon)this.getIcon()。getDescription()。看起來像setIcon只設置ImageIcon而不是圖標。因爲它顯示了一個JButton是一個ImageIcon存在,但是當我試圖找回它,它得到空
我試圖做一個「的System.out.println(((ImageIcon的)this.getIcon() ).getDescription());」在SameIcon類中,但是我所有的都是空的 –