简体   繁体   English

当悬停在上面时,JButton变得不透明

[英]JButton turns non-transparent when hovered over

I'm trying to make a small game (just for fun because i get bored) and at the current time i have a few panels, buttons, frames, images and whatnot already in it working well, but i'm come to a problem that i couldn't figure out for the past few days so i thought i'd ask here 我正在尝试制作一个小游戏(只是为了好玩,因为我觉得无聊)而且在当前我有一些面板,按钮,框架,图像以及其中已经运行良好的东西,但我遇到了问题这几天我无法弄清楚所以我想我会问这里

The problem i have is i want to set a JButton transparent so that all you see is the image that is placed onto it (left and right arrows), and it works....except for when you hover your mouse over the transparent button, a different part of the screen becomes the background/foreground of the JButton instead of it still being transparent 我遇到的问题是我想将JButton设置为透明,这样你所看到的就是放在它上面的图像(左箭头和右箭头),它可以正常工作....除非你将鼠标悬停在透明按钮上,屏幕的不同部分成为JButton的背景/前景,而不是仍然是透明的

(The White Spots are where my mouse is at the time, i do not click or anything, it just changes when you interact with the JButton, look specifically at the larger left arrow) (White Spots是我当时鼠标所在的位置,我没有点击或任何东西,它只是在你与JButton交互时改变,特别注意较大的左箭头)

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

Image With Mouse on JButton - http://i.imgur.com/8PosnwP.png 在JButton上使用鼠标的图像 - http://i.imgur.com/8PosnwP.png

As you can see the previously transparent JButton has not got a background/foreground and most definitely not transparent anymore 正如你所看到的,以前透明的JButton没有背景/前景,而且绝对不再透明了

The code for the JFrame is like this (i tried to make a smaller demo but it works properly in a plain, new frame so here's my actual code(feel free to give tips on improving it by PM)) 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;
}

} }

The code that works out the Transparent JButton is 解决透明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);

If anyone could please help me figure out why its playing up like it is that would be a massive help, and i'm still a beginner so all help is good help :) thanks :) 如果有人可以请帮助我弄清楚为什么它的播放就像是一个巨大的帮助,我仍然是一个初学者所以所有的帮助是很好的帮助:)谢谢:)

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

Check out Background With Transparency for the probable problem and a couple of solutions. 查看具有透明度的背景 ,了解可能的问题和几个解决方案。

Basically you need to make sure the parent component gets painted before you paint your components background. 基本上,您需要确保在绘制组件背景之前绘制父组件。

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

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