Reputation: 11
I have tried setting the layout of a JFrame
, and it doesn't seem to work. Is this possible?
JFrame frame = new JFrame();
frame.setLayout(new FlowLayout());
System.out.println(frame.getLayout().toString());
The output I get from this is:
java.awt.BorderLayout[hgap=0,vgap=0]
Upvotes: 0
Views: 224
Reputation: 347314
You've run into a minor hierarchal issue associated with JFrame
.
JFrame
isn't actually a single component, it's a composite component. This means it contains a number of layers, which make up the actual structure of the window, for example...
Prior to 1.5 (I think), you would have been required to use JFrame#getContentPane#setLayout
and JFrame#getContentPane#getLayout
, which, as you can imagine, is tedious to type.
Since 1.5, you can now call JFrame#setLayout
directly and the call will be forwarded directly to the contentPane
instead. The problem is, getLayout
still needs to return the layout manager used by the frame itself, which is the hole you've found yourself in.
If, instead, you use ...
frame.setLayout(new FlowLayout());
System.out.println(frame.getContentPane().getLayout());
It will print java.awt.FlowLayout[hgap=5,vgap=5,align=center]
Yes, I know, it's not entirely obvious, which is why the original API required you to be specific about your intent, calling getContentPane
directly, rather then passing the call on automatically
Upvotes: 1