Jbutton over Jbutton background image?

一笑奈何 提交于 2019-12-02 02:32:07

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

If the Topmost JButton being Translucent can solve your purpose, here is one example code, how you can do that. Simply change the AlphaComposite values i.e. 0.7f, used in my case, to whatever deemed fit for your instance of the code :

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 :

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!