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

}
4

2 回答 2

2

所有没有在特定对象上调用的方法,实际上都被调用了this,所以

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

就像

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

这意味着它在JFrame.

于 2012-08-08T10:53:07.203 回答
2

您会发现的另一个问题是,设置框架opacity将平等地影响它的所有孩子

如果您想看到一个很好的长时间讨论(和示例),请查看如何设置 JFrame 背景透明但 JPanel 或 JLabel 背景不透明?

于 2012-08-08T11:09:40.897 回答