【问题标题】:How to set transparent png to JButton?如何将透明png设置为JButton?
【发布时间】:2012-07-26 02:46:36
【问题描述】:

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

【问题讨论】:

  • 为了获得更好的帮助,请尽快发布 SSCCE,

标签: java swing jbutton


【解决方案1】:

试试这个:

button.setOpaque(false);
button.setContentAreaFilled(false);
button.setBorderPainted(false);

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案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); 即可查看差异:-)

      【讨论】:

      • +1 表示sscce;可悲的是,UI 委托,例如com.apple.laf.AquaLookAndFeel 可能会否决你。
      • 啊哈,刚刚学习如何在 Swing 中绘画,所以不能像往常一样质疑您的判断力 :-) 。那么有什么很好的例子可以解决这种情况吗?
      • 抱歉,我不知道一个好的跨平台方法; Java 7 试图解决这个问题。不使用setBackground(),而是使用半透明背景在g2 中合成图像,类似于hereCLEAR 的完成方式。
      【解决方案4】:

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

      这对你有很大帮助。欲了解更多信息,您可以点击此链接

      Jbutton Tutorial

      Jbutton Class

      【讨论】:

        【解决方案5】:

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

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

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

        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);
        

        【讨论】:

          猜你喜欢
          • 2013-02-27
          • 1970-01-01
          • 2011-06-02
          • 2011-06-26
          • 2011-11-29
          • 2015-04-17
          • 1970-01-01
          • 2014-04-05
          • 1970-01-01
          相关资源
          最近更新 更多