---
title: "jdk7透明异形窗体 "
author: "alanland"
date: "2014-04-10T20:42:16Z"
tags: ["Java", "Swing"]
categories: ["编程语言"]
slug: "jdk7"
draft: false
lastmod: "2014-04-10T20:42:16Z"
id: 2043204
url: "https://alanland.iteye.com/blog/2043204"
---

在jdk6中设置透明异形窗体的方法与jdk7方法的比较

Method in Java SE 6 Update 10 JDK 7 Equivalent `AWTUtilities.isTranslucencySupported(Translucency)` `GraphicsDevice.isWindowTranslucencySupported(WindowTranslucency)` `AWTUtilities.isTranslucencyCapable(GraphicsConfiguration)` `GraphicsConfiguration.isTranslucencyCapable()` `AWTUtilities.setWindowOpacity(Window, float)` `Window.setOpacity(float)` `AWTUtilities.setWindowShape(Window, Shape)` `Window.setShape(Shape)` `AWTUtilities.setWindowOpaque(boolean)` `Window.setBackground(Color)` Passing `new Color(0,0,0,alpha)` to this method, where`alpha` is less than 255, installs per-pixel translucency.

jdk7判断操作系统是否支持透明窗体的设置

**\[html]** [view plain](http://blog.csdn.net/zhhaogen/article/details/6898763 "view plain")[copy](http://blog.csdn.net/zhhaogen/article/details/6898763 "copy")

01. // Determine what the default GraphicsDevice can support.
02. GraphicsEnvironment ge = GraphicsEnvironment
03. .getLocalGraphicsEnvironment();
04. GraphicsDevice gd = ge.getDefaultScreenDevice();
05. boolean isUniformTranslucencySupported = gd
06. .isWindowTranslucencySupported(TRANSLUCENT);//是否支持全部统一透明度的窗体
07. boolean isPerPixelTranslucencySupported = gd
08. .isWindowTranslucencySupported(PERPIXEL\_TRANSLUCENT);//是否每部分不同透明度的窗体
09. boolean isShapedWindowSupported = gd
10. .isWindowTranslucencySupported(PERPIXEL\_TRANSPARENT);//是否支持异性窗体
11. System.out.println("isUniformTranslucencySupported:"+isUniformTranslucencySupported);
12. System.out.println("isPerPixelTranslucencySupported:"+isPerPixelTranslucencySupported);
13. System.out.println("isShapedWindowSupported:"+isShapedWindowSupported);

 

1.统一透明度窗体的设置

**\[html]** [view plain](http://blog.csdn.net/zhhaogen/article/details/6898763 "view plain")[copy](http://blog.csdn.net/zhhaogen/article/details/6898763 "copy")

01. GraphicsEnvironment ge = GraphicsEnvironment
02. .getLocalGraphicsEnvironment();
03. GraphicsDevice gd = ge.getDefaultScreenDevice();
04. boolean isUniformTranslucencySupported = gd
05. .isWindowTranslucencySupported(TRANSLUCENT);//是否支持全部统一透明度的窗体
06. if(isUniformTranslucencySupported)
07. {
08. JFrame.setDefaultLookAndFeelDecorated(true);
09. JFrame jf=new JFrame("统一透明度");
10. jf.setLayout(new GridBagLayout());
11. Button bu=new Button("这是个按钮");
12. jf.add(bu);
13. jf.setDefaultCloseOperation(JFrame.EXIT\_ON\_CLOSE);
14. jf.setBounds(100, 60, 200, 300);
15. jf.setOpacity(0.8f);
16. jf.setVisible(true);
17. }

 

刚开始一直弄官方例子http://download.oracle.com/javase/tutorial/uiswing/misc/trans\_shaped\_windows.html，发现一直报错

**\[html]** [view plain](http://blog.csdn.net/zhhaogen/article/details/6898763 "view plain")[copy](http://blog.csdn.net/zhhaogen/article/details/6898763 "copy")

1. Exception in thread "AWT-EventQueue-0" java.awt.IllegalComponentStateException: The frame is decorated
2. at java.awt.Frame.setOpacity(Unknown Source)

原来setOpacity方法说明

**\[html]** [view plain](http://blog.csdn.net/zhhaogen/article/details/6898763 "view plain")[copy](http://blog.csdn.net/zhhaogen/article/details/6898763 "copy")

1. The TRANSLUCENT translucency must be supported by the underlying system
2. The window must be undecorated (see setUndecorated(boolean) and Dialog.setUndecorated(boolean))
3. The window must not be in full-screen mode (see GraphicsDevice.setFullScreenWindow(Window))

1.操作系统必须支持设置透明方法  
2.窗体必须未修饰（无标题栏）  
3.窗体必须非全屏模式

加上一句

**\[html]** [view plain](http://blog.csdn.net/zhhaogen/article/details/6898763 "view plain")[copy](http://blog.csdn.net/zhhaogen/article/details/6898763 "copy")

1. JFrame.setDefaultLookAndFeelDecorated(true);