如何在网格布局中重叠组件?

How do you overlap components in the grid layout?

为了我的高级项目,我正在 Java 编写游戏。它有一个方形网格,您需要通过它才能完成目标。我发现网格布局可以满足我的需要,除了我似乎无法让主角与关卡上的空间重叠(继续)。我基本上希望能够将图标 'start' 与任何其他图标(例如 'empty')重叠。我还没有找到这样做的好方法(当然我是新手)。我想知道如何做到这一点,或者即使我不能。如果我不能做到这一点,我要求你留下一个 link 来证明我可以实现我的目标的方法。

下面是与设置关卡相关的代码中的一个小片段。当我尝试将主角添加到网格上的指定区域时,它只是将所有内容向上推 1.

public loadWindow() throws IOException {

    try{
        level = new FileReader("levels/debug1.txt");

        while ((type = level.read()) != -1){
                if (type == 'a'){
                    icon = start;}
                if (type == 'b'){
                    icon = empty;}

                //Includes the rest of the if statemets

                if (type == 'x'){
                    test = false;}


                if (test == true){
                    JLabel spot = new JLabel(new ImageIcon(icon));
                    add(spot);}
            }

        } finally {
            if (level != null){
                level.close();
        }
    }
}

使用 JLayeredPane,您可以在 JLayeredPane 的不同层(默认、调色板、模态、弹出窗口、拖动)添加各种组件,如 JButton、JPanel 等。

要将 JButton 添加到 'Default Layer',请使用:jlayeredpane.add(jbutton, JLayeredPane.DEFAULT_LAYER);