简体   繁体   English

用鼠标悬停改变JButton alpha背景

[英]JButton alpha background change with mouseover

I want to have 2 buttons on my application with a transparent background and I "almost" done it. 我想在我的应用程序上有2个按钮,透明背景,我“差点”完成它。

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 : 但当我将鼠标移到2个按钮上时,就会发生这种情况:

在此输入图像描述

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. 提前感谢您的回答。

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. 基本问题是你的组件是不透明的,但背景具有透明度,这会破坏摆动组件之间的绘画契约,因此你会得到绘画工件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM