Java - the ol' transparent JFrame, without restricted libs
Yes, this question is everywhere. But all of the (working) solutions use the AWTUtilities toolkit, which is restricted.
So. I just want to control the opacity of my window. No need to shape or undecorate. Just a transparent contentPane (easy), and a transparent JFrame background (ridiculously difficult).
I could have sworn I got the right combination of stuff yesterday, but now I can't seem to reproduce it. Also there are some solutions out there without using AWTUtilities, but they don't work...does someone have a good solution?
An example of my failing 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 this just makes a white square. Close but no cigar. I've also tried overriding the paint method, and someone was saying something about throwing out the alpha channel, but that made开发者_Go百科 it black (of course). So...Stack Overflow it is.
If there's a dupe that answers this exactly, please point me to it and I'll delete right away.
Update Per requests in comments, here's a history of how I arrived here:
The first link everyone arrives at is how to create translucent and shaped windows. This has several lines in there similar to "...the frame translucent with a 75% level of opacity." So...looks like they're defined the same, at least in that article. Unfortunately they use the library.
A couple links that I could chase down:
A non-working "working" solution is reported at http://www.java-gaming.org/index.php?topic=24635.0 which shows some hope but I couldn't get it working.
http://techgearup.wordpress.com/2008/09/19/transparent-jframe-background/ is a hackish way using screenshots and is duplicated in a few other places.
You can't do this without using Java v.6u10+ or third-party libraries that call native code. In Swing Frames, Dialogs, and Windows are considered 'top-level components' and are rendered in the underlying windowing system. Before Java v6u10, all top-level components had a default, opaque light-grayish background, and there was no way to change this from Java code.
Sigh I came across the same problem, and after a lot of time working on it, I found a way to get a properly set transparent screen on my computer. However, a lot of things stop working as soon as you set the frame transparent. If you use JTextArea declaration anywhere, it immediately stop working. It won't even throw an error!
So here's the code, hope this is of some use. Plz reply if you make a working JScrollPane with it :)
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.
精彩评论