Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/396.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Jbutton在Jbutton背景图像上?_Java_Swing_Jpanel_Jbutton_Sprite - Fatal编程技术网

Java Jbutton在Jbutton背景图像上?

Java Jbutton在Jbutton背景图像上?,java,swing,jpanel,jbutton,sprite,Java,Swing,Jpanel,Jbutton,Sprite,假设我用jbutton创建了一个2d平铺贴图,然后在贴图顶部创建了单位。当单位(也是jbutton)位于平铺顶部时,有没有办法显示贴图的背景,因为现在单位的背景颜色只是红色的,所以可以用jbutton覆盖jbutton吗?可能,是的,明智啊,可能不是 我认为您需要将标题按钮的布局更改为您可以控制的布局(这将取决于您的视觉要求) 一、 就个人而言,他可能会选择一个带有标签的面板,使用鼠标监听器监视鼠标动作&可能是键盘交互的输入/动作图 JButton只是jcomponent,因此它们拥有jcomp

假设我用jbutton创建了一个2d平铺贴图,然后在贴图顶部创建了单位。当单位(也是jbutton)位于平铺顶部时,有没有办法显示贴图的背景,因为现在单位的背景颜色只是红色的,所以可以用jbutton覆盖jbutton吗?

可能,是的,明智啊,可能不是

我认为您需要将标题按钮的布局更改为您可以控制的布局(这将取决于您的视觉要求)

一、 就个人而言,他可能会选择一个带有标签的面板,使用鼠标监听器监视鼠标动作&可能是键盘交互的输入/动作图


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:谢谢并保持微笑:-)