Implement buttons at different layers : JLayeredPane « Swing « Java Tutorial






import java.awt.Color;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLayeredPane;

public class Main extends JFrame {
  public Main() {
    setSize(200, 150);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    JLayeredPane lp = getLayeredPane();
    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);

    lp.add(middle, new Integer(2));
    lp.add(top, new Integer(3));
    lp.add(bottom, new Integer(1));
    
    setVisible(true);
  }

  public static void main(String[] args) {
    new Main();
    
  }
}








14.52.JLayeredPane
14.52.1.JLayeredPane Layer Constants
14.52.2.Implement buttons at different layers
14.52.3.The JLayeredPane serves as the main component container of a JRootPane.
14.52.4.Moves an image of Duke around within a layered pane in response to mouse motion events.Moves an image of Duke around within a layered pane in response to mouse motion events.