2013-04-05 4 views
0

자바 스윙에서 0.05f의 불투명도를 가진 투명 jframe을 만들어야합니다. 아래 코드를 시도했지만 작동하지 않습니다. 나는 창문에서 일한다.자바 스윙의 Transparant JFrame

작동하려면 무엇을해야합니까?

 import java.awt.AlphaComposite; 
    import java.awt.EventQueue; 
    import java.awt.Graphics; 
    import java.awt.Graphics2D; 
    import javax.swing.JFrame; 
    import java.awt.Color; 


public class BackgroundNull { 

    private JFrame frame; 

    public static void main(String[] args) { 
    EventQueue.invokeLater(new Runnable() { 
     public void run() { 
      try { 
       BackgroundNull window = new BackgroundNull(); 
       window.frame.setVisible(true); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
    }); 
} 

public BackgroundNull() { 
    initialize(); 

private void initialize() { 
    frame = new JFrame(); 
    frame.setBackground(new Color(0, 0, 0, 0)); 
    frame.setBounds(100, 100, 450, 300); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.setOpacity(0.5f); 
} 

    public void paint(Graphics g) { 
     Graphics2D g2 = (Graphics2D) g.create(); 
     g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.0f)); 

     frame.getContentPane().paint(g2); 
     g2.dispose(); 
    } 

} 
+0

봐 : http://docs.oracle.com/javase/tutorial /uiswing/misc/trans_shaped_windows.html – Sticks

+0

투명하고 반투명하지 않습니다. – MadProgrammer

답변

0

플랫폼에 따라, 윈도우 투명성은 기본 (금속) 느낌보고와 함께 작동 할 수 있습니다.

Mac OSX 및 Windows 7에서 시스템의 모양과 느낌을 사용해 보았습니다. 코드에서 누락 된 부분이있다

...

JFrame.setDefaultLookAndFeelDecorated(true);

import java.awt.*; 
import javax.swing.*; 
import static java.awt.GraphicsDevice.WindowTranslucency.*; 

public class TranslucentWindowDemo extends JFrame { 

    public TranslucentWindowDemo() { 
     super("TranslucentWindow"); 
     setLayout(new GridBagLayout()); 

     setSize(300, 200); 
     setLocationRelativeTo(null); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     //Add a sample button. 
     add(new JButton("I am a Button")); 
    } 

    public static void main(String[] args) { 
     // Determine if the GraphicsDevice supports translucency. 
     GraphicsEnvironment ge = 
         GraphicsEnvironment.getLocalGraphicsEnvironment(); 
     GraphicsDevice gd = ge.getDefaultScreenDevice(); 

     //If translucent windows aren't supported, exit. 
     if (!gd.isWindowTranslucencySupported(TRANSLUCENT)) { 
      System.err.println(
          "Translucency is not supported"); 
      System.exit(0); 
     } 

     JFrame.setDefaultLookAndFeelDecorated(true); 

     // Create the GUI on the event-dispatching thread 
     SwingUtilities.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { 
       } 
       TranslucentWindowDemo tw = new TranslucentWindowDemo(); 

       // Set the window to 55% opaque (45% translucent). 
       tw.setOpacity(0.55f); 

       // Display the window. 
       tw.setVisible(true); 
      } 
     }); 
    } 
} 

지금, 여기에 슬픈 소식입니다. Java 6에서는 작동하도록 할 수 있습니다. 투명 네이티브 윈도우를 만들 것입니다 (자바 6에서)

다음 코드 ... 자바 창을 투명하게 만들기에 대한 자세한 내용은 여기

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(); 
    } 
}