简体   繁体   中英

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

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

Image Without Mouse on JButton - http://i.imgur.com/xWWE5E0.png

Image With Mouse on 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

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

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 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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