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

时间:2019-05-02 13:18:16

标签: 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();
    }
}

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

感谢您提前提供帮助^^

2 个答案:

答案 0 :(得分:1)

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

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

然后更改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

答案 1 :(得分:1)

在按钮上尝试以下方法:

var $container = $('div[id$="Login_DNN"]');

$container.keydown(function (event) {
    alert('key press = ' + event.which); //<--- This pops up for all keys but enter

    if(event.which===13){
        //This only works for modern browsers
        if (HTMLElement.prototype.click) {
            $container.find('a[id$="cmdLogin"]')[0].click();
        }
    };
});