【问题标题】:JButton alpha background change with mouseoverJButton alpha 背景随鼠标悬停而变化
【发布时间】:2014-08-06 14:04:59
【问题描述】:

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

这就是我所做的:

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));
}

这就是我在视觉上拥有的:

那太好了,这就是我想要的。但是当我将鼠标移到 2 个按钮上时,会发生这种情况:

因此,每次鼠标移到背景上时,背景都会变得越来越不透明,然后我们可以看到两个按钮的文本混合在一起。

提前感谢您的回答。

【问题讨论】:

  • 你尝试过 repaint()
  • @madhawa priyashantha not JButton 有三种状态

标签: java swing background jbutton alpha


【解决方案1】:

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

基本问题是您的组件是不透明的,但背景具有透明度,这会破坏摆动组件之间的绘画合同,因此您会得到绘画工件。

【讨论】:

    猜你喜欢
    • 2013-08-03
    • 1970-01-01
    • 2013-09-05
    • 1970-01-01
    • 2013-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多