2015-04-20 118 views
3

我正在使用Java 6.为什么JFrame仍然不透明?

我希望以下代码可以显示透明窗口。但它仍然显示一个白色背景的正常窗口。为什么?我认为如果我隐藏所有窗格,它应该给我一个透明的窗口是合乎逻辑的。

package MaskingEffect; 

import java.awt.EventQueue; 

import javax.swing.JFrame; 

public class GlassMaskTest { 

    public static void main(String[] args) { 
     // TODO Auto-generated method stub 

     EventQueue.invokeLater(new Runnable() { 

      @Override 
      public void run() { 
       // TODO Auto-generated method stub 
       GlassMaskFrame frame=new GlassMaskFrame(); 

       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.getContentPane().setVisible(false); 
       frame.getLayeredPane().setVisible(false); 
       frame.getRootPane().setVisible(false); 
       frame.getGlassPane().setVisible(false); 
       frame.setVisible(true); 
       //AWTUtilities.setWindowOpacity(frame, 0.1f); 

      } 
     }); 

    } 

} 

这里是GlassMaskFrame

package MaskingEffect; 

import java.awt.Color; 
import java.awt.Component; 
import java.awt.Dimension; 
import java.awt.Graphics2D; 

import javax.swing.JComponent; 
import javax.swing.JFrame; 

import com.sun.awt.AWTUtilities; 
import com.sun.xml.internal.ws.api.server.Container; 

public class GlassMaskFrame extends JFrame { 
    /** 
    * 
    */ 
    private static final long serialVersionUID = 1L; 

    public GlassMaskFrame() { 

     this.setSize(new Dimension(500, 600)); 
    } 

} 

我也试图setBackground(new Color(0,0,0,0))对4个窗格。但仍然没有获得透明的窗口。我不想使用AWTUtilities.setWindowOpacity()方法。

这就是我得到:

enter image description here

+0

因为你setVisible(false);对于任何可以是不透明或透明的东西,在Java7和从不版本中,第一个JFrame(它不可见)必须是未修饰的,然后才可以播放其余的... – mKorbel

+0

遵循Oracle教程的工作代码示例 – mKorbel

+0

@mKorbel对不起,我错过了提到我正在使用Java 6. – smwikipedia

回答

0

设置你的框架的背景颜色

<frame-name>.setBackground(new Color(0, 0, 0, 0)); 

,并设置内容窗格或任何你正在使用的不透明度...

<content-pane-name>.setOpaque(false); 

这可能会诀窍....

+0

看起来很有前途。只是我没有找到'setOpaque()'方法。你使用哪个Java版本? – smwikipedia

+0

我正在使用JDK8 – CoderNeji

+0

'setOpaque()'用于内容窗格,而不是框架 – Kuba

0
public TransparentJFrame() 
{ 
setTitle("Transparent JFrame Demo"); 
setSize(400,400); 
setLayout(new GridBagLayout()); 
setDefaultCloseOperation(EXIT_ON_CLOSE); 
setVisible(true); 
//For Java 1.7 or above 
setOpacity(0.4f); 
//For lower java versions 
//com.sun.awt.AWTUtilities.setWindowOpacity(this,0.4f); 
} 
+0

谢谢。但我听说不建议使用AWTUtilities.setWindowOpacity()。虽然我不知道为什么。 – smwikipedia

0

它与Java 7+更轻松,Java 6中,您需要使用私有API com.sun.awt.AWTUtilities

对于这一点,我写了一个实用工具类,这使得它更容易使用。它使用反射来确定是否有可能实际的通话,如果你正在使用低于6u10中

public static boolean supportsPerAlphaPixel() { 

    boolean support = false; 

    try { 

     Class<?> awtUtilsClass = Class.forName("com.sun.awt.AWTUtilities"); 
     support = true; 

    } catch (Exception exp) { 
    } 

    return support; 

} 

public static void setOpaque(Window window, boolean opaque) { 

    try { 

     Class<?> awtUtilsClass = Class.forName("com.sun.awt.AWTUtilities"); 
     if (awtUtilsClass != null) { 

      Method method = awtUtilsClass.getMethod("setWindowOpaque", Window.class, boolean.class); 
      method.invoke(null, window, opaque); 

     } 

    } catch (Exception exp) { 
    } 

} 

public static void setOpacity(Window window, float opacity) { 

    try { 

     Class<?> awtUtilsClass = Class.forName("com.sun.awt.AWTUtilities"); 
     if (awtUtilsClass != null) { 

      Method method = awtUtilsClass.getMethod("setWindowOpacity", Window.class, float.class); 
      method.invoke(null, window, opacity); 

     } 

    } catch (Exception exp) { 

     exp.printStackTrace(); 

    } 

} 

public static float getOpacity(Window window) { 

    float opacity = 1f; 
    try { 

     Class<?> awtUtilsClass = Class.forName("com.sun.awt.AWTUtilities"); 
     if (awtUtilsClass != null) { 

      Method method = awtUtilsClass.getMethod("getWindowOpacity", Window.class); 
      Object value = method.invoke(null, window); 
      if (value != null && value instanceof Float) { 

       opacity = ((Float) value).floatValue(); 

      } 

     } 

    } catch (Exception exp) { 

     exp.printStackTrace(); 

    } 

    return opacity; 

} 

不透明的Java版本

Opaque

import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.EventQueue; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.GridBagLayout; 
import java.awt.Window; 
import java.lang.reflect.Method; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 
import javax.swing.border.LineBorder; 

public class Test { 

    public static void main(String[] args) { 
     new Test(); 
    } 

