如何使图像显示使JFrame背景和JPanel透明

时间:2014-02-05 15:40:26

标签: java swing jframe jpanel

嘿我正在尝试制作某种发射器而且“窗口”必须是透明的,因为如果你理解我的意思,我希望我用的图像是它的设计。我尝试setUndecorated(true);setBackground(new Color(0, 0, 0, 0));,但看起来很奇怪。以下是它的外观图片:http://prntscr.com/2pqohq这是我的代码:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridBagLayout;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

/**
 * 
 * Our Launcher for the client
 * 
 * @author Daniel <Skype: daniel.gusdal>
 *
 * Current Date: 5. feb. 2014 Current Time: 14:29:54
 * Project: 742 client. File Name: Launcher.java
 *
 */
public class Launcher2 {

    public Launcher2() {
        JFrame frame = new JFrame();
        frame.getContentPane().add(new ImagePanel());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //frame.pack(); //had to remove, got an error from it... 
        frame.setUndecorated(true); //transparent
        frame.setBackground(new Color(0, 0, 0, 0)); //transparent
        frame.setVisible(true);
        frame.setSize(1080, 550);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable(){
            public void run() {
                new Launcher2();
            }
        });
    }

    @SuppressWarnings("serial")
    public class ImagePanel extends JPanel {

        BufferedImage img;

        public ImagePanel() {
            setLayout(new GridBagLayout());
            try {
                img = ImageIO.read(new File("C:/Users/Daniel/Pictures/Launcher3.png/"));
            } catch (MalformedURLException ex) {
                Logger.getLogger(Launcher2.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(Launcher2.class.getName()).log(Level.SEVERE, null, ex);
            }

        }

        /**
         * Draws the image and sets the image dimension
         */
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            //g.drawImage(img, 100, 100, 1080, 550, this);
            g.drawImage(img, 0, 0, getWidth(), getHeight(), this);
        }

        /**
         * Sets the JPanel dimension
         */
        public Dimension getPreferredSize() {
            return new Dimension(1080, 550); 
        }
    }
}

1 个答案:

答案 0 :(得分:7)

您需要将ImagePanel设置为setOpaque(false)

 public ImagePanel() {
        setOpaque(false);

此外,您获得了例外,因为您需要setUndecorate(true) <{1}}

pack();

这是我改变的唯一两件事,它起作用。我也摆脱了 JFrame frame = new JFrame(); frame.add(new ImagePanel()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setUndecorated(true); frame.pack(); frame.setBackground(new Color(0, 0, 0, 0)); frame.setVisible(true);

setSize()之后使用frame.setLocationRelativeTo(null);将框架居中。


这是一个例子(仅供未来的读者阅读,因为我认为这可能是一个受欢迎的问题)

enter image description here

pack()