The Java Tutorials Translucent Window 示例是否给那些玩 jdk7 的人带来了麻烦?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6259269/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is The Java Tutorials Translucent Window example giving trouble to those playing with jdk7?
提问by mre
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:
如果您TranslucentWindow
在支持 jdk7 的 NetBeans IDE 7.0 中构建并运行,您将收到以下异常:
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.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// Taken from http://download.oracle.com/javase/tutorial/uiswing/misc/trans_shaped_windows.html#uniform
import java.awt.*;
import javax.swing.*;
import static java.awt.GraphicsDevice.WindowTranslucency.*;
public class TranslucentWindow extends JFrame {
public TranslucentWindow() {
super("TranslucentWindow");
setLayout(new GridBagLayout());
setSize(300,200);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Add a sample button.
add(new JButton("I am a Button"));
}
public static void main(String[] args) {
// Determine if the GraphicsDevice supports translucency.
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gd = ge.getDefaultScreenDevice();
//If translucent windows aren't supported, exit.
if (!gd.isWindowTranslucencySupported(TRANSLUCENT))
{
System.err.println("Translucency is not supported");
System.exit(0);
}
// Create the GUI on the event-dispatching thread
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
TranslucentWindow tw = new TranslucentWindow();
// Set the window to 55% opaque (45% translucent).
tw.setOpacity(0.55f);
// Display the window.
tw.setVisible(true);
}
});
}
0(EventQueue.java:101)
at java.awt.EventQueue.run(EventQueue.java:666)
at java.awt.EventQueue.run(EventQueue.java:664)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain.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?
根据此堆栈跟踪,违规行是tw.setOpacity(0.55f)
。但是,正如错误所指示的那样,如果您setUndecorated(true)
在框架上调用,则它不会抛出异常并会创建一个半透明的窗口,尽管没有任何装饰(这很痛苦)。这是正常的吗?这段代码不应该“开箱即用”运行吗?我是否忽略了什么?
EDIT
编辑
Why does their translucent window appear decorated, or is this custom rendering?
为什么他们的半透明窗口看起来是装饰的,或者这是自定义渲染?
What it is...
这是什么...
What it should be...
应该是什么...
回答by Thomas Owens
Right from the JavaDocs for java.awt.frame.setOpacity() in JDK7:
直接从JavaDocs for java.awt.frame.setOpacity() in JDK7:
The following conditions must be met in order to set the opacity value less than 1.0f:
- The TRANSLUCENT translucency must be supported by the underlying system
- The window must be undecorated (see setUndecorated(boolean) and Dialog.setUndecorated(boolean))
- The window must not be in full-screen mode (see GraphicsDevice.setFullScreenWindow(Window))
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.
必须满足以下条件才能将不透明度值设置为小于 1.0f:
- TRANSLUCENT 半透明必须得到底层系统的支持
- 窗口必须是未装饰的(参见 setUndecorated(boolean) 和 Dialog.setUndecorated(boolean))
- 窗口不得处于全屏模式(请参阅 GraphicsDevice.setFullScreenWindow(Window))
如果请求的不透明度值小于1.0f,并且不满足上述任何一个条件,则窗口不透明度不会改变,并且会抛出IllegalComponentStateException。
The behavior that you are seeing is documented and is expected behavior.
您所看到的行为已记录在案并且是预期行为。
回答by Rick Hodgin
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.
这是一个经过验证的错误。我已经向 Oracle 发送了有关他们使用默认安装的 JDK 1.7.0 或 JRE7 失败的示例代码的信息。使用下面编译成 TranslucentWindow.java 的源代码,它失败并产生上面最初指出的异常。
From web page Oracle's Translucency / Shaped Windows Page
Exception in thread "AWT-EventQueue-0" java.awt.IllegalComponentStateException: The frame is decorated
at java.awt.Frame.setOpacity(Frame.java:960)
at TranslucentWindow.run(TranslucentWindow.java:38)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:705)
at java.awt.EventQueue.accessJFrame.setDefaultLookAndFeelDecorated(true);
0(EventQueue.java:101)
at java.awt.EventQueue.run(EventQueue.java:666)
at java.awt.EventQueue.run(EventQueue.java:664)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain.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)
}
}
//If translucent windows aren't supported, exit.
if (!gd.isWindowTranslucencySupported(TRANSLUCENT)) {
System.err.println(
"Translucency is not supported");
System.exit(0);
}
JFrame.setDefaultLookAndFeelDecorated(true);
// Create the GUI on the event-dispatching thread
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
TranslucentWindow tw = new TranslucentWindow();
// Set the window to 55% opaque (45% translucent).
tw.setOpacity(0.55f);
// Display the window.
tw.setVisible(true);
}
});
回答by Sujith Themath
回答by kimbaudi
Hi the problem with this code is that it is missing the following line of code in the main() method:
嗨,此代码的问题在于它在 main() 方法中缺少以下代码行:
##代码##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.
此外,统一半透明图像的图像具有误导性,因为它使用了 java 外观。相反,图像应该使用 Windows 系统的外观和感觉(假设您使用的是 Windows)。如果您尝试使用 Java 外观(即JFrame.setDefaultLookAndFeelDecorated(false);
),则会抛出与以前相同的错误。我希望半透明窗口可以与 Java 外观和感觉一起使用,但我认为这是不可能的。
回答by Benjemaa
I think you need to setUndecorated before setBackground this will fix the problem
我认为你需要在 setBackground 之前 setUndecorated 这将解决问题