简体   繁体   中英

Can a variable refer to a component inside of a FOR?

Here´s what I have:

There are 9 spell levels. 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.

The numbers below (4, 3, 2, 1 and 0s) are how many spells that the mage may cast.

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. 3 spells on level 2, image2square4 through image2square9 become invisible.

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?

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

如果要创建基于图块的游戏,则多维数组或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. I have 9 images showing in each pane, but after started, it should set a few to 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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