我正在尝试将我的 2D 数组移动到 GridBagLayout

I'm trying to move my 2D-array into a GridBagLayout

我正在开发一个简单的游戏,这个 2D 数组就是地图,可以用作碰撞框。我希望位置 level[0][0] 在 gridbag positon (0,0) 和 level[1][0] 在 (0,1) 等等。但是,我无法让它工作,我不明白为什么不。

import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;

public class TestSnaken {

    private JFrame frame;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    TestSnaken window = new TestSnaken();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public TestSnaken() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 1024, 768);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        GridBagLayout gridBagLayout = new GridBagLayout();
        gridBagLayout.columnWidths = new int[]{0, 0};
        gridBagLayout.rowHeights = new int[]{0, 0};
        gridBagLayout.columnWeights = new double[]{0.0, Double.MIN_VALUE};
        gridBagLayout.rowWeights = new double[]{0.0, Double.MIN_VALUE};
        frame.getContentPane().setLayout(gridBagLayout);
        GridBagConstraints grid = new GridBagConstraints();

        int[][] level1 = {
            {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
            {1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
            {1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
            {1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
            {1,0,0,0,1,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,1},
            {1,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1},
            {1,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1},
            {1,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1},
            {1,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1},
            {1,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1},
            {1,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1},
            {1,0,0,0,1,0,0,0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,1,0,0,0,0,0,0,1},
            {1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,1},
            {1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,1},
            {1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,1},
            {1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,1},
            {1,0,0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,1},
            {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,1},
            {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,1},
            {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,1},
            {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,1},
            {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,1,0,0,0,0,0,0,1},
            {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
            {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
            {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
            {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
            {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
            {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
            {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}

        };
        //Here I'm trying to move the 2D-array into the GridBagLayout
        JLabel label;
        int i = 0;
        int j = 0;

        for (int[] arr: level1) {
            grid.gridy = i;
            for (int elem: arr) {
                if(elem == 1){
                    label = new JLabel("x");
                }
                else {
                    label = new JLabel(" ");
                }
                grid.gridx = j;
                frame.getContentPane().add(label, grid);
                j++;
            }
            i++;
        }
    }
}

保留gridBayLayout参数。我认为你用错了。并在第二个 for 循环的每个 运行 之前将 j 变量重置为 0!

private void initialize() {
    [...]
    GridBagLayout gridBagLayout = new GridBagLayout();
    //gridBagLayout.columnWidths = new int[]{0, 0};
    //gridBagLayout.rowHeights = new int[]{0, 0};
    //gridBagLayout.columnWeights = new double[]{0.0, Double.MIN_VALUE};
    //gridBagLayout.rowWeights = new double[]{0.0, Double.MIN_VALUE};

    [...]

    JLabel label;
    int i = 0;
    int j = 0;

    for (int[] arr: level1) {
        grid.gridy = i;
        j = 0; // !!!!!!!!!!!!!!!
        for (int elem: arr) {
            if(elem == 1){
                label = new JLabel("x");
            }
            else {
                label = new JLabel(" ");
            }
            grid.gridx = j;
            frame.getContentPane().add(label, grid);
            j++;
        }
        i++;
    }
}

那么它对我有用。

如您所见,您的迭代方式很容易出错。一个简单的for循环会更清晰:

for (int i = 0; i < level1.length; i++) {
    for (int j = 0; j < level1[0].length; j++) {
        grid.gridy = i;
        grid.gridx = j;
        if (level1[i][j] == 1) {
            label = new JLabel("x");
        }
        else {
            label = new JLabel(" ");
        }
        frame.getContentPane().add(label, grid);
    }
}

此外,设置列和行的宽度和高度的行不会在您的代码中执行任何操作。

以下是使用frame.pack()后的内容。 setBounds 有不知道你要运行你的代码的屏幕尺寸的问题。