在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, wherealpha is less than 255, installs per-pixel translucency. |
jdk7判断操作系统是否支持透明窗体的设置
- // Determine what the default GraphicsDevice can support.
- GraphicsEnvironment ge = GraphicsEnvironment
- .getLocalGraphicsEnvironment();
- GraphicsDevice gd = ge.getDefaultScreenDevice();
- boolean isUniformTranslucencySupported = gd
- .isWindowTranslucencySupported(TRANSLUCENT);//是否支持全部统一透明度的窗体
- boolean isPerPixelTranslucencySupported = gd
- .isWindowTranslucencySupported(PERPIXEL_TRANSLUCENT);//是否每部分不同透明度的窗体
- boolean isShapedWindowSupported = gd
- .isWindowTranslucencySupported(PERPIXEL_TRANSPARENT);//是否支持异性窗体
- System.out.println("isUniformTranslucencySupported:"+isUniformTranslucencySupported);
- System.out.println("isPerPixelTranslucencySupported:"+isPerPixelTranslucencySupported);
- System.out.println("isShapedWindowSupported:"+isShapedWindowSupported);
1.统一透明度窗体的设置
- GraphicsEnvironment ge = GraphicsEnvironment
- .getLocalGraphicsEnvironment();
- GraphicsDevice gd = ge.getDefaultScreenDevice();
- boolean isUniformTranslucencySupported = gd
- .isWindowTranslucencySupported(TRANSLUCENT);//是否支持全部统一透明度的窗体
- if(isUniformTranslucencySupported)
- {
- JFrame.setDefaultLookAndFeelDecorated(true);
- JFrame jf=new JFrame("统一透明度");
- jf.setLayout(new GridBagLayout());
- Button bu=new Button("这是个按钮");
- jf.add(bu);
- jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- jf.setBounds(100, 60, 200, 300);
- jf.setOpacity(0.8f);
- jf.setVisible(true);
- }
刚开始一直弄官方例子http://download.oracle.com/javase/tutorial/uiswing/misc/trans_shaped_windows.html,发现一直报错
- Exception in thread "AWT-EventQueue-0" java.awt.IllegalComponentStateException: The frame is decorated
- at java.awt.Frame.setOpacity(Unknown Source)
原来setOpacity方法说明
- 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))
1.操作系统必须支持设置透明方法
2.窗体必须未修饰(无标题栏)
3.窗体必须非全屏模式
加上一句
- JFrame.setDefaultLookAndFeelDecorated(true);