gpt4 book ai didi

java - 在 BorderPane 中设置节点位置时出错

转载 作者:行者123 更新时间:2023-11-30 03:08:42 27 4
gpt4 key购买 nike

每当我编译程序时,我都会收到此错误: java.lang.reflect.InitationTargetException 当我将 BorderPane 的内部节点(HBox)设置为底部时,会发生该错误。为什么我会收到此错误以及如何解决它?

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.layout.*;
import javafx.scene.control.*;
import javafx.scene.shape.*;
import javafx.scene.paint.*;
import javafx.geometry.*;
import java.util.*;
import javafx.scene.text.*;
public class Test extends Application
{
public void start(Stage stage)
{
BorderPane pane = new BorderPane();
HBox hBox = new HBox(10);
boolean player1 = true;

Label label = new Label("Player" + ((player1) ? "1" : "2") + "'s turn");
label.setFont(Font.font(20));

TextField colField = new TextField();
colField.setPrefColumnCount(1);

Button submit = new Button("Submit");

hBox.getChildren().addAll(label, colField, submit);

pane.getChildren().add(hBox);
pane.setBottom(hBox);

Scene scene = new Scene(pane);
stage.setTitle("Connect 4");
stage.setScene(scene);
stage.show();


}
}

完全错误:

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: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$152(LauncherImpl.java:182)
at com.sun.javafx.application.LauncherImpl$$Lambda$49/1645995473.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.IllegalArgumentException: Children: duplicate children added: parent = BorderPane@6bb9ea3e
at javafx.scene.Parent$2.onProposedChange(Parent.java:451)
at com.sun.javafx.collections.VetoableListDecorator.add(VetoableListDecorator.java:206)
at javafx.scene.layout.BorderPane$BorderPositionProperty.invalidated(BorderPane.java:680)
at javafx.beans.property.ObjectPropertyBase.markInvalid(ObjectPropertyBase.java:111)
at javafx.beans.property.ObjectPropertyBase.set(ObjectPropertyBase.java:146)
at javafx.scene.layout.BorderPane.setBottom(BorderPane.java:296)
at Test.start(Test.java:30)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$159(LauncherImpl.java:863)
at com.sun.javafx.application.LauncherImpl$$Lambda$52/5501708.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$172(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl$$Lambda$45/186276003.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$null$170(PlatformImpl.java:295)
at com.sun.javafx.application.PlatformImpl$$Lambda$48/1769582490.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$171(PlatformImpl.java:294)
at com.sun.javafx.application.PlatformImpl$$Lambda$47/237061348.run(Unknown Source)
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$145(WinApplication.java:101)
at com.sun.glass.ui.win.WinApplication$$Lambda$36/2117255219.run(Unknown Source)
... 1 more

最佳答案

您将 hbox 添加到 pane 两次:一次使用 pane.getChildren().add(...); 和一次使用 pane.setBottom(...);。这会导致 hbox 在场景图中出现两次,从而给出“重复子项”错误。您可能只需要 pane.setBottom(...); 版本。

关于java - 在 BorderPane 中设置节点位置时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34124275/

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