📅  最后修改于: 2020-10-01 03:17:42             🧑  作者: Mango
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. |
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);
}
}
输出: