简体   繁体   English

变量可以引用FOR内部的组件吗?

[英]Can a variable refer to a component inside of a FOR?

Here´s what I have: 这是我所拥有的:

There are 9 spell levels. 有9个法术等级。 The mage may cast only a certain number of spells per day. 法师每天只能施放一定数量的咒语。

I built a tabbed pane (9 tabs) that has 9 empty squares (images) in each tab. 我建立了一个选项卡式窗格(9个选项卡),每个选项卡中都有9个空方块(图像)。

The numbers below (4, 3, 2, 1 and 0s) are how many spells that the mage may cast. 下面的数字(4、3、2、1和0s)是法师可以施展的法术数量。

I want to set the squares that won´t be used unvisible. 我想设置不会被使用的正方形。

example: 例:

if he can cast 4 spells on level 1, image1square5 through image1square9 become invisible. 如果他可以在等级1上施放4个咒语,则image1square5至image1square9变为不可见。 3 spells on level 2, image2square4 through image2square9 become invisible. 在第2级,从image2square4到image2square9的3个咒语变得不可见。

I tried this: 我尝试了这个:

int lvl1, lvl2, lvl3, lvl4, lvl5,lvl6,lvl7,lvl8,lvl9;
    lvl1 = 4 +1;
    lvl2 = 3 +1;
    lvl3 = 3 +1;
    lvl4 = 2 +1;
    lvl5 = 1 +1;
    lvl6 = 0 +1;
    lvl7 = 0 +1;
    lvl8 = 0 +1;
    lvl9 = 0 +1;

for (int i = n1; i <= 9; i++) {
            image1square+i.setVisible(false);
    }
for (int i = n2; i <= 9; i++) {
            image2square+i.setVisible(false);
    }

and so on... 等等...

How do I use a variable inside of a FOR? 如何在FOR中使用变量?

使用数组或列表,而不是顺序命名变量。

如果要创建基于图块的游戏,则多维数组或List<List<?>>会更合适。

I went back and studied, but still won´t work. 我回去学习,但仍然无法正常工作。 Here´s what I have now: 这是我现在拥有的:

I omitted the 我省略了

jImage1Empty1, jImage1Empty2, jImage2Empty1, etc,

but they are declared. 但是他们被宣布了。 They are jlabels with an icon on it. 它们是带有标签的jlabel。 I have 9 images showing in each pane, but after started, it should set a few to setvisible(false). 我在每个窗格中显示9张图像,但是开始后,应该将其中一些设置为setvisible(false)。

int lvl[] = new int[9];
    lvl[0] = 4 +1;
    lvl[1] = 3 +1;
    lvl[2] = 3 +1;
    lvl[3] = 2 +1;
    lvl[4] = 1 +1;
    lvl[5] = 0 +1;
    lvl[6] = 0 +1;
    lvl[7] = 0 +1;
    lvl[8] = 0 +1;
    String img1 = "jImage1Empty";
    String img2 = "jImage2Empty";


    for (int i = 0; i < 9; i++) {
            img1 +lvl[0].setVisible(false);
    }
    for (int i = 0; i < 9; i++) {
            img2 +lvl[1].setVisible(false);
    }
}

I wanted the result to be like: 我希望结果是这样的:

jImage1Empty5.setVisible(false);
jImage1Empty6.setVisible(false);
jImage1Empty7.setVisible(false);
jImage1Empty8.setVisible(false);
jImage1Empty9.setVisible(false);
jImage2Empty4.setVisible(false);
jImage2Empty5.setVisible(false);
jImage2Empty6.setVisible(false);
jImage2Empty7.setVisible(false);
jImage2Empty8.setVisible(false);
jImage2Empty9.setVisible(false);
etc...

But not as string. As a command.

Is it possible? 可能吗? Help 救命

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM