How to make JFrame background and JPanel transparent with only image showing

后端 未结 1 825
南笙
南笙 2021-01-18 13:12

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 me

相关标签:
1条回答
  • 2021-01-18 13:13

    You need to set the ImagePanel to setOpaque(false)

     public ImagePanel() {
            setOpaque(false);
    

    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);
    

    Those are the only two things I changed and it works. Also I got rid of the setSize()

    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)

    enter image description here

    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);
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题