3

我正在使用 swing 制作 Java 桌面应用程序。我想将 png 设置为 jbutton。但我无法设置透明图像。我想像在 android 中那样设置背景 null 以便可以设置透明图像。

4

5 回答 5

9

尝试这个 :

button.setOpaque(false);
button.setContentAreaFilled(false);
button.setBorderPainted(false);
于 2012-07-27T12:15:44.090 回答
3

看看这个示例程序,这是你要求的吗?

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.*;

public class ButtonTransparentImage
{
    private BufferedImage originalImage, modifiedImage;
    private ImageIcon image;

    private JButton imageButton;

    private void displayGUI()
    {
        JFrame frame = new JFrame("Transparent Image on JButton");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        getModifiedImage();
        image = new ImageIcon(modifiedImage);
        imageButton = new JButton(image);
        imageButton.setBackground(Color.GREEN.darker());

        JPanel contentPane = new JPanel();
        contentPane.add(imageButton);

        frame.setContentPane(contentPane);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    private void getModifiedImage()
    {
        try
        {
            originalImage = ImageIO.read(
                new URL("http://gagandeepbali.uk.to/" + 
                    "gaganisonline/images/swing/stackoverflow/geek3.gif"));
            modifiedImage = new BufferedImage(
                originalImage.getWidth(),
                originalImage.getHeight(),
                BufferedImage.TYPE_INT_ARGB);       
        }
        catch(IOException ioe)
        {
            System.out.println("Unable to read the Content of the Image.");
            ioe.printStackTrace();
        }

        Graphics2D g2 = modifiedImage.createGraphics();
        AlphaComposite newComposite = 
            AlphaComposite.getInstance(
                AlphaComposite.SRC_OVER, 0.5f);
        g2.setComposite(newComposite);      
        g2.drawImage(originalImage, 0, 0, null);
        g2.dispose();
    }

    public static void main(String... args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                new ButtonTransparentImage().displayGUI();
            }
        });
    }
}

输出 :

透明图像

只需更改此行image = new ImageIcon(modifiedImage);即可image = new ImageIcon(originalImage);查看差异:-)

于 2012-07-27T14:01:53.323 回答
3

尝试button.setIcon(new ImageIcon(ImageIO.read(new File("path/to/image.png"))))

于 2012-07-27T12:25:32.740 回答
2

ImageIcon cup = new ImageIcon("images/cup.png"); JButton button2 = new JButton(cup);

这会帮助你很多。有关更多信息,您可以单击此链接

Jbutton 教程

按钮类

于 2012-07-27T12:32:47.063 回答
1

要使用透明 PNG 创建一个JButton,我使用:

JButton jButton1 = new JButton(new ImageIcon(ImageIO.read(new File("yourImage.png")  

JButton要使用缩放的透明 PNG创建一个,我使用:

ImageIcon image = new ImageIcon("yourImage.png") 
JButton jButton1 = new JButton(new ImageIcon(getScaledImage(icon.getImage(), 32, 32)));


/**
 * Resizes an image using a Graphics2D object backed by a BufferedImage.
 * @param srcImg - source image to scale
 * @param w - desired width
 * @param h - desired height
 * @return - the new resized image
 */
private Image getScaledImage(Image srcImg, int w, int h){
    BufferedImage resizedImg = new BufferedImage(w, h, BufferedImage.TRANSLUCENT);
    Graphics2D g2 = resizedImg.createGraphics();
    g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
    g2.drawImage(srcImg, 0, 0, w, h, null);
    g2.dispose();
    return resizedImg;
}

然后,如果您不想要可见的边框,请使用:

jButton1.setOpaque(false);
jButton1.setBorderPainted(false);
jButton1.setContentAreaFilled(false);
于 2012-07-27T13:02:43.143 回答