📜  Java Swing-JLayeredPane

📅  最后修改于: 2020-10-01 03:17:42             🧑  作者: Mango

Java JLayeredPane

JLayeredPane类用于增加摆动容器的深度。它用于为组件定位提供第三维,并将深度范围划分为几个不同的层。

JLayeredPane类声明

public class JLayeredPane extends JComponent implements Accessible

常用的构造函数:

Constructor Description
JLayeredPane It is used to create a new JLayeredPane

常用方法:

Method Description
int getIndexOf(Component c) It is used to return the index of the specified Component.
int getLayer(Component c) It is used to return the layer attribute for the specified Component.
int getPosition(Component c) It is used to return the relative position of the component within its layer.

Java JLayeredPane示例

import javax.swing.*;
import java.awt.*;
public class LayeredPaneExample extends JFrame {
  public LayeredPaneExample() {
    super("LayeredPane Example");
    setSize(200, 200);
    JLayeredPane pane = getLayeredPane();
    //creating buttons
    JButton top = new JButton();
    top.setBackground(Color.white);
    top.setBounds(20, 20, 50, 50);
    JButton middle = new JButton();
    middle.setBackground(Color.red);
    middle.setBounds(40, 40, 50, 50);
    JButton bottom = new JButton();
    bottom.setBackground(Color.cyan);
    bottom.setBounds(60, 60, 50, 50);
    //adding buttons on pane
    pane.add(bottom, new Integer(1));
    pane.add(middle, new Integer(2));
    pane.add(top, new Integer(3));
  }
  public static void main(String[] args) {
  LayeredPaneExample panel = new  LayeredPaneExample();
      panel.setVisible(true);
  }
}

输出: