gpt4 book ai didi

java - 无法将形状从数组添加到 GridPane

转载 作者:行者123 更新时间:2023-12-01 11:11:33 26 4
gpt4 key购买 nike

作为一项作业,我正在 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] 中,我确实有一个 矩形,为什么它不允许我将其添加到网格中?

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

最佳答案

该错误表明您正在将同一个子项再次添加到网格 Pane 中。检查您的代码以重新添加。很可能您在该代码中构建了一些循环和错误。

关于java - 无法将形状从数组添加到 GridPane,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32295234/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com