import javax.swing.*; import java.awt.Color; import java.awt.event.*; public class SimpleLayers { public static void main(String[] args) { // Create a frame & gets its layered pane JFrame f = new JFrame(); f.addWindowListener( new WindowAdapter() { public void windowClosing( WindowEvent e ) { e.getWindow().dispose(); System.exit( 0 ); } } ); JLayeredPane lp = f.getLayeredPane(); // Create 3 buttons. Since JLayeredPane has no layoutmanager we use // setBounds(). This is not a good idea. Try resizing the frame and // you see why. JButton top = new JButton(); top.setBackground(Color.white); top.setBounds(20, 20, 50, 50); JButton middle = new JButton(); middle.setBackground(Color.gray); middle.setBounds(40, 40, 50, 50); JButton bottom = new JButton(); bottom.setBackground(Color.black); bottom.setBounds(60, 60, 50, 50); // Place the buttons in different layers. The absolute integer values // are of no importance. It is the relation between them that matters. lp.add(middle, new Integer(2)); lp.add(top, new Integer(3)); lp.add(bottom, new Integer(1)); // Show the frame f.setSize(150, 150); f.setVisible(true); } }