有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java JButton alpha背景更改,鼠标悬停

我想在我的应用程序上有两个透明背景的按钮,我“几乎”做到了

这就是我所做的:

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) 个答案

  1. # 1 楼答案

    查看Backgrounds With Transparency了解问题的解释和一些解决方案

    基本的问题是,你的组件是不透明的,但背景是透明的,这打破了swing组件之间的绘画合同,所以你得到了绘画作品