2012-08-08 85 views
1

在下面的代碼中,我無法理解setBackground()方法爲哪個圖層設置背景。其次,當我包括這一行爲什麼在窗口中有一個洞,意味着當我在窗口之間點擊時,它最小化,因爲我已經點擊了其他地方。無法正確理解代碼

import java.awt.Color; 
import java.awt.FlowLayout; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
java.awt.GraphicsDevice; 
import java.awt.GraphicsDevice.WindowTranslucency; 
import java.awt.GraphicsEnvironment; 

import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.SwingUtilities; 


public class transparentWindow extends JFrame { 

public transparentWindow() { 
    // TODO Auto-generated constructor stub 
    //JFrame jfrm=new JFrame("Transparent Window"); 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    setSize(300,200); 
    getContentPane().setLayout(new FlowLayout()); 
    //setBackground(new Color(0,0,0,0)); 

    add(new JButton("Enter")); 
    setOpacity(0.7f); 
    setVisible(true); 
} 
public static void main(String[] args) { 
    // TODO Auto-generated method stub 
    GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment(); 
    GraphicsDevice gd=ge.getDefaultScreenDevice(); 
    if(!gd.isWindowTranslucencySupported(WindowTranslucency.TRANSLUCENT)) 
    { 
     System.out.println("Transparency not supported"); 
     System.exit(0); 
    } 
    JFrame.setDefaultLookAndFeelDecorated(true); 
    SwingUtilities.invokeLater(new Runnable(){public void run(){new transparentWindow();}}); 
} 

} 

回答

2

未叫特定對象上的所有方法,實際上是呼籲this,所以

setBackground(new Color(0,0,0,0)); 

就像

this.setBackground(new Color(0,0,0,0)); 

這意味着它是呼籲JFrame

+0

然後JFrame中應該有變成透明的,但內容窗格仍然不透明。就像contentPane()是jframe的一部分,因此設置jframe透明使所有3內容窗格,玻璃窗格和Layered窗格透明。 – 2012-08-08 10:57:56

+0

@Naveen嘗試將內容窗格設置爲透明,最簡單的方法是創建一個JPanel,在面板上調用setOpaque(false),然後調用JFrame的setContentPane,傳入面板 – MadProgrammer 2012-08-08 11:07:08

+0

@MadProgrammer使用此方法將使內容窗格透明,但JFrame仍然不透明,因此它不起作用。 – 2012-08-08 11:14:49