【问题标题】:Why the JFrame is still not transparent?为什么 JFrame 仍然不透明?
【发布时间】:2015-06-26 22:22:34
【问题描述】:

我正在使用 Java 6。

我希望下面的代码可以显示一个透明的窗口。但它仍然显示一个带有白色背景的普通窗口。为什么?我认为如果我隐藏所有窗格,它应该给我一个透明窗口是合乎逻辑的。

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

            }
        });

    }

}

这里是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));
    }

}

我还为 4 个窗格中的每一个尝试了 setBackground(new Color(0,0,0,0))。但仍然没有得到透明窗口。

我不想使用AWTUtilities.setWindowOpacity() 方法。

这是我得到的:

【问题讨论】:

  • 因为你 setVisible(false);对于所有可以不透明或透明的东西,在 Java7 中并且从不版本 1st JFrame(它不可见)必须是未装饰的,然后可以与其余部分一起玩 ...
  • 按照 Oracle 教程获取工作代码示例
  • @mKorbel 抱歉,我没有提到我正在使用 Java 6。
  • 那么一切皆有可能,不明白为什么不透明或透明不起作用:-),
  • “我使用的是 Java 6。” 为什么使用过时的 Java 版本?

标签: java swing jframe transparency opacity


【解决方案1】:

将框架的背景颜色设置为

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

并设置内容窗格或您正在使用的任何内容的不透明度...

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

这可能会成功....

【讨论】:

  • 看起来很有希望。只是我没有找到setOpaque() 方法。您使用的是哪个 Java 版本?
  • setOpaque() 用于内容窗格,而不是框架
  • 也许这只是JDK8。我正在使用JDK6。
【解决方案2】:
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);
}

【讨论】:

  • 谢谢。但我听说不推荐使用 AWTUtilities.setWindowOpacity()。虽然我不知道为什么。
【解决方案3】:

使用 Java 7+ 更容易,使用 Java 6,您需要使用私有 API com.sun.awt.AWTUtilities

为此,我编写了一个实用程序类,使其更易于使用。它使用反射来确定是否可以实际调用,以防您使用低于 6u10 的 Java 版本

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;

}

不透明

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;

    }

}

透明

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;

    }

}

现在,在您完全了解框架“无边界”之前,这是为了让它工作,但是,它可能不适用于所有平台(如 MacOS),它需要您使用外观和感觉框架边框(例如金属),这不是特别令人愉快...

【讨论】:

  • 谢谢。是否可以在透明框架上绘制不透明矩形(未填充)?我想要达到的是某种“瞄准”效果。
  • 或者让我这样说:它就像在一块玻璃上绘制的一个矩形。
  • 是的,但你需要伪造它。基本上,您创建一个透明窗口并使用 JPanel,它也是透明的。覆盖面板的 paintComponent 方法并使用 AlphaComposite 绘制您想要的内容
猜你喜欢
  • 2021-06-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多