Reputation:
I've come across an example on the internet which uses both of these statements:
dp.add( bg , new Integer( 50 ) );
(dp being a JDesktopPane object and bg being a JLabel)
setLayeredPane( dp );
If you would like to know how they are used, then this is what I was looking at: http://www.coderanch.com/t/329874/GUI/java/put-background-image-swing
I'm a beginner when it comes to Java and I understand the rest of the example, just not these two statements - and it bugs me that I don't know what they do! The bit that confuses me the most is "new Integer( 50 ) )" but please could you give a thorough, beginner-friendly explanation of both? I'd much appreciate it.
Thanks in advance,
Alex.
Upvotes: 0
Views: 83
Reputation: 29646
See the documentation on JLayeredPane
.
Each layer is a distinct integer number. The layer attribute can be set on a
Component
by passing anInteger
object during the add call. For example:layeredPane.add(child, JLayeredPane.DEFAULT_LAYER);
or
layeredPane.add(child, new Integer(10));
You can find the integer values of the default layer values here.
dp.add(lbl,new Integer(50));
The above adds the JLabel
component lbl
to the JDesktopPane
(which is a JLayeredPane
) with the specified layer of 50
. Components added to dp
with layers that are less than 50 will be rendered before, while components with layers greater than 50 will be rendered after -- i.e. a simple depth order, where greater layers refer to nearer components.
setLayeredPane( dp );
This sets the JFrame
represented by the ImagePaneTest
object (which probably shouldn't be a subclass) to use dp
as its layered pane. You can see how Swing top-level containers work in the relevant Java Tutorial.
Upvotes: 2