小编use*_*r18的帖子

JButton中不透明像素的事件检测

我有一个类扩展JButton,我试图应用.png图像.图像形状不规则,并被透明像素包围.我已经覆盖了paintComponent()方法,在JButton我的缓冲图像应用到按钮.现在,图像是唯一被绘制的东西,这就是我想要的.

但是,按钮仍然会检测周围矩形中的事件.有没有办法将检测限制在仅包含不透明像素的区域(或者更确切地说检测透明像素上的事件)?

按钮类的代码如下.

public class DrawButton extends JButton{

    private BufferedImage bi;

    public DrawButton(BufferedImage bi){
        setPreferredSize(new Dimension(bi.getWidth(), bi.getHeight()));
        this.bi = bi;
    }

    @Override
    protected void paintComponent(Graphics g){
        g.drawImage(bi, 0, 0, null);
        g.dispose();
    }
}
Run Code Online (Sandbox Code Playgroud)

java swing jbutton

2
推荐指数
1
解决办法
365
查看次数

标签 统计

java ×1

jbutton ×1

swing ×1