如何获得透明的JFrame? [英] How to get transparent JFrame?

查看:66
本文介绍了如何获得透明的JFrame?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试运行以获取透明框架时,它会显示异常.

When I try to run to get transparent frame it shows exception.

我的代码是:

public class NewJFrame extends javax.swing.JFrame {
    public NewJFrame() {
        initComponents();
        com.sun.awt.AWTUtilities.setWindowOpacity(this, 0.05f);
    }

例外是:

Exception in thread "AWT-EventQueue-0" java.awt.IllegalComponentStateException: The frame is decorated
at java.awt.Frame.setOpacity(Frame.java:960)
at java.awt.Window$1.setOpacity(Window.java:4032)
at com.sun.awt.AWTUtilities.setWindowOpacity(AWTUtilities.java:174)
at test.NewJFrame.<init>(NewJFrame.java:28)
at test.NewJFrame$2.run(NewJFrame.java:115)

推荐答案

在调用setWindowOpacity之前先调用setUndecorated(true).

在Java 7+中,核心API提供了此支持(无需使用com.sun.*库).有关更多详细信息,请参见如何创建半透明和异形Windows .例如,您可以改用JFrame#setOpacity ...

In Java 7+ this support is provided in the core API (without the need to use a com.sun.* library). See How to Create Translucent and Shaped Windows for more details. For example, you could use JFrame#setOpacity instead...

this.setOpacity(0.05f);

哦,尽管本教程可能提出建议,但我相信使装饰后的窗口透明的唯一方法是当窗口不使用操作系统提供的装饰(并且使用外观和感觉提供的装饰,而并非所有外观都如此)并感到支持),但我可能错了

Oh, and despite what the tutorial might suggest, I believe the only way to make a decorated window transparent is when the window is NOT using the OS provided decorations (and is using the look and feel provided decorations, which not all look and feels support), but I could be wrong

这篇关于如何获得透明的JFrame?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