创建一个标签数组 [英] Create an array of labels

查看:103
本文介绍了创建一个标签数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建一个标签数组(其中16个),我不确定我所拥有的是正确的还是完全错误的,这可能是完全错误的。我将如何创建这些JLabel?我应该制作一个有16个方块(JLabel)组成棋盘的记忆游戏



我尝试了什么: < br $>


I need to create an array of Labels(16 of them) and Im not sure if what I have is correct or if its entirely wrong which it probably is entirely wrong. And how would I go about creating these JLabels? I'm supposed to be making a memory game with 16 squares(JLabels) which make up the board

What I have tried:

public JLabel[] createLabels()
   {
       JLabel[] l = new JLabel[16];
       for(int i = 0; i < lblBoard.length; i++)
       {
         l[i] = new JLabel("test " + i);
       }
       return l;

推荐答案

这是错的。您正在创建16个16个标签的数组(并且您没有使用实际标签填充它)。例如,请参阅如何在Java中创建一个JLabel数组以打印到JFrame - Stack Overflow [ ^ ]。
That's wrong. You are creating 16 times an array of 16 labels (and you aren't populating it with the actual labels). See, for instance, here How to create an array of JLabels in Java to be printed to a JFrame - Stack Overflow[^].


这篇关于创建一个标签数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