Java教程半透明窗口示例是否给玩jdk7的人带来麻烦?
这是示例。
如果您在支持 jdk7 的 NetBeans IDE 7.0 中构建并运行 TranslucentWindow
,您将收到以下异常:
Exception in thread "AWT-EventQueue-0" java.awt.IllegalComponentStateException: The frame is decorated
at java.awt.Frame.setOpacity(Frame.java:960)
at main.TranslucentWindow.<init>(TranslucentWindow.java:23)
at main.TranslucentWindow$1.run(TranslucentWindow.java:47)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:705)
at java.awt.EventQueue.access$000(EventQueue.java:101)
at java.awt.EventQueue$3.run(EventQueue.java:666)
at java.awt.EventQueue$3.run(EventQueue.java:664)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:675)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
BUILD SUCCESSFUL (total time: 1 second)
根据此堆栈跟踪,有问题的行是 tw.setOpacity( 0.55f)
。但是,正如错误所示,如果您在框架上调用 setUndecorated(true)
,那么它不会抛出异常,并且会创建一个半透明的窗口,尽管没有任何装饰(这是一个痛苦) 。这是正常的吗?这段代码不应该“开箱即用”正确运行吗?我是否忽略了什么?
编辑
为什么他们的半透明窗口看起来是装饰过的,或者这是自定义渲染?
这是什么...
它应该是什么...
Here's the example.
If you build and run TranslucentWindow
in let's say, NetBeans IDE 7.0, which supports jdk7, you'll get the following exception:
Exception in thread "AWT-EventQueue-0" java.awt.IllegalComponentStateException: The frame is decorated
at java.awt.Frame.setOpacity(Frame.java:960)
at main.TranslucentWindow.<init>(TranslucentWindow.java:23)
at main.TranslucentWindow$1.run(TranslucentWindow.java:47)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:705)
at java.awt.EventQueue.access$000(EventQueue.java:101)
at java.awt.EventQueue$3.run(EventQueue.java:666)
at java.awt.EventQueue$3.run(EventQueue.java:664)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:675)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
BUILD SUCCESSFUL (total time: 1 second)
According to this stack trace, the offending line is tw.setOpacity(0.55f)
. But, as the error indicates, if you invoke setUndecorated(true)
on the frame, then it won't throw the exception and will create a translucent window, albeit without any decoration (which is a pain). Is this normal? Shouldn't this code run right "out-of-the-box"? Am I overlooking something?
EDIT
Why does their translucent window appear decorated, or is this custom rendering?
What it is...
What it should be...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
直接来自 java.awt.frame.setOpacity 的 JavaDocs () 在 JDK7 中:
您所看到的行为已记录在案,并且是预期行为。
Right from the JavaDocs for java.awt.frame.setOpacity() in JDK7:
The behavior that you are seeing is documented and is expected behavior.
这是一个已验证的错误。我已向 Oracle 发送了有关其示例代码在使用默认安装的 JDK 1.7.0 或 JRE7 时失败的信息。使用下面编译成 TranslucentWindow.java 的源代码,它会失败并产生上面最初指出的异常。
来自网页 Oracle 的半透明/异形 Windows 页面
}
This is a verified bug. I have sent Oracle information on their sample code failing using the default install of JDK 1.7.0 or JRE7. Using the source code below compiled into TranslucentWindow.java, it fails and produces the exception initially indicated above.
From web page Oracle's Translucency / Shaped Windows Page
}
在 JDK 7 中使用
com.sun.awt.AWTUtilities.setWindowOpacity(w, 0.5f)
。请参阅 此处。
Use
com.sun.awt.AWTUtilities.setWindowOpacity(w, 0.5f)
in JDK 7.See here.
我认为你需要在 setBackground 之前 setUndecorated 这将解决问题
I think you need to setUndecorated before setBackground this will fix the problem
您好,此代码的问题是它在 main() 方法中缺少以下代码行:
它应该紧接在检查是否不支持半透明窗口并退出的代码之后:
另外,统一半透明的图像图像具有误导性,因为它使用了 java 外观。相反,图像应该使用 Windows 系统的外观(假设您使用的是 Windows)。如果您尝试使用 Java 外观(即
JFrame.setDefaultLookAndFeelDecolated(false);
),那么它将抛出与以前相同的错误。我希望半透明窗口能够与 Java 的外观和感觉配合使用,但我认为这是不可能的。Hi the problem with this code is that it is missing the following line of code in the main() method:
It should go right after the code that checks if translucent windows aren't supported and exits:
Also, the image of the uniform translucent image is misleading because it uses the java look and feel. Instead the image should use the Windows system look and feel (assuming you're on Windows). If you try to use the Java look and feel (i.e.,
JFrame.setDefaultLookAndFeelDecorated(false);
), then it will throw the same error as before. I was hoping that the translucent window would work with the Java look and feel, but I don't think this is possible.