JButton alpha background change with mouseover

你说的曾经没有我的故事 提交于 2020-01-03 20:04:13

问题


I want to have 2 buttons on my application with a transparent background and I "almost" done it.

This is what I've done :

public class PanelMenu extends JPanel{

//VARIABLES
private JButton buttonFightSimulator, buttonMatchUp;

//CONSTRUCTORS
public PanelMenu ()
{
    this.setBounds(0,0,240,768);
    this.setLayout(new FlowLayout(0, 0, 0));

    //BUTTON CREATION
    buttonFightSimulator = new JButton("FIGHT SIMULATOR");
    buttonMatchUp = new JButton("MATCH UP");

    buttonFightSimulator.setBackground(new Color(255,255,255,128));
    buttonFightSimulator.setFocusPainted(false);
    buttonFightSimulator.setBorderPainted(false);
    buttonFightSimulator.setPreferredSize(new Dimension(240,60));

    buttonMatchUp.setBackground(new Color(255,255,255,128));
    buttonMatchUp.setFocusPainted(false);
    buttonMatchUp.setBorderPainted(false);
    buttonMatchUp.setPreferredSize(new Dimension(240,60));

    add(buttonFightSimulator);
    add(buttonMatchUp);
    this.setBackground(new Color(0,0,0,90));
}

And this is what I visually have :

Well that's great that's what I wanted. But when I pass my mouse over the 2 buttons, this is what happen :

So first the background get less and less transparent every time the mouse moves over it and then we can see that the text of the both buttons are mixed together.

Thank you in advance for your answer.


回答1:


Check out Backgrounds With Transparency for an explanation of the problem and a couple of solutions.

The basic problem is that your component is opaque but the background has transparency which breaks the painting contract between swing components so you get painting artifacts.



来源:https://stackoverflow.com/questions/25162510/jbutton-alpha-background-change-with-mouseover

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!