A layered pane is a Swing container which is used to hold the various components using the concept of layers. The components present in the upper layer overlaps the components present in the lower layer. The layered pane is created using the JLayeredPane class. the only constructor of this class is JLayeredPane ().
Example: A program to demonstrate the use of JLayeredPane class.
import java.awt.*;
import javax.swing.*;
class LayerExample extends JApplet
{
JFrame jf ;
JLayeredPane LPane;
JButton first, second, third;
LayerExample ()
{
jf =new JFrame(“Layered Pane Example”);
LPane =new JLayeredPane();
jf.getContentPane() .add(LPane);
first= new JButton(“First”);
first.setBackground(Color.red);
first.setBounds(50,30,100,100);
second= new JButton(“Second”);
second.setBackground(Color.yellow);
second.setBounds(140,60,100,100);
third= new JButton(“Third”);
third.setBackground(Color.green);
third.setBounds(230,90,100,100);
LPane.add(first, new Integer(3));
LPane.add(second, new Integer(2));
LPane.add(third, new Integer(1));
jf.setDefaultCloseOperation(jf.EXIT_ON_CLOSE);
jf.setSize (400,300) ;
jf.setVisible(true);
}
public static void main(String args[])
{
LayerExample le= new LayerExample();
}
}
The output of the program is shown in Figure