java fx vbox IndexOutOfBoundsException

问题描述 投票:0回答:1

嘿伙计们是java fx的新手。我的代码使用rand func(1-16)的输出更改标签设计。最初我的代码工作没有错误,但我希望我的设计自动调整大小响应页面,所以我将我的16标签放在Vbox中,它现在是一个响应页面。问题就在这里。我不明白如何通过代码访问vbox。我的代码无法访问标签,它会给出错误。当我把我的标签从vbox代码中移除时再次运行。我想知道使用vbox是okey吗?任何建议,将不胜感激。

int myrand = rand.nextInt(15)+1;
System.out.println(myrand + "blue");
root.getChildrenUnmodifiable().get(myrand).setStyle("-fx-background-color: blue"); 

然而;代码指向此行,行号上没有任何错误符号。

root.getChildrenUnmodifiable().get(myrand).setStyle("-fx-background-color: blue");

这是完整的错误日志

Exception in thread "Timer-0" java.lang.IndexOutOfBoundsException: Index: 10, Size: 2
10blue
    at java.util.ArrayList.rangeCheck(ArrayList.java:653)
    at java.util.ArrayList.get(ArrayList.java:429)
    at com.sun.javafx.collections.ObservableListWrapper.get(ObservableListWrapper.java:89)
    at com.sun.javafx.collections.VetoableListDecorator.get(VetoableListDecorator.java:306)
    at javafx.collections.FXCollections$UnmodifiableObservableListImpl.get(FXCollections.java:936)
    at javafxapp.Lighproject$1.run(Lighproject.java:42)
    at java.util.TimerThread.mainLoop(Timer.java:555)
    at java.util.TimerThread.run(Timer.java:505) 

如您所见,它可以使用1循环并给出错误。这是我的层次结构gluon scene builder Eror属于顶级Vbox。

javafx indexoutofboundsexception scenebuilder gluon
1个回答
0
投票

尝试使用int myrand = rand.nextInt(root.getChildren().size());

发生这种情况是因为你的myrand变量大于root组中的节点数。

© www.soinside.com 2019 - 2024. All rights reserved.