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)方法 )。
参考:
相关推荐
在Java编程中,窗体透明化是一个常见的需求,特别是在创建具有现代感的...不过要注意,由于`com.sun.awt.AWTUtilities`并不是标准的Java API,所以在某些平台上可能无法正常工作,因此在生产环境中使用时要特别谨慎。
- 当`JSlider`值改变时,通过`com.sun.awt.AWTUtilities.setWindowOpacity`方法设置窗体的透明度。这个方法接受两个参数:当前窗体的引用和一个介于0.0到1.0之间的浮点数,表示不透明度。值越小,窗体越透明。 4. ...
在Java中,通过使用`com.sun.awt.AWTUtilities`类中的方法,可以实现窗口透明效果。本文将详细讲解如何使用Java编写一个简单程序来实现透明界面,并允许用户通过滑块调整透明度。 #### 二、核心概念与技术点 #####...
尽管这些新功能并未正式纳入公共API,而是通过非官方支持的`com.sun.awt.AWTUtilities`类提供,但其实现的便利性和性能表现令人瞩目。 - **透明窗口**:`AWTUtilities.setWindowOpacity(Window, float)`方法允许...
但是,从Java 6 Update 10开始,Sun Microsystems(后来被Oracle收购)引入了一个名为`com.sun.awt.AWTUtilities`的非公开类,它提供了一些额外的功能,其中包括设置窗口的不透明度。因此,要实现JFrame的透明效果,...