java - 让按钮上图像的透明部分透明

标签 java swing background awt transparent

我使用了两张图片,其中一张是屏幕背景,另一张是按钮背景。按钮图像的某些部分是透明的。我希望它能够覆盖透明部分的背景图像

它应该看起来像 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();
    }
}

如何才能使图像的透明部分真正透明

提前感谢您的帮助^^

最佳答案

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

您应该通过重写 paintComponent(Graphics g) 方法来绘制内容 Pane 的背景图像,如下所示 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));
    }
}

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

preview

关于java - 让按钮上图像的透明部分透明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55953407/

相关文章:

css - 如何为文本添加渐变背景(多行)

java - 给物体补水是什么意思?

java - java中如何将字符串转换为8位ascii?

java - 为什么 PostgreSQL JDBC 驱动程序在身份验证期间向服务器发送错误的数据包 header ?

java - 如何在 JOptionPane 选项对话框中使用 JSlider(或其他 JComponent)?

Java Swing MVC 动态组件

objective-c - 注销时的 MBProgressHUD

java - 如何在 Http POST 请求中发送图像文件? ( java )

java - 我怎么只能关闭JDialog框?

html - 背景图片网址未显示图片