Why is JFrame still not transparent?

I am using Java 6.

Hope the following code can display a transparent window. But it still shows a normal window with a white background. What for? I think it makes sense that if I hide all the panels this should give me a transparent window.

package MaskingEffect;

import java.awt.EventQueue;

import javax.swing.JFrame;

public class GlassMaskTest {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                GlassMaskFrame frame=new GlassMaskFrame();

                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.getContentPane().setVisible(false);
                frame.getLayeredPane().setVisible(false);
                frame.getRootPane().setVisible(false);
                frame.getGlassPane().setVisible(false);
                frame.setVisible(true);
                //AWTUtilities.setWindowOpacity(frame, 0.1f);

            }
        });

    }

}

      

And so GlassMaskFrame

:

package MaskingEffect;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics2D;

import javax.swing.JComponent;
import javax.swing.JFrame;

import com.sun.awt.AWTUtilities;
import com.sun.xml.internal.ws.api.server.Container;

public class GlassMaskFrame extends JFrame {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    public GlassMaskFrame() {

        this.setSize(new Dimension(500, 600));
    }

}

      

I have also tried setBackground(new Color(0,0,0,0))

for each of the 4 panels. But still don't get a transparent window.

I don't want to use the method AWTUtilities.setWindowOpacity()

.

This is what I get:

enter image description here

+3


source to share


3 answers


Set the background color of your frame to

<frame-name>.setBackground(new Color(0, 0, 0, 0));

      

And set the opacity of the content area or whatever you are using ...



<content-pane-name>.setOpaque(false);

      

this might do the trick ...

0


source


public TransparentJFrame()
{
setTitle("Transparent JFrame Demo");
setSize(400,400);
setLayout(new GridBagLayout());
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
//For Java 1.7 or above
setOpacity(0.4f);
//For lower java versions
//com.sun.awt.AWTUtilities.setWindowOpacity(this,0.4f);
}

      



0


source


Easier with Java 7+ with Java 6 you need to use private API com.sun.awt.AWTUtilities

For this, I wrote a utility class that makes it easy to use. It uses reflection to determine if it is possible for the actual call, in case you are using a Java version below 6u10

public static boolean supportsPerAlphaPixel() {

    boolean support = false;

    try {

        Class<?> awtUtilsClass = Class.forName("com.sun.awt.AWTUtilities");
        support = true;

    } catch (Exception exp) {
    }

    return support;

}

public static void setOpaque(Window window, boolean opaque) {

    try {

        Class<?> awtUtilsClass = Class.forName("com.sun.awt.AWTUtilities");
        if (awtUtilsClass != null) {

            Method method = awtUtilsClass.getMethod("setWindowOpaque", Window.class, boolean.class);
            method.invoke(null, window, opaque);

        }

    } catch (Exception exp) {
    }

}

public static void setOpacity(Window window, float opacity) {

    try {

        Class<?> awtUtilsClass = Class.forName("com.sun.awt.AWTUtilities");
        if (awtUtilsClass != null) {

            Method method = awtUtilsClass.getMethod("setWindowOpacity", Window.class, float.class);
            method.invoke(null, window, opacity);

        }

    } catch (Exception exp) {

        exp.printStackTrace();

    }

}

public static float getOpacity(Window window) {

    float opacity = 1f;
    try {

        Class<?> awtUtilsClass = Class.forName("com.sun.awt.AWTUtilities");
        if (awtUtilsClass != null) {

            Method method = awtUtilsClass.getMethod("getWindowOpacity", Window.class);
            Object value = method.invoke(null, window);
            if (value != null && value instanceof Float) {

                opacity = ((Float) value).floatValue();

            }

        }

    } catch (Exception exp) {

        exp.printStackTrace();

    }

    return opacity;

}

      

Opaque

