java 如何将透明png设置为JButton?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11687527/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set transparent png to JButton?
提问by Android Dev
I am making java desktop application using swing. I want to set png to jbutton. but i can't set transparent image. I want to do as in android like set background null so transparent image can be set.
我正在使用 Swing 制作 Java 桌面应用程序。我想将 png 设置为 jbutton。但我无法设置透明图像。我想像在android中一样设置背景为空,以便可以设置透明图像。
回答by Mennan
Try this :
试试这个 :
button.setOpaque(false);
button.setContentAreaFilled(false);
button.setBorderPainted(false);
回答by James
try button.setIcon(new ImageIcon(ImageIO.read(new File("path/to/image.png"))))
尝试 button.setIcon(new ImageIcon(ImageIO.read(new File("path/to/image.png"))))
回答by nIcE cOw
Have a look at this example program, is this what you asking for ?
看看这个示例程序,这是你要求的吗?
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();
}
});
}
}
OUTPUT :
输出 :
Simply change this line image = new ImageIcon(modifiedImage);
to image = new ImageIcon(originalImage);
to see the difference :-)
只需改变这一行image = new ImageIcon(modifiedImage);
来image = new ImageIcon(originalImage);
看看二者的区别:-)
回答by Saunik Singh
ImageIcon cup = new ImageIcon("images/cup.png"); JButton button2 = new JButton(cup);
ImageIcon cup = new ImageIcon("images/cup.png"); JButton button2 = new JButton(cup);
This will help you lot. for more information you can click on this link
这会对你有很大帮助。有关更多信息,您可以单击此链接
回答by alain.janinm
To create a JButton
with a transparent PNG, I use :
要JButton
使用透明 PNG创建一个,我使用:
JButton jButton1 = new JButton(new ImageIcon(ImageIO.read(new File("yourImage.png")
To create a JButton
with a scaledtransparent PNG, I use :
要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;
}
Then if you don't want visible border use :
然后,如果您不想要可见边框,请使用:
jButton1.setOpaque(false);
jButton1.setBorderPainted(false);
jButton1.setContentAreaFilled(false);