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

标签 java javafx

每当我编译程序时,我都会收到此错误: 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/

相关文章:

scala - 为什么 sbt 第二次运行 gui 应用程序失败?

java - JFrame 上的背景图像

java - 如何从文件中获取数组输入,对其进行排序并在同一文件中显示输出

java - 单击按钮时如何重新启动 JavaFX 应用程序

CSS 选择器是 JavaFX setStyle() 方法

java - 如何使选项卡 Pane 选项卡在面向左侧时旋转并可见?

java - 在 IntelliJ 的 resources 文件夹中创建包

java - 了解链表 (Java)

java - 创建数据库 View 与创建 Hibernate 映射 - 性能

java - 我可以从 <filter-mapping> 中的 <url-pattern> 中排除一些具体的 url 吗?