我想在圖像上添加事件。也就是說,當我點擊圖片時,它應該被替換爲另一張圖片。如何爲圖像添加事件
import java.awt.Canvas;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class RadioButtonLoader extends Canvas{
private static final long serialVersionUID = 1L;
BufferedImage image = null;
public RadioButtonLoader(){
try {
this.image = ImageIO.read(new File("res/rb1.gif"));
} catch (IOException ex) {
ex.printStackTrace();
}
}
public RadioButtonLoader(String checked){
try {
this.image = ImageIO.read(new File("res/rb2.gif"));
} catch (IOException ex) {
ex.printStackTrace();
}
}
@Override
public void setSize(Dimension d) {
super.setSize(new Dimension(15,22));
}
@Override
public void paint(Graphics g) {
setSize(new Dimension(15,22));
g.drawImage(image, 0, 0, null);
}
}
我該怎麼辦?
怎麼樣'MouseListener'? –
您現在正在設置'paint()'中的大小嗎?!?仍然使用'null'作爲'ImageObserver'?你需要多少次被告知? –