    public Test() { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { 
        ex.printStackTrace(); 
       } 

       JFrame frame = new JFrame("Testing"); 
       frame.setUndecorated(true); 
       setOpaque(frame, false); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.setContentPane(new TestPane()); 
       frame.pack(); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      } 
     }); 
    } 

    public class TestPane extends JPanel { 

     public TestPane() { 
      setOpaque(false); 
      setBorder(new LineBorder(Color.RED)); 
      setLayout(new GridBagLayout()); 
      add(new JLabel("Look no hands")); 
     } 

     @Override 
     public Dimension getPreferredSize() { 
      return new Dimension(200, 200); 
     } 

    } 

    public static boolean supportsPerAlphaPixel() { 

     boolean support = false; 

     try { 

      Class<?> awtUtilsClass = Class.forName("com.sun.awt.AWTUtilities"); 
      support = true; 

     } catch (Exception exp) { 
     } 

     return support; 

    } 

    public static void setOpaque(Window window, boolean opaque) { 

     try { 

      Class<?> awtUtilsClass = Class.forName("com.sun.awt.AWTUtilities"); 
      if (awtUtilsClass != null) { 

       Method method = awtUtilsClass.getMethod("setWindowOpaque", Window.class, boolean.class); 
       method.invoke(null, window, opaque); 

      } 

     } catch (Exception exp) { 
     } 

    } 

    public static void setOpacity(Window window, float opacity) { 

     try { 

      Class<?> awtUtilsClass = Class.forName("com.sun.awt.AWTUtilities"); 
      if (awtUtilsClass != null) { 

       Method method = awtUtilsClass.getMethod("setWindowOpacity", Window.class, float.class); 
       method.invoke(null, window, opacity); 

      } 

     } catch (Exception exp) { 

      exp.printStackTrace(); 

     } 

    } 

    public static float getOpacity(Window window) { 

     float opacity = 1f; 
     try { 

      Class<?> awtUtilsClass = Class.forName("com.sun.awt.AWTUtilities"); 
      if (awtUtilsClass != null) { 

       Method method = awtUtilsClass.getMethod("getWindowOpacity", Window.class); 
       Object value = method.invoke(null, window); 
       if (value != null && value instanceof Float) { 

        opacity = ((Float) value).floatValue(); 

       } 

      } 

     } catch (Exception exp) { 

      exp.printStackTrace(); 

     } 

     return opacity; 

    } 

} 

透明

Transparent

import java.awt.Dimension; 
import java.awt.EventQueue; 
import java.awt.GridBagLayout; 
import java.awt.Window; 
import java.lang.reflect.Method; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 

public class Test { 

    public static void main(String[] args) { 
     new Test(); 
    } 

    public Test() { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { 
        ex.printStackTrace(); 
       } 

       JFrame frame = new JFrame("Testing"); 
       frame.setUndecorated(true); 
       setOpacity(frame, 0.5f); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.setContentPane(new TestPane()); 
       frame.pack(); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      } 
     }); 
    } 

    public class TestPane extends JPanel { 

     public TestPane() { 
      setLayout(new GridBagLayout()); 
      add(new JLabel("Look no hands")); 
     } 

     @Override 
     public Dimension getPreferredSize() { 
      return new Dimension(200, 200); 
     } 

    } 

    public static boolean supportsPerAlphaPixel() { 

     boolean support = false; 

     try { 

      Class<?> awtUtilsClass = Class.forName("com.sun.awt.AWTUtilities"); 
      support = true; 

     } catch (Exception exp) { 
     } 

     return support; 

    } 

    public static void setOpaque(Window window, boolean opaque) { 

     try { 

      Class<?> awtUtilsClass = Class.forName("com.sun.awt.AWTUtilities"); 
      if (awtUtilsClass != null) { 

       Method method = awtUtilsClass.getMethod("setWindowOpaque", Window.class, boolean.class); 
       method.invoke(null, window, opaque); 

      } 

     } catch (Exception exp) { 
     } 

    } 

    public static void setOpacity(Window window, float opacity) { 

     try { 

      Class<?> awtUtilsClass = Class.forName("com.sun.awt.AWTUtilities"); 
      if (awtUtilsClass != null) { 

       Method method = awtUtilsClass.getMethod("setWindowOpacity", Window.class, float.class); 
       method.invoke(null, window, opacity); 

      } 

     } catch (Exception exp) { 

      exp.printStackTrace(); 

     } 

    } 

    public static float getOpacity(Window window) { 

     float opacity = 1f; 
     try { 

      Class<?> awtUtilsClass = Class.forName("com.sun.awt.AWTUtilities"); 
      if (awtUtilsClass != null) { 

       Method method = awtUtilsClass.getMethod("getWindowOpacity", Window.class); 
       Object value = method.invoke(null, window); 
       if (value != null && value instanceof Float) { 

        opacity = ((Float) value).floatValue(); 

       } 

      } 

     } catch (Exception exp) { 

      exp.printStackTrace(); 

     } 

     return opacity; 

    } 

} 

现在,在您全面了解该框架的“无边界”之前,这是远离它的工作,但它可能无法在所有平台(如MacOS)上工作,它需要您使用外观和感觉哪些耗材框架边框(比如金属),这不是特别令人愉快的...

+0

谢谢。是否有可能在透明框架上绘制一个不透明的矩形(未填充)?我想实现的是某种“瞄准”效果。 – smwikipedia

+0

或者让我这样说:它就像一块玻璃上画的矩形。 – smwikipedia

+0

是的,但你需要伪装它。基本上,你创建一个透明窗口并使用JPanel,它也是透明的。覆盖面板的paintComponent方法,并使用AlphaComposite来绘制你想要的 – MadProgrammer