无法从数组向GridPane添加Shape

时间:2015-08-30 09:49:15

标签: java javafx javafx-8

作为一项任务,我在JavaFX中制作了老虎机程序。

如果我这样做:

GridPane grid = new GridPane();
grid.add(new Rectangle(20, 20), 0, 0);

它有效。但如果我这样做:

GridPane grid = new GridPane();
Shape[] shapes = new Shape[6];
Rectangle square = new Rectangle(20, 20);
shapes[0] = square;
grid.add(shapes[0], 0, 0);

它不会。它返回以下编译时错误

    Executing D:\Libraries\Documents\NetBeansProjects\SlotMachine\dist\run624299409\SlotMachine.jar using platform C:\Program Files\Java\jdk1.8.0_60\jre/bin/java
Exception in Application start method
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:497)
    at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
    at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
    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:497)
    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$156(LauncherImpl.java:182)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.IllegalArgumentException: Children: duplicate children added: parent = Grid hgap=50.0, vgap=50.0, alignment=CENTER
    at javafx.scene.Parent$2.onProposedChange(Parent.java:454)
    at com.sun.javafx.collections.VetoableListDecorator.add(VetoableListDecorator.java:206)
    at javafx.scene.layout.GridPane.add(GridPane.java:965)
    at slotmachine.SlotMachine.start(SlotMachine.java:71)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$163(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$176(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$174(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$175(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$149(WinApplication.java:191)
    ... 1 more
Exception running application slotmachine.SlotMachine
Java Result: 1

我还尝试更改数组类型并将其声明为Rectangle[],但仍然无法编译。

我不明白为什么。在shapes[0]我有一个Rectangle,为什么它不允许我将其添加到网格中?

编辑:我使用的是数组,因为点击一个旋转按钮后,会显示一个随机形状(从0到5的随机索引)

1 个答案:

答案 0 :(得分:1)

错误表示您要将同一个孩子再次添加到网格窗格。检查您的代码以重新添加。很可能你在那段代码中构造了一些循环和错误。