将多个JLabel分配给2d数组索引

时间:2014-05-02 16:26:45

标签: java arrays swing jlabel

如何将多个JLabel分配给一个2d数组索引。我知道我发布的代码是错的,我把它放在那里只是为了说明我想要的东西。

private JLabel img1 = new JLabel();
private JLabel img2 = new JLabel();
private JLabel img3 = new JLabel();
private JLabel img4 = new JLabel();
private JLabel img5 = new JLabel();
private JLabel img6 = new JLabel();
private JLabel img7 = new JLabel();
private JLabel img8 = new JLabel();

private JLabel[][] group =  new JLabel[3][];
group[1][1] = new JLabel (img1, img2, img3);
group[1][2] = new JLabel (img1, img4, img5);

1 个答案:

答案 0 :(得分:0)

您不能将多个对象分配给数组的单个元素。

如果要对它们进行分组,请使用List并将列表分配给2D元素。

类似的东西:

List<JLabel> list1 = new ArrayList<JLabel>();
list1.add(img1);
list1.add(img2);
list1.add(img3);

List<JLabel> list2 = new ArrayList<JLabel>();
list2.add(img1);
list2.add(img4);
list3.add(img5);

ArrayList<JLabel>[][] group = new ArrayList<JLabel>[3][2];  // you need to specify the size of the 2nd dimension here if you don't initialize it before using...
group[0][0] = list1;   // 0-based indexing. assuming this is what you wanted...
group[0][1] = list2;