How to make JFrame background and JPanel transparent with only image showing(如何使 JFrame 背景和 JPanel 透明且仅显示图像)
问题描述
嘿,我正在尝试制作某种启动器,窗口"必须是透明的,因为如果您理解我的意思,我希望我正在使用的图像成为它的设计.我试图做 setUndecorated(true); 和 setBackground(new Color(0, 0, 0, 0)); 但它看起来很奇怪.这是一张关于它的外观的图片:http://prntscr.com/2pqohq 这是我的代码:p>
Hey I am trying to make a some kind of launcher and the "window" must be transparent because I want the image I am using to be the design of it if you understand what I mean. I tried to do setUndecorated(true); and setBackground(new Color(0, 0, 0, 0)); but it just looked weird. Here is a picture on how it looks: http://prntscr.com/2pqohq Here is my code:
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); 
        }
    }
}
推荐答案
需要将ImagePanel设置为setOpaque(false)
 public ImagePanel() {
        setOpaque(false);
你也得到了异常,因为你需要 setUndecorate(true) before 你 pack();
Also you were getting the exception because you need to setUndecorate(true) before you 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()
Those are the only two things I changed and it works. Also I got rid of the setSize()
同样在 pack() 之后使用 frame.setLocationRelativeTo(null); 使框架居中.
Also use frame.setLocationRelativeTo(null); after pack() to center the frame.
这是一个例子(仅供未来的读者参考,因为我认为这可能是一个热门问题)
Here's is an example (just for future readers, since I think this may be a popular question)
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.*;
public class CircleSplashScreen {
    public CircleSplashScreen() {
        JFrame frame = new JFrame();
        frame.getContentPane().add(new ImagePanel());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setUndecorated(true);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setBackground(new Color(0, 0, 0, 0));
        frame.setVisible(true);
    }
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new CircleSplashScreen();
            }
        });
    }
    @SuppressWarnings("serial")
    public class ImagePanel extends JPanel {
        BufferedImage img;
        public ImagePanel() {
            setOpaque(false);
            setLayout(new GridBagLayout());
            try {
                img = ImageIO.read(new URL("http://www.iconsdb.com/icons/preview/royal-blue/stackoverflow-4-xxl.png"));
            } catch (IOException ex) {
                Logger.getLogger(CircleSplashScreen.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.drawImage(img, 0, 0, getWidth(), getHeight(), this);
        }
        @Override
        public Dimension getPreferredSize() {
            return new Dimension(500, 500);
        }
    }
}
                        这篇关于如何使 JFrame 背景和 JPanel 透明且仅显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何使 JFrame 背景和 JPanel 透明且仅显示图像
				
        
 
            
        - 未找到/usr/local/lib 中的库 2022-01-01
 - 如何指定 CORS 的响应标头? 2022-01-01
 - java.lang.IllegalStateException:Bean 名称“类别"的 BindingResult 和普通目标对象都不能用作请求属性 2022-01-01
 - 转换 ldap 日期 2022-01-01
 - 如何使 JFrame 背景和 JPanel 透明且仅显示图像 2022-01-01
 - Eclipse 的最佳 XML 编辑器 2022-01-01
 - 获取数字的最后一位 2022-01-01
 - 将 Java Swing 桌面应用程序国际化的最佳实践是什么? 2022-01-01
 - 在 Java 中,如何将 String 转换为 char 或将 char 转换 2022-01-01
 - GC_FOR_ALLOC 是否更“严重"?在调查内存使用情况时? 2022-01-01
 
						
						
						
						
						
				
				
				
				