鼠标悬停时更改JButton透明度背景

3
我希望在我的应用程序中有两个透明背景的按钮,我已经“几乎”完成了。
这是我所做的:
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));
}

这是我目前的视觉效果: enter image description here 很好,这正是我想要的。但当我将鼠标移到这两个按钮上时,会发生以下情况: enter image description here 首先,背景逐渐变得不透明,每次鼠标移动时都会变得更少,然后我们可以看到两个按钮的文本混在一起。
非常感谢您的回答。

1
你尝试过 repaint() 吗? - Madhawa Priyashantha
@madhawa priyashantha 不是JButton有三种状态。 - mKorbel
1个回答

2
请查看带透明度的背景,了解问题及其解决方案。
基本问题在于您的组件是不透明的,但是背景具有透明度,这会破坏swing组件之间的绘画协议,因此会出现绘画伪影。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接