以上的JButton JButton的背景图片?(Jbutton over Jbutton back

2019-06-26 07:44发布

比方说,我创建了一个2D瓦地图Jbuttons中,然后在地图之上创建单位有没有办法来显示地图的背景当单元(也一个JButton)是在瓷砖之上,因为它是如何现在在该单元的背景仅仅是红色,所以是有可能超过Jbuttons中Jbuttons中做到这一点?

Answer 1:

可能是这个缘故,可取的,啊,大概不会。

我相信你会需要更改标题按钮的东西,你可以控制的布局(这要取决于你的视觉要求)。

我个人而言,可能会去里面坐了一个标签面板,使用鼠标监听器来监视鼠标动作可能与输入/动作映射键盘交互。

Jbuttons中只是JComponent的,所以他们得到所有jcomponents具备的功能



Answer 2:

如果朵蒙特的JButton是Translucent可以解决你的目的,这里是一个示例代码,你如何能做到这一点。 简单地改变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);          
    }
}

下面是相同的输出:



文章来源: Jbutton over Jbutton background image?