问题描述
这是。
如果您构建并运行 TranslucentWindow
,比如支持jdk7的NetBeans IDE 7.0,您将获得以下异常:
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)
根据此堆栈跟踪,违规行为 tw.setOpacity(0.55f)
。但是,正如错误所示,如果你在框架上调用 setUndecorated(true)
,它就不会抛出异常并创建一个半透明的窗口,尽管没有任何装饰(这很痛苦)。这是正常的吗?这段代码不应该开箱即用吗?我忽略了什么吗?
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?
编辑
为什么他们的半透明窗口会出现装饰,或者这是自定义渲染?
Why does their translucent window appear decorated, or is this custom rendering?
它是什么......
What it is...
应该是什么......
What it should be...
推荐答案
从 java.awt.frame.setOpacity()的JavaDocs :
- 底层系统必须支持TRANSLUCENT半透明
- 窗口必须未修饰(参见setUndecor ated(boolean)和Dialog.setUndecorated(boolean))
- 窗口不能处于全屏模式(参见GraphicsDevice.setFullScreenWindow(Window))
如果请求的不透明度值小于1.0f,并且不满足上述任何条件,则窗口不透明度将不会更改,并且将抛出IllegalComponentStateException。
If the requested opacity value is less than 1.0f, and any of the above conditions are not met, the window opacity will not change, and the IllegalComponentStateException will be thrown.
您所看到的行为已记录在案并且是预期行为。
The behavior that you are seeing is documented and is expected behavior.
这篇关于Java Tutorials半透明窗口示例给使用jdk7的人带来了麻烦吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!