当鼠标悬停在JButton上时,它变得不透明。

3
我正在尝试制作一个小游戏(只是为了消遣,因为我感到无聊),目前我已经有了一些面板、按钮、框架、图像等,并且它们都能够正常工作,但是我遇到了一个问题,过去几天我都无法解决,所以我想在这里问一下。
我的问题是,我想将JButton设置为透明,这样你只能看到放在上面的图片(左右箭头),它可以工作……除了当你将鼠标悬停在透明按钮上时,屏幕的另一部分会成为JButton的背景/前景,而不是仍然保持透明。
(白色斑点是我的鼠标在那个时间,我没有点击或做任何事情,它只是在与JButton交互时发生变化,特别注意较大的左箭头)
没有鼠标在JButton上的图像 - http://i.imgur.com/xWWE5E0.png 有鼠标在JButton上的图像 - http://i.imgur.com/8PosnwP.png 正如你所看到的,之前透明的JButton现在有了背景/前景,绝对不再透明。
JFrame的代码如下(我尝试制作了一个更小的演示,但在普通的新框架中它可以正常工作,所以这里是我的实际代码(请随时通过私信提供建议))。
package com.Braxeo.Games;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class CharacterNew 
{
static ImageIcon LArrow = new ImageIcon("Materials/LeftArrow.png");
static ImageIcon RArrow = new ImageIcon("Materials/RightArrow.png");
static ImageIcon bg = new ImageIcon("Materials/CharacterNewBackground.jpg");
public static JPanel CreateNewCharacter()
{
    JPanel contentPane = new JPanel();
    JPanel BackGround = new JPanel();
    JLabel background = new JLabel();

    JLabel shipshape = new JLabel();
    JButton shapeleft = new JButton();
    JButton shaperight = new JButton();
    JLabel shapetext = new JLabel();
    JPanel ShapeText = new JPanel();
    JPanel ShipShape = new JPanel();
    JPanel Shape = new JPanel();

    JLabel shipcolor = new JLabel();
    JButton colorleft = new JButton();
    JButton colorright = new JButton();
    JLabel colortext = new JLabel();
    JPanel ShipColor = new JPanel();
    JPanel ShipText = new JPanel();
    JPanel SColor = new JPanel();

    JLabel chartext = new JLabel();
    JLabel charicon = new JLabel();
    JButton iconleft = new JButton();
    JButton iconright = new JButton();
    JPanel iconpane = new JPanel();
    JPanel icontext = new JPanel();
    JPanel icon = new JPanel();

    JButton back = new JButton();
    JPanel backtext = new JPanel();

    JButton continu = new JButton();
    JPanel cont = new JPanel();

    JLabel charname = new JLabel();
    JTextField typename = new JTextField();
    JPanel charnamePane = new JPanel();



            // Shape
    ShapeText.setLayout(new GridLayout(1,1));
    ShipShape.setLayout(new GridLayout(1,3));
    Shape.setLayout(new BorderLayout());

    shipshape = new JLabel("ADD SSHIPS");
    shapeleft = new JButton(LArrow);
    shaperight = new JButton(RArrow);

    shapeleft.setContentAreaFilled(false);
    shapeleft.setOpaque(false);
    shapeleft.setBackground(new Color(0,0,0,0));


    shapetext = new JLabel("Choose Shape of Ship");
    ShipShape.setBackground(new Color(0,0,0,0));
    Shape.setBackground(new Color(0,0,0,0));
    Shape.setBounds(600, 100, 500, 400);
    ShipShape.add(shapeleft);
    ShipShape.add(shipshape);
    ShipShape.add(shaperight);
    ShapeText.add(shapetext);
    Shape.add(ShapeText, BorderLayout.PAGE_START);
    Shape.add(ShipShape);
    contentPane.add(Shape);
            // Color
    ShipColor.setLayout(new GridLayout(1, 3));
    ShipText.setLayout(new GridLayout(1,1));
    SColor.setLayout(new BorderLayout());

    colortext = new JLabel("  Choose Color of your Ship");
    shipcolor = new JLabel("ADD IMAGES");
    colorleft = new JButton("ADD LEFT ARROW");
    colorright = new JButton("ADD RIGHT ARROW");
    colortext.setFont(new Font("Serif", Font.BOLD, 32));
    ShipText.setBackground(new Color(0,0,0,0));
    colortext.setForeground(Color.red);
    SColor.setBackground(new Color(0,0,0,0));
    ShipText.setOpaque(true);
    ShipText.add(colortext);
    ShipColor.add(colorleft);
    ShipColor.add(shipcolor);
    ShipColor.add(colorright);
    SColor.setBounds(Main.x - 750,400,380,145);
    SColor.add(ShipText, BorderLayout.PAGE_START);
    SColor.add(ShipColor);
    contentPane.add(SColor);

            // Icon
    iconpane.setLayout(new GridLayout(1,3));
    icontext.setLayout(new GridLayout(1,1));
    icon.setLayout(new BorderLayout());

    chartext = new JLabel("  Pick Your Desired Icon");
    charicon = new JLabel("PLACE IMAGE");
    iconleft = new JButton("PLACE LEFT ICON");
    iconright = new JButton("PLACE right ICON");
    chartext.setFont(new Font("Serif", Font.BOLD, 32));
    icontext.setBackground(new Color(0,0,0,0));
    icon.setBackground(new Color(0,0,0,0));
    chartext.setForeground(Color.red);
    icontext.setOpaque(true);
    icontext.add(chartext);
    iconpane.add(iconleft);
    iconpane.add(charicon);
    iconpane.add(iconright);
    icon.setBounds(Main.x - 750,550,380,145);
    icon.add(icontext, BorderLayout.PAGE_START);
    icon.add(iconpane);
    contentPane.add(icon);

            // back
    backtext.setLayout(new BorderLayout());
    back = new JButton("Back");
    backtext.setBounds(50, Main.y-150, 252,76);
    back.addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent e)
        {
            Main.defaultframe.getContentPane().removeAll();
            Main.defaultframe.getContentPane().revalidate();
                          Main.defaultframe.setContentPane(CharacterSelect.CreateLoadCharacter());
            Main.defaultframe.setVisible(true);
        }
    });
    backtext.add(back);
    contentPane.add(backtext);

            // continue
    cont.setLayout(new BorderLayout());
    continu = new JButton("Continue");
    cont.setBounds(Main.x-302, Main.y-150, 252,76);
    cont.add(continu);
    contentPane.add(cont);

            // Name
    charnamePane.setLayout(new GridLayout( 2,1));
    charname = new JLabel("     Enter Your Name Below");
    charname.setFont(new Font("Impact", Font.BOLD, 32));
    typename.setSize(252, 76);
    typename.setFont(new Font("Razer Text Regular", Font.BOLD, 22));
    typename.setBackground(Color.LIGHT_GRAY);
    typename.setHorizontalAlignment(JLabel.CENTER);
    charnamePane.setBounds(100,200,400,100);        
    charnamePane.setOpaque(true);
    charnamePane.setBackground(new Color(0,0,0,0));
    charname.setBackground(new Color(0,0,0,0));
    charname.setForeground(Color.white);
    charnamePane.add(charname);
    charnamePane.add(typename, BorderLayout.PAGE_START);

    contentPane.add(charnamePane);

            // Background
    background = new JLabel(bg);
    BackGround.setLayout(new BorderLayout());
    BackGround.setBounds(0, 0, Main.x, Main.y);
    BackGround.add(background);
    contentPane.add(BackGround);


    contentPane.setLayout(new BorderLayout());
    return contentPane;
}

}

