悬停在JButton上的奇怪的颜色变化 [英] Weird color changes on hovering on JButton

查看:246
本文介绍了悬停在JButton上的奇怪的颜色变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,这是一个问题,当我想使用透明度时,我偶然发现了。



因此,改变hover背景的代码是... ...



  received.setMouseListener(new MouseAdapter()
@Override
public void mouseEntered(MouseEvent me)
{
received.setBackground(new Color(50,50,50,100));
}
});

开始时我为按钮设置蓝色。



这里是显示颜色变化的gif ...



GifMeme09541718022016.gif

  public class FakeTransperencyButton extends JButton {

private float alpha = 0;

public FakeTransperencyButton(String text){
super(text);
setOpaque(false);
setBackground(Color.RED);

addMouseListener(new MouseAdapter(){
@Override
public void mouseEntered(MouseEvent e){
alpha = 0.4f;
repaint
}

@Override
public void mouseExited(MouseEvent e){
alpha = 0f;
repaint();
}

});
}

@Override
public boolean isOpaque(){
return false;
}

public float getAlpha(){
return alpha;
}

protected void paintComponent(Graphics g){
Graphics2D g2d =(Graphics2D)g.create();
g2d.setComposite(AlphaComposite.SrcOver.derive(getAlpha()));
g2d.setColor(getBackground());
g2d.fillRect(0,0,getWidth(),getHeight());
g2d.dispose();
super.paintComponent(g);
}

}


Ok so this was a problem I stumbled upon when I wanted to use transparency..

So the code for changing background on hover is this...

 received.setMouseListener(new MouseAdapter()
 @Override 
 public void mouseEntered(MouseEvent me) 
 {
        received.setBackground(new Color(50,50,50,100));
 } 
});

At the beginning I set the blue color for the button..

Here's the gif showing the color changes...

GifMeme09541718022016.gif https://drive.google.com/file/d/0B9XFyaTVy8oYci1zMmRhMmtYcnM/view?usp=docslist_api

Why does this happen? If this is not a correct approach what is the correct approach?

解决方案

Basically, Swing only understand how to paint transparent and opaque components, it doesn't know how to deal with translucent components, so using an alpha based background color causes issues.

Instead, you need to "fake" it by taking control over how the component's background is painted, for example...

public class FakeTransperencyButton extends JButton {

    private float alpha = 0;

    public FakeTransperencyButton(String text) {
        super(text);
        setOpaque(false);
        setBackground(Color.RED);

        addMouseListener(new MouseAdapter() {
            @Override
            public void mouseEntered(MouseEvent e) {
                alpha = 0.4f;
                repaint();
            }

            @Override
            public void mouseExited(MouseEvent e) {
                alpha = 0f;
                repaint();
            }

        });
    }

    @Override
    public boolean isOpaque() {
        return false;
    }

    public float getAlpha() {
        return alpha;
    }

    protected void paintComponent(Graphics g) {
        Graphics2D g2d = (Graphics2D) g.create();
        g2d.setComposite(AlphaComposite.SrcOver.derive(getAlpha()));
        g2d.setColor(getBackground());
        g2d.fillRect(0, 0, getWidth(), getHeight());
        g2d.dispose();
        super.paintComponent(g);
    }

}

这篇关于悬停在JButton上的奇怪的颜色变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