Opaque

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagLayout;
import java.awt.Window;
import java.lang.reflect.Method;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.LineBorder;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setUndecorated(true);
                setOpaque(frame, false);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setContentPane(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setOpaque(false);
            setBorder(new LineBorder(Color.RED));
            setLayout(new GridBagLayout());
            add(new JLabel("Look no hands"));
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

    }

    public static boolean supportsPerAlphaPixel() {

        boolean support = false;

        try {

            Class<?> awtUtilsClass = Class.forName("com.sun.awt.AWTUtilities");
            support = true;

        } catch (Exception exp) {
        }

        return support;

    }

    public static void setOpaque(Window window, boolean opaque) {

        try {

            Class<?> awtUtilsClass = Class.forName("com.sun.awt.AWTUtilities");
            if (awtUtilsClass != null) {

                Method method = awtUtilsClass.getMethod("setWindowOpaque", Window.class, boolean.class);
                method.invoke(null, window, opaque);

            }

        } catch (Exception exp) {
        }

    }

    public static void setOpacity(Window window, float opacity) {

        try {

            Class<?> awtUtilsClass = Class.forName("com.sun.awt.AWTUtilities");
            if (awtUtilsClass != null) {

                Method method = awtUtilsClass.getMethod("setWindowOpacity", Window.class, float.class);
                method.invoke(null, window, opacity);

            }

        } catch (Exception exp) {

            exp.printStackTrace();

        }

    }

    public static float getOpacity(Window window) {

        float opacity = 1f;
        try {

            Class<?> awtUtilsClass = Class.forName("com.sun.awt.AWTUtilities");
            if (awtUtilsClass != null) {

                Method method = awtUtilsClass.getMethod("getWindowOpacity", Window.class);
                Object value = method.invoke(null, window);
                if (value != null && value instanceof Float) {

                    opacity = ((Float) value).floatValue();

                }

            }

        } catch (Exception exp) {

            exp.printStackTrace();

        }

        return opacity;

    }

}

      

Transparent

Transparent

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.Window;
import java.lang.reflect.Method;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setUndecorated(true);
                setOpacity(frame, 0.5f);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setContentPane(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new GridBagLayout());
            add(new JLabel("Look no hands"));
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

    }

    public static boolean supportsPerAlphaPixel() {

        boolean support = false;

        try {

            Class<?> awtUtilsClass = Class.forName("com.sun.awt.AWTUtilities");
            support = true;

        } catch (Exception exp) {
        }

        return support;

    }

    public static void setOpaque(Window window, boolean opaque) {

        try {

            Class<?> awtUtilsClass = Class.forName("com.sun.awt.AWTUtilities");
            if (awtUtilsClass != null) {

                Method method = awtUtilsClass.getMethod("setWindowOpaque", Window.class, boolean.class);
                method.invoke(null, window, opaque);

            }

        } catch (Exception exp) {
        }

    }

    public static void setOpacity(Window window, float opacity) {

        try {

            Class<?> awtUtilsClass = Class.forName("com.sun.awt.AWTUtilities");
            if (awtUtilsClass != null) {

                Method method = awtUtilsClass.getMethod("setWindowOpacity", Window.class, float.class);
                method.invoke(null, window, opacity);

            }

        } catch (Exception exp) {

            exp.printStackTrace();

        }

    }

    public static float getOpacity(Window window) {

        float opacity = 1f;
        try {

            Class<?> awtUtilsClass = Class.forName("com.sun.awt.AWTUtilities");
            if (awtUtilsClass != null) {

                Method method = awtUtilsClass.getMethod("getWindowOpacity", Window.class);
                Object value = method.invoke(null, window);
                if (value != null && value instanceof Float) {

                    opacity = ((Float) value).floatValue();

                }

            }

        } catch (Exception exp) {

            exp.printStackTrace();

        }

        return opacity;

    }

}

      

Now, before you go to the frame, this is "borderless", it is far from everything to make it work, but it may not work on all platforms (like macOS), it requires you to use the look and feel that provides border frames (e.g. metal), which is not particularly nice ...

0


source







All Articles