了解 JLayeredPane 中的图层值:确定 pane.add() 参数的范围

问题描述 投票:0回答:1

pane.add() 中可以使用的最大和最小数字是多少?

public class Main {

    public static void main(String[] args) {

        JLayeredPane pane = new JLayeredPane();
        pane.setPreferredSize(new Dimension(500,500));
        pane.setBackground(Color.pink);
        pane.setOpaque(true);

        JPanel blueBox = new JPanel();
        blueBox.setBounds(100,100,100,100);
        blueBox.setBackground(Color.BLUE);

        JPanel redBox = new JPanel();
        redBox.setBounds(150,100,100,100);
        redBox.setBackground(Color.RED);


        pane.add(blueBox, Integer.valueOf(-948884));
        pane.add(redBox, Integer.valueOf(1009499595));


        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(pane);
        frame.pack();
        frame.setVisible(true);


    }
}

我阅读了文档,但没有找到答案:https://docs.oracle.com/en/java/javase/17/docs/api/java.desktop/javax/swing/JLayeredPane.html

我的猜测是:最小的数字是最小的整数,最大的数字是最大的整数,但我想确定一下😕

java swing
1个回答
-1
投票

将面板放入 JFrame 中应该是 如果您想在 JFrame 的内容窗格中按 Z 顺序堆叠 A.K.A 面板。 注意:所有 JFrame 包含的内部面板实际上是一个“组件”

分层窗格包含与放入 JLayeredPane 中一样多的组件层。

层不应与“位置”混淆,“位置”的编号从第一层或最低层“0”到最后一层和最顶层(插入到 JLayeredPane 的层数减一)。 JLayeredPane 的 HighestLayer() 方法将返回插入的最顶层面板的索引号,而不是位置深度。请参阅 API 文档中的 JLayeredPane setPosition(Component c, int position)

   // frame.getContentPane().add(panel0,-1); 
// z order bottom most
// This however does not get anything more than the default JFrame panel

// If you want to add a stack of 25 panels starting with last
Component[] layercomponent = new Component[25];
JLayeredPane jlayerpane = frame.getLayeredPane();

for(int cm = 0; cm < 25; cm++){
// do not forget to size your components and give them a layout or they cannot be visible
layercomponent[cm] = new JPanel();
layercomponent[cm].setSize(1024,768);
} //enfr

for(int av = 0; av < 25; av++){
jlayerpane.add(layercomponent[av],av);
}
    
    // Component frame.getGlassPane() NOTE Container "extends" Component
    Container framecontainer = frame.getContentPane();
    
    int totalPanels = framecontainer.getComponentCount();
© www.soinside.com 2019 - 2024. All rights reserved.