Java 二维数组网格在每个单元的中间放置字符
具有2d字符数组,用来表示地图Java 二维数组网格在每个单元的中间放置字符,java,arrays,user-interface,awt,Java,Arrays,User Interface,Awt,具有2d字符数组,用来表示地图 char test[][]={{'A','B','C'},{'D','E','F'},{'G','H','I'}; 如何使用java.awt库创建一个3x3网格,每个单元格的中心都有一个字符 下面的代码给了我一个网格,但我仍然不知道如何将每个字符放在每个单元格的中心 int sideLength=115; int distance=sideLength*3; int i=0; int j=0; for ( int x=90; x<=distance; x
char test[][]={{'A','B','C'},{'D','E','F'},{'G','H','I'};
如何使用java.awt库创建一个3x3网格,每个单元格的中心都有一个字符
下面的代码给了我一个网格,但我仍然不知道如何将每个字符放在每个单元格的中心
int sideLength=115;
int distance=sideLength*3;
int i=0;
int j=0;
for ( int x=90; x<=distance; x+=sideLength )
{
for( int y=30; y<=distance; y+=sideLength )
{
g.drawRect(x,y,sideLength, sideLength);
g.drawString(Character.toString(test[i][j]), sideLength/2, sideLength/2);
}
i++;
j++;
}
int sideLength=115;
内距=边长*3;
int i=0;
int j=0;
对于(intx=90;x除了烟雾和镜子什么都没有
好的,JLabel
s和Border
s
只有烟雾和镜子
好的,JLabel
s和Border
s
很接近,但我会稍微重构循环。与其迭代距离,不如迭代数组中的索引。然后使用数组索引导出位置
char[][] test = {{'A','B','C'}, {'D','E','F'}, {'G','H','I'}};
int sideLength=115;
for (int i = 0; i < test.length; i++) {
int x = 90 + sideLength * i;
for (int j = 0; j < test[j].length; j++) {
int y = 30 + sideLength * j;
g.drawRect(x, y, sideLength, sideLength);
g.drawString(
Character.toString(test[i][j]),
x + sideLength / 2,
y + sideLength / 2);
}
}
char[]test={{'A','B','C'},{'D','E','F'},{'G','H','I'};
内边长=115;
对于(int i=0;i
您很接近,但我会稍微重构您的循环。与其迭代距离,不如迭代数组中的索引。然后使用数组索引导出位置
char[][] test = {{'A','B','C'}, {'D','E','F'}, {'G','H','I'}};
int sideLength=115;
for (int i = 0; i < test.length; i++) {
int x = 90 + sideLength * i;
for (int j = 0; j < test[j].length; j++) {
int y = 30 + sideLength * j;
g.drawRect(x, y, sideLength, sideLength);
g.drawString(
Character.toString(test[i][j]),
x + sideLength / 2,
y + sideLength / 2);
}
}
char[]test={{'A','B','C'},{'D','E','F'},{'G','H','I'};
内边长=115;
对于(int i=0;i
GridLayout
先到mind,然后是每个单元格的gridbagloayout,因为当一个组件以默认方式添加到使用该组件的容器中时,布局将使组件居中。您可能还想先让一个通读GridLayout
先到mind,然后是每个单元格的gridbagloayout,因为当一个组件t以默认方式添加到使用此选项的容器中,布局将使组件居中。您也可以通过通读此任务开始。此任务不需要使用此解决方案所需的布局知识。这符合项目要求。@Julian抱歉,您是否错过了第二节?您还需要“忘记”提及“限制”:P@MadProgrammer你说得对。我确实忘了提到这一点。我对你的答案投了更高的票,因为我需要在下一个项目中实现gridlayout。我将在以后的问题中更加具体:)本作业不需要了解使用该解决方案所需的布局知识。这符合项目要求。@Julian对不起,您错过了第二节了吗?您还“忘记”提到了“限制”" :P@MadProgrammer你说得对。我确实忘了提那件事。我对你的答案投了赞成票,因为我需要在下一个项目中实现gridlayout。我将在以后的问题中更加具体:)
char[][] test = {{'A','B','C'}, {'D','E','F'}, {'G','H','I'}};
int sideLength=115;
for (int i = 0; i < test.length; i++) {
int x = 90 + sideLength * i;
for (int j = 0; j < test[j].length; j++) {
int y = 30 + sideLength * j;
g.drawRect(x, y, sideLength, sideLength);
g.drawString(
Character.toString(test[i][j]),
x + sideLength / 2,
y + sideLength / 2);
}
}