时间:2011-09-29 08:58:31

标签: java awt

我想在图片上添加和事件。也就是说,当我点击图像时,它应该被另一个图像替换。

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);


    }   

}

我该怎么做?

1 个答案:

答案 0 :(得分:1)

只需在mouselistener onclick方法的图像中添加mouselistener即可添加代码...............

http://www.anyexample.com/programming/java/java_full_screen_image_viewer.xml