[英]Error when setting position of node in BorderPane
每当我编译程序时,都会出现以下错误:java.lang.reflect.InvocationTargetException当我将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(...);
版。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.