Java - transparent JFrame, no libs restrictions

Yes, this question is everywhere. But all (working) solutions use the AWTUtilities toolkit, which is limited.

So. I just want to control the opacity of my window. No need to shape or decompose. Just a transparent contentPane (simple) and a transparent JFrame background (ridiculously complicated).

I could have sworn I got the right combination yesterday, but now I can’t reproduce it. There are also some solutions that do not use AWTUtilities, but they do not work ... does anyone have a good solution?

Example of my failed code:

public static void main(String[] args) {
    JFrame test = new JFrame();
    test.setSize(400, 400);
    test.setLocationRelativeTo(null);
    test.setUndecorated(true);
    test.setBackground(new Color(0, 0, 0, 0));
    test.getContentPane().setBackground(new Color(0,0,0,0));
    test.setVisible(true);
}

but it just makes a white square. Close, but not a cigar. I also tried overriding the drawing method, and someone was saying something about discarding the alpha channel, but that made it black (of course). So ... Qaru it.

, , , , .

, , :

, , - . , "... 75% - ". ... , , , . , .

, :

"" http://www.java-gaming.org/index.php?topic=24635.0, , .

http://techgearup.wordpress.com/2008/09/19/transparent-jframe-background/ , .

+5
2

, Java v.6u10 + , . Swing Frames, Dialogs Windows " " . Java v6u10 - , Java.

+1

, , , . , . JTextArea , . !

, , , . Plz, JScrollPane:)

public class gui {
    public frame f;
    public String name = "Transparent?"; //name your frame *
    public JButton exit = new JButton("Exit");
    public gui(){ //non-static context
        f = new  JFrame(name);
        f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); //*
        f.setLocationRelativeTo(null);
        f.setExtendedState(f.getExtendedState() | JFrame.MAXIMIZED_BOTH);
        //maximizes window. if you dont want it, f.setSize(int, int)
        f.setUndecorated(true); //necessary for setOpacity
        f.setOpacity(.7f); //achieve trasparancy
        f.setVisible(true); //show window
        exit.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent arg0){
                System.exit(0);
            }
        });
        f.add(exit, BorderLayout.CENTER); 
        //since X is not available in the window, make a button to exit
    }

    public static void main(){
        new gui();
    }
}
//* Note: since the default Window manager is set off with setUndecorated(true),
// the name and buttons won't really show. 
0

All Articles