javafx中的多个Hbox

时间:2017-06-30 16:11:28

标签: java javafx

我在JavaFX中有多个Hbox,如下所示:

HBox hb = new HBox(10);
HBox hb2 = new HBox(10);
HBox hb3 = new HBox(10);
HBox hb4 = new HBox(10);
etc..

我想将hbox创建放在循环中,但我不确定如何做到这一点。

我试过这样的话:

HBox[] array = new HBox[4];
for (int i = 0; i < 3; i++){
    array[i] = new HBox(10);
}

但是当我尝试向hbox添加一些内容时,我收到此错误:

Exception in Application start method
Exception in thread "main" java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(LauncherImpl.java:182)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NullPointerException
    at OptieScherm.start(OptieScherm.java:74)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
    ... 1 more`

谢谢,

2 个答案:

答案 0 :(得分:0)

像这样:

ArrayList<HBox> boxes = new ArrayList<HBox>();
for(int i = 0;i<10;i++){
    Hbox h = new HBox(10);
    boxes.add(h);
}

然后只需从列表中访问它们

答案 1 :(得分:-2)

是的,您可以执行以下操作:

首先:找到你想要添加HBoxes的元素。

第二个创建一个全球性的HBox,如:

class TicketManager(models.Manager):
def create_in_atom(self, ticket):
    if type(ticket) is not self.model:
        raise Exception('Not a Ticket object.')

然后循环并为变量分配HBox实例然后将创建的HBox分配给根元素:

{{1}}