i don’t understand why we use getLayeredPane() before setting the contentPane, in this example :
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("RootLayeredPaneDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create and set up the content pane.
RootLayeredPaneDemo newContentPane = new RootLayeredPaneDemo(
frame.getLayeredPane());
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);
//Display the window.
frame.setSize(new Dimension(300, 350));
frame.setVisible(true);
}
and then in the constructor we just re-use it (layeredPane) :
public RootLayeredPaneDemo(JLayeredPane layeredPane) {
super(new GridLayout(1,1));
//Create and set up the layered pane.
this.layeredPane = layeredPane;
But in another example, we just forget the parameter, for example (new LayeredPaneDemo()):
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("LayeredPaneDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create and set up the content pane.
JComponent newContentPane = new LayeredPaneDemo();
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);
//Display the window.
frame.pack();
frame.setVisible(true);
}
and the constructor :
public LayeredPaneDemo() {
setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
//Create and set up the layered pane.
layeredPane = new JLayeredPane();
?
Ok so the first example refer to http://docs.oracle.com/javase/tutorial/uiswing/components/rootpane.html#layeredpane
and the seconde one refer to : http://docs.oracle.com/javase/tutorial/uiswing/components/layeredpane.html
and the answer is told in the first ref. :
(This) is a version of LayeredPaneDemo that uses the root pane’s layered pane, rather than creating a new layered pane
RootPane are used to manage layers using their references instead of creating new layer… quite clear I think.