0

也许这是不可能的,但我想要做的是同时使用 ArrayList 来动态地更改 GUI 中多个元素的可见性。对象是由它们自己以另一种方法创建的。

oldScreen.setVisible (false); oldScreen<1>.setVisible(false); 语句会导致错误。我有一种预感,我的想法不会那么顺利。

这基本上就是我所拥有的,我可以通过什么方式实现这一目标?

private void initScreens() {
// I create some ArrayLists as "screensets" of sorts and put some GUI elements in there
    ArrayList startScreen = new ArrayList();
    ArrayList lostScreen = new ArrayList();
    ArrayList playScreen = new ArrayList();
    startScreen.add(startB);
    startScreen.add(exitB);

    lostScreen.add(yl1);
    lostScreen.add(yl2);
    lostScreen.add(yl3);
    lostScreen.add(yl4);
    lostScreen.add(yl5);


}
private void changeScreen(ArrayList oldScreen,ArrayList newScreen) {
// now i try to create a handy method to handle the length of the arrays itself, so if
i need to make changes to screens I just add them to there array. They are then easily
displayed, and hidden when told.

    int os = oldScreen.size();
    int ns = newScreen.size();

    for (int i = os; i > 0; i--){
        oldScreen<i>.setVisible(false);
        oldScreen<1>.setVisible(false);
    }
4

1 回答 1

2

那是无效的语法。

你正在尝试写

oldScreen.get(i)

您还应该使用泛型 ( ArrayList<Screen>) 来避免强制转换。

于 2012-05-17T14:31:53.757 回答