com.sun.awt.AWTUtilities.setWindowOpacity(Window arg0, float arg1 ); 是JDK6之后增加的一个新 的方法,作用是可以使得窗体以及窗体内部的组件透明。但是在使用的时候可能会遇到如下俩个麻烦:
1.在eclipse中(jdk1.6.*)版本中出现编译不通过而报错,报错是因为这个包里面的方法不属于jdk正式版本,也就是不能保证下个版本还存在,所以编译器会拒绝,你可以在eclipse中如下设置: 选择Window->Preferences->Java->Compiler->Errors/Warnings;然后选择Deprecated and restricted API,将 Deprecated API从ERROR改为Warning即可!
2.在JDK1.7使用这个方法的时候,编译通过,但是运行出错,那么请看看官方的说明.
The following conditions must be met in order to set the opacity value less than 1.0f:
(必须符合下列条件才能设置不透明度小于1.0f)
1.The TRANSLUCENT translucency must be supported by the underlying system
(底层系统必须支持半透明属性)
2.The window must be undecorated (see setUndecorated(boolean) and Dialog.setUndecorated(boolean))
(窗体必须是未加修饰的(见 setUndecorated(boolean) 和 Dialog.setUndecorated(boolean)))
3.The window must not be in full-screen mode (see GraphicsDevice.setFullScreenWindow(Window))
(窗体 不是在全屏模式下(见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.0 f,以及上述任何条件不满足,窗口透明度将不会改变,而且还将会抛出IllegalComponentStateException)
(必须符合下列条件才能设置不透明度小于1.0f)
1.The TRANSLUCENT translucency must be supported by the underlying system
(底层系统必须支持半透明属性)
2.The window must be undecorated (see setUndecorated(boolean) and Dialog.setUndecorated(boolean))
(窗体必须是未加修饰的(见 setUndecorated(boolean) 和 Dialog.setUndecorated(boolean)))
3.The window must not be in full-screen mode (see GraphicsDevice.setFullScreenWindow(Window))
(窗体 不是在全屏模式下(见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.0 f,以及上述任何条件不满足,窗口透明度将不会改变,而且还将会抛出IllegalComponentStateException)
所以一般需要去掉窗体的修饰(采用 setUndecorated(boolean)方法 )。
参考: