这是example。
如果您使用支持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)
,那么它不会抛出异常并创建一个半透明窗口,虽然没有任何装饰(这很痛苦)。这是正常的吗?这段代码不应该“开箱即用”吗?我忽略了什么吗?
修改
为什么他们的半透明窗口看起来是装饰的,还是这种自定义渲染?
它是什么......
应该是什么......
答案 0 :(得分:16)
从JavaDocs for java.awt.frame.setOpacity() in JDK7开始:
必须满足以下条件才能将不透明度值设置为小于1.0f:
- 底层系统必须支持TRANSLUCENT半透明
- 窗口必须未修饰(参见setUndecorated(boolean)和Dialog.setUndecorated(boolean))
- 窗口不得处于全屏模式(请参阅GraphicsDevice.setFullScreenWindow(Window))
如果请求的不透明度值小于1.0f,并且不满足上述任何条件,则窗口不透明度将不会更改,并且将抛出IllegalComponentStateException。
您所看到的行为已被记录,并且是预期的行为。
答案 1 :(得分:4)
这是经过验证的错误。我使用默认安装的JDK 1.7.0或JRE7发送了有关其示例代码失败的Oracle信息。使用下面编译成TranslucentWindow.java的源代码,它会失败并产生上面最初指出的异常。
来自网页Oracle's Translucency / Shaped Windows Page
// 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);
}
});
}
}
Exception in thread "AWT-EventQueue-0" java.awt.IllegalComponentStateException: The frame is decorated
at java.awt.Frame.setOpacity(Frame.java:960)
at TranslucentWindow$1.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.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)
答案 2 :(得分:2)
在JDK 7中使用com.sun.awt.AWTUtilities.setWindowOpacity(w, 0.5f)
。
请参阅here。
答案 3 :(得分:0)
您好这个代码的问题是它缺少main()方法中的以下代码行:
JFrame.setDefaultLookAndFeelDecorated(true);
它应该在检查是否不支持半透明窗口的代码之后立即退出:
//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);
}
});
此外,均匀半透明图像的图像具有误导性,因为它使用了Java外观。相反,图像应该使用Windows系统的外观(假设你在Windows上)。如果您尝试使用Java外观(即JFrame.setDefaultLookAndFeelDecorated(false);
),那么它将抛出与以前相同的错误。我希望半透明窗口可以与Java外观一起使用,但我不认为这是可能的。
答案 4 :(得分:0)
我认为你需要在setBackground之前setUndecorated这将解决问题