【问题标题】:Let the transparent part of the image on a button be transparent让按钮上图像的透明部分透明
【发布时间】:2019-09-21 00:46:24
【问题描述】:

我使用了 2 张图片,其中一张是屏幕背景,另一张是按钮背景。按钮的图像在某些部分是透明的。我想让它覆盖透明部分的背景图片

它应该看起来像this,但事实并非如此。看起来像this

这是我的代码:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class TryOut1 extends JFrame
{

   public TryOut1()
   {
       screen();
       buttonDoor();

       setSize(1280,1024); 
   }

   public void screen(){
       setSize(1280,1024);

       setDefaultCloseOperation(EXIT_ON_CLOSE);
       setVisible(true);
       setLayout(new BorderLayout());
       setContentPane(new JLabel(new ImageIcon("Ziegel4.jpg")));
       setLayout(new FlowLayout());

       setSize(1280,1024);
   }

   public void buttonDoor(){
       JButton b1 = new JButton(new ImageIcon("Tor2.png"));
       b1.setEnabled(true);
       b1.setVisible(true);
       b1.setBackground(new Color( 0, 0, 0, 0) );


       add(b1);

       b1.addActionListener(new ActionListener(){
       public void actionPerformed(ActionEvent e){
           dispose();
       }
       });
   }

   public static void main(String args[]) 
    {
        new TryOut1();
    }
}

我怎样才能使图像的透明部分真正透明

提前感谢您的帮助^^

【问题讨论】:

    标签: java swing background awt transparent


    【解决方案1】:

    在您的按钮上尝试以下方法:

    b1.setBorderPainted(false);
    b1.setContentAreaFilled(false);
    b1.setFocusPainted(false);
    b1.setOpaque(false);
    

    【讨论】:

    • 好电话。这就是我在this answer 中使用的技术,它从单个图像中形成 5 个按钮和 4 个标签(查看链接,它并不像听起来那样不合逻辑)。
    【解决方案2】:

    与其创建JLabel,不如为其添加背景图像,然后将此标签添加到您的内容窗格,这不是最好的方法。标签将被视为一个组件,布局管理器不会正确定位所有其他组件。

    您应该通过覆盖paintComponent(Graphics g) 方法来绘制内容窗格的背景图像,如here. 所示

    然后更改 JButton 的正确属性并使其透明,如图所示 here.

    所有这些都在SSCCE

    public class TryOut1 extends JFrame {
    
        public TryOut1() {
            try {
                screen();
            } catch (IOException e) {
                e.printStackTrace();
            }
            buttonDoor();
    
            setSize(1280, 1024);
        }
    
        public void screen() throws IOException {
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            setLayout(new BorderLayout());
    
            File desktop = new File(System.getProperty("user.home"), "Desktop");
            File backgroundImg = new File(desktop, "background.png");
            Image img = ImageIO.read(backgroundImg);
            JPanel contentPane = new JPanel(new FlowLayout()) {
                @Override
                protected void paintComponent(Graphics g) {
                    super.paintComponent(g);
                    g.drawImage(img, 0, 0, null);
                }
            };
            setContentPane(contentPane);
        }
    
        public void buttonDoor() {
            File desktop = new File(System.getProperty("user.home"), "Desktop");
            File doorFile = new File(desktop, "door.png");
            JButton b1 = new JButton(new ImageIcon(doorFile.getAbsolutePath()));
            b1.setOpaque(false);
            b1.setContentAreaFilled(false);
            b1.setBorder(BorderFactory.createEmptyBorder());
            b1.setBorderPainted(false);
            add(b1);
    
            b1.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    dispose();
                }
            });
        }
    
        public static void main(String args[]) {
            // All swing apps must run in their own thread.
            SwingUtilities.invokeLater(() -> new TryOut1().setVisible(true));
        }
    }
    

    预览:(忽略右边的空白,我的背景图很小)

    【讨论】:

    • all other components won't be oriented properly by the layout manager. - 如果您设置标签的布局管理器,他们会这样做。鉴于上面的代码以原始大小绘制图像,如果事实上使用 JLabel 更好,因为现在标签将具有图像的首选大小,并且框架将正确显示和 pack()。所以在这种情况下,我只会使用 JLabel。当您进行自定义绘画时,您还负责确定组件的首选尺寸。如果您需要缩放/平铺背景图像,我只会使用自定义绘画。
    猜你喜欢
    • 2020-06-11
    • 2013-08-04
    • 2010-10-02
    • 2012-02-24
    • 1970-01-01
    • 2021-09-18
    • 1970-01-01
    • 2014-10-06
    • 2010-12-01
    相关资源
    最近更新 更多