JFrame with blurred transparent background

前端 未结 2 1027
-上瘾入骨i
-上瘾入骨i 2021-01-21 11:49

I would like to do a blur on the background of the JFrame which is transparent to show what is happening underneath it, but I have no idea how can I blur the background and avoi

2条回答
  •  野性不改
    2021-01-21 12:08

    You can do this very easily and there are multiple ways.Following is the sample code how you can do this.

    import java.awt.Color;
    import java.awt.FlowLayout;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.GraphicsDevice;
    import java.awt.GraphicsDevice.WindowTranslucency;
    import java.awt.GraphicsEnvironment;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.SwingUtilities;
    
    
    public class transparentWindow extends JFrame {
    
    public transparentWindow() {
        // TODO Auto-generated constructor stub
        //JFrame jfrm=new JFrame("Transparent Window");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(300,200);
        getContentPane().setLayout(new FlowLayout());
        //setBackground(new Color(0,0,0,0));
    
        add(new JButton("Enter"));
        setOpacity(0.7f);
        setVisible(true);
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice gd=ge.getDefaultScreenDevice();
        if(!gd.isWindowTranslucencySupported(WindowTranslucency.TRANSLUCENT))
        {
            System.out.println("Transparency not supported");
            System.exit(0);
        }
        JFrame.setDefaultLookAndFeelDecorated(true);
        SwingUtilities.invokeLater(new Runnable(){public void run(){new transparentWindow();}});
    }
    
    }
    

    With this you can even watch the live video through it .You can adjust the transparency level as well.The output is as follows :-

    enter image description here

提交回复
热议问题