简体   繁体   中英

How to create transluent window in Java swing?

I work with java and I want to make a translucent window (like a black with 80% alpha). The basis of my code is those in the following post: How to make a transparent JFrame but keep everything else the same?

The problem is that when I run my program, the window is translucent but as you may see on the following picture, the background color (magenta) appears only on the text, not on the white. 在此处输入图片说明 Even worse, when I choose a black with transparency Color(0,0,0,125) as drawing color, nothing appears on the screen.

Here is what I have so far :

public class DssWindow extends JFrame
{
    /**
     * 
     */
    private static final long serialVersionUID = 2455172975892566000L;

    /**
     * 
     */
    public DssWindow()
    {
        super("My frame");

        setUndecorated(true);   
        setBackground(new Color(0,0,0,0));
        setAlwaysOnTop(true);
        JPanel contentPane = new JPanel()
        {
            @Override
            public void paintComponent(Graphics g)
            {
                super.paintComponent(g);
                if(!(g instanceof Graphics2D))
                {
                    return;
                }
                Graphics2D gr = (Graphics2D)g.create();
                gr.setColor(new Color(255,0,255,200));
                gr.fillRect(0, 0, getWidth(), getHeight());
            }
        };
        contentPane.setOpaque(false);
        Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
        contentPane.setPreferredSize(new Dimension(screen.width,screen.height));
        setContentPane(contentPane);
        pack();
    }
}

Do someone knows/has a clue of what is the problem ? I am working on Linux (may this is a part of the answer).

Thank you.

好的,这里是我的问题: Linux图形驱动程序上的Java窗口半透明性似乎是这种行为的原因。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM