简体   繁体   English

Jbutton在Jbutton背景图片上吗?

[英]Jbutton over Jbutton background image?

可以说我创建了一个带有jbutton的2d瓦片地图,然后在地图顶部创建了单位,当单位(也是jbutton)位于瓦片上方时,有一种方法可以显示地图背景,因为现在单元的背景只是被涂成红色,是否可以通过jbuttons而不是jbuttons来做到这一点?

If the Topmost JButton being Translucent can solve your purpose, here is one example code, how you can do that. 如果最Translucent的TopButton JButton可以解决您的问题,那么下面是一个示例代码,介绍了如何实现此目的。 Simply change the AlphaComposite values ie 0.7f , used in my case, to whatever deemed fit for your instance of the code : 只需将在本0.7f中使用的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);          
    }
}

Here is the output of the same : 这是相同的输出:

透明按钮

Possible, yes, advisable, ah, probably not. 可能,是的,建议这样做,嗯,可能不是。

I believe you're going to need to change the layout of the title button to something you can control (this is going to depend upon your visual requirements). 我相信您将需要将标题按钮的布局更改为可以控制的内容(这将取决于您的视觉要求)。

I, personally, would probably go for a panel with a label inside it, using mouse listeners to monitor for mouse actions & probably input/action maps for keyboard interaction. 我个人而言,可能会选择一个带有标签的面板,使用鼠标侦听器监视鼠标动作,并可能监视键盘交互的输入/动作图。

Jbuttons are just jcomponent, so they get all the functionality that jcomponents have Jbutton只是jcomponent,因此它们获得了jcomponent具有的所有功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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