我正在尝试制作一款小游戏(只是为了好玩,因为我感到无聊)而且目前我还有一些面板,按钮,框架,图像以及其中已有的功能,但我来了对于过去几天我无法弄清楚的问题所以我想我会问这里
我遇到的问题是我想要将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);
如果有人能帮我弄清楚为什么它的播放就像是一个巨大的帮助,我仍然是初学者所以所有的帮助都是很好的帮助:)谢谢:)
答案 0 :(得分:2)
ShipShape.setBackground(new Color(0,0,0,0));
查看Background With Transparency可能出现的问题以及几个解决方案。
基本上,您需要确保在绘制组件背景之前绘制父组件。