【问题标题】:JButton turns non-transparent when hovered overJButton 悬停时变为不透明
【发布时间】:2014-01-28 21:01:17
【问题描述】:

我正在尝试制作一个小游戏(只是为了好玩,因为我觉得无聊),目前我有一些面板、按钮、框架、图像和其他东西已经很好用了,但我来了过去几天我无法弄清楚的问题,所以我想我会在这里问

我遇到的问题是我想设置一个透明的 JButton,以便您看到的只是放置在其上的图像(左右箭头),并且它可以工作....除非您将鼠标悬停在透明按钮,屏幕的不同部分成为 JButton 的背景/前景,而不是它仍然是透明的

(白点是我的鼠标当时所在的位置,我没有点击或任何其他东西,它只是在您与 JButton 交互时发生变化,具体请查看较大的左箭头)

JButton 上没有鼠标的图像 - http://i.imgur.com/xWWE5E0.png

鼠标在 JButton 上的图像 - http://i.imgur.com/8PosnwP.png

如您所见,之前透明的 JButton 没有背景/前景,而且肯定不再透明

JFrame 的代码是这样的(我尝试制作一个较小的演示,但它可以在一个普通的新框架中正常工作,所以这是我的实际代码(请随时通过 PM 提供改进它的提示))

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

如果有人能帮我弄清楚为什么它会像现在这样播放,那将是一个巨大的帮助,而且我还是一个初学者,所以所有的帮助都是很好的帮助:)谢谢:)

【问题讨论】:

    标签: java swing jpanel jbutton transparent


    【解决方案1】:
    ShipShape.setBackground(new Color(0,0,0,0));
    

    查看Background With Transparency 了解可能的问题和一些解决方案。

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

    【讨论】:

    • 感谢您的回复,这么快!! :) 我尝试了你的建议,我确实弄清楚发生了什么,并试图将 alphacontainer 应用到代码中,但它仍然没有工作,就在最近我确实弄清楚了如何让它工作` ShipShape.setBackground(新颜色(0,0,0,0)); shapeleft.setBackground(ShipShape.getBackground()); ShipShape.setOpaque(false); shapeleft.setOpaque(false); shapeleft.setContentAreaFilled(false); shapeleft.setBorderPainted(false); `
    猜你喜欢
    • 2020-11-17
    • 1970-01-01
    • 1970-01-01
    • 2012-07-27
    • 2011-04-04
    • 2011-11-26
    • 2018-01-23
    • 2018-10-11
    • 2014-02-21
    相关资源
    最近更新 更多