Java Jbutton在Jbutton背景图像上?
假设我用jbutton创建了一个2d平铺贴图,然后在贴图顶部创建了单位。当单位(也是jbutton)位于平铺顶部时,有没有办法显示贴图的背景,因为现在单位的背景颜色只是红色的,所以可以用jbutton覆盖jbutton吗?可能,是的,明智啊,可能不是 我认为您需要将标题按钮的布局更改为您可以控制的布局(这将取决于您的视觉要求) 一、 就个人而言,他可能会选择一个带有标签的面板,使用鼠标监听器监视鼠标动作&可能是键盘交互的输入/动作图Java Jbutton在Jbutton背景图像上?,java,swing,jpanel,jbutton,sprite,Java,Swing,Jpanel,Jbutton,Sprite,假设我用jbutton创建了一个2d平铺贴图,然后在贴图顶部创建了单位。当单位(也是jbutton)位于平铺顶部时,有没有办法显示贴图的背景,因为现在单位的背景颜色只是红色的,所以可以用jbutton覆盖jbutton吗?可能,是的,明智啊,可能不是 我认为您需要将标题按钮的布局更改为您可以控制的布局(这将取决于您的视觉要求) 一、 就个人而言,他可能会选择一个带有标签的面板,使用鼠标监听器监视鼠标动作&可能是键盘交互的输入/动作图 JButton只是jcomponent,因此它们拥有jcomp
JButton只是jcomponent,因此它们拥有jcomponents所拥有的所有功能如果最顶端的JButton是半透明的,可以解决您的问题,下面是一个示例代码,您可以如何做到这一点。只需将本案例中使用的
AlphaComposite
值(即0.7f
)更改为适合您的代码实例的值:
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.FlowLayout;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import javax.swing.*;
public class TransparentButton
{
private CustomButton button;
private ImageIcon backgroundImage;
private void displayGUI()
{
JFrame frame = new JFrame("Transparent Button");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel contentPane = new JPanel();
contentPane.setOpaque(true);
contentPane.setBackground(Color.BLUE);
try
{
backgroundImage = new ImageIcon(
new URL("http://gagandeepbali.uk.to/" +
"gaganisonline/images/404error.jpg"));
}
catch(IOException ioe)
{
ioe.printStackTrace();
}
JButton baseButton = new JButton(backgroundImage);
baseButton.setOpaque(true);
baseButton.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));
button = new CustomButton("Transparent Button");
baseButton.add(button);
contentPane.add(baseButton);
frame.setContentPane(contentPane);
frame.setSize(300, 300);
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String... args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new TransparentButton().displayGUI();
}
});
}
}
class CustomButton extends JButton
{
private BufferedImage buttonImage = null;
public CustomButton(String title)
{
super(title);
setOpaque(false);
}
@Override
public void paint(Graphics g)
{
if (buttonImage == null ||
buttonImage.getWidth() != getWidth() ||
buttonImage.getHeight() != getHeight())
{
buttonImage = (BufferedImage) createImage(
getWidth(), getHeight());
}
Graphics gButton = buttonImage.getGraphics();
gButton.setClip(g.getClip());
super.paint(gButton);
/*
* Make the graphics object sent to
* this paint() method translucent.
*/
Graphics2D g2 = (Graphics2D) g;
AlphaComposite newComposite =
AlphaComposite.getInstance(
AlphaComposite.SRC_OVER, 0.7f);
g2.setComposite(newComposite);
/*
* Copy the JButton's image to the destination
* graphics, translucently.
*/
g2.drawImage(buttonImage, 0, 0, null);
}
}
以下是相同的输出:
是的,我知道,但我的其他队友使用的是jbuttons而不是jbuttons,我想看看是否有一种简单的方法可以做到类似于JPanels的效果。看,这很好。jbuttin只是一个jcomponent,这意味着您可以添加其他组件,并调整布局以满足您的需要。如果您将最上面的jbuttin
制作成半透明的,对您的情况会怎么样?这是一个很好的建议,请使用不透明的属性,可能与我想要的不完全相同,它更像是背景上的一个单位,单位的图像是空的,背景是显示的,所以基本上像是另一个图像上的叠加图像,但是谢谢你的帮助。似乎对于你想要的场景,JButton
真的不是正确的选择,正如@MadProgrammer所描述的,我猜答案是千里迢迢的:——)@GagandeepBali很棒+1@joeyrohan:谢谢并保持微笑:-)