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

时间:2015-12-10 11:27:19

标签: java multidimensional-array jframe gridbaglayout

我正在制作一个简单的游戏,这个2D阵列就是地图,可以作为一个命中箱。我希望位置级别[0] [0]位于网格位置(0,0)中,级别[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++;
        }
    }
}

2 个答案:

答案 0 :(得分:0)

保留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++;
    }
}

然后它对我有用。

答案 1 :(得分:0)

您注意到,迭代的方式容易出错。一个简单的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的问题是不知道要运行代码的屏幕大小。

enter image description here