计算透明JButton的代码如下:

 JButton shapeleft = new JButton();
 JPanel ShapeText = new JPanel();
 JPanel Shape = new JPanel();
 shapeleft = new JButton(LArrow);
 shapeleft.setContentAreaFilled(false);
 shapeleft.setOpaque(false);
 shapeleft.setBackground(new Color(0,0,0,0));
 ShipShape.setBackground(new Color(0,0,0,0));
 Shape.setBackground(new Color(0,0,0,0));
 Shape.setBounds(600, 100, 500, 400);
 ShipShape.add(shapeleft);
 Shape.add(ShipShape);
 contentPane.add(Shape);

如果有人能帮我找出为什么它出现问题,那将是巨大的帮助,我还是一个初学者,所有的帮助都是好的帮助:)谢谢:)

1个回答

2
ShipShape.setBackground(new Color(0,0,0,0));

请查看带透明度的背景,了解可能出现的问题和几个解决方案。

基本上,您需要确保父组件在绘制您的组件背景之前进行绘制。


谢谢您的快速回复! :) 我尝试了您建议的方法,并找出了问题所在,尝试将alphacontainer应用到代码中,但仍然无法正常工作。最近我终于找到了解决方法。` ShipShape.setBackground(new Color(0,0,0,0)); shapeleft.setBackground(ShipShape.getBackground()); ShipShape.setOpaque(false); shapeleft.setOpaque(false); shapeleft.setContentAreaFilled(false); shapeleft.setBorderPainted(false);` - Braxeo

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