首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >将HBox放在BorderPane中会引发start方法JavaFX中的异常

将HBox放在BorderPane中会引发start方法JavaFX中的异常
EN

Stack Overflow用户
提问于 2020-02-18 23:33:09
回答 1查看 42关注 0票数 1

我正在尝试制作一个由一个TreeView列表和几个文本字段组成的布局,按钮在底部。我想我应该使用BorderPane来保持我的布局。但是我得到了一个Exception in Application start method,它还告诉我Children: duplicate children added: parent = BorderPane@71e344e8

你们知道怎么回事吗?

代码语言:javascript
运行
复制
 public class Main extends Application {

        Stage window;
        TextField nameInput, quantityInput;
        TreeView<String> tree;

        public static void main(String[] args) {
            launch(args);
        }

        public void start (Stage primaryStage) throws Exception{

            window = primaryStage;
            window.setTitle("GrocerieList");

            // tree view
            TreeItem<String> root, food, drink;

            root = new TreeItem<>();
            root.setExpanded(true);

            // food
            food = makeBranch("Food",root);
            makeBranch("Banana", food);
            makeBranch("Coconut", food);
            makeBranch("Eggs", food);

            // drink
            drink = makeBranch("Drinks",root);
            makeBranch("Water", drink);
            makeBranch("Fanta", drink);
            makeBranch("Beer", drink);

            // tree
            tree = new TreeView<>(root);
            tree.setShowRoot(false);

            // bottom input fields
            nameInput = new TextField();
            nameInput.setPromptText("Productname");
            nameInput.setMinWidth(100);

            quantityInput = new TextField();
            quantityInput.setPromptText("Quantity");
            quantityInput.setMinWidth(100);

            // buttons
            Button addbutton = new Button("Add");

            // Layout
            // HBOX
            HBox hBox = new HBox();
            hBox.setPadding(new Insets(10));
            hBox.setSpacing(10);
            hBox.getChildren().addAll(nameInput, quantityInput, addbutton);

            // BorderPane
            BorderPane borderPane = new BorderPane();
            borderPane.getChildren().addAll(tree, hBox);
            borderPane.setCenter(tree);
            borderPane.setBottom(hBox);

            Scene scene = new Scene(borderPane, 520, 620);
            window.setScene(scene);
            window.show();

        }
        public TreeItem<String> makeBranch(String title, TreeItem<String> parent){
            TreeItem<String> item = new TreeItem<>(title);
            item.setExpanded(true);
            parent.getChildren().add(item);
            return item;
        }

完全错误:

代码语言:javascript
运行
复制
    Exception in Application start method
java.lang.reflect.InvocationTargetException
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:567)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:464)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:363)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:567)
    at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1051)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:900)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:195)
    at java.base/java.lang.Thread.run(Thread.java:830)
Caused by: java.lang.IllegalArgumentException: Children: duplicate children added: parent = BorderPane@71e344e8
    at javafx.graphics/javafx.scene.Parent$3.onProposedChange(Parent.java:560)
    at javafx.base/com.sun.javafx.collections.VetoableListDecorator.add(VetoableListDecorator.java:206)
    at javafx.graphics/javafx.scene.layout.BorderPane$BorderPositionProperty.invalidated(BorderPane.java:692)
    at javafx.base/javafx.beans.property.ObjectPropertyBase.markInvalid(ObjectPropertyBase.java:112)
    at javafx.base/javafx.beans.property.ObjectPropertyBase.set(ObjectPropertyBase.java:147)
    at javafx.graphics/javafx.scene.layout.BorderPane.setCenter(BorderPane.java:268)
    at sample.Main.start(Main.java:70)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
    at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
Exception running application sample.Main
EN

回答 1

Stack Overflow用户

发布于 2020-02-18 23:39:59

你的问题在这里:

代码语言:javascript
运行
复制
BorderPane borderPane = new BorderPane();
borderPane.getChildren().addAll(tree, hBox);
borderPane.setCenter(tree);
borderPane.setBottom(hBox);

setCentersetBottom的调用将这些节点添加到子节点列表中。由于您已手动将两个节点添加到子节点列表中,因此这些方法调用会导致将同一节点添加到同一父节点中两次-这是不允许的。当涉及到为定位提供特殊属性的布局时,例如BorderPane,您希望避免直接与子列表交互。将您的代码更改为:

代码语言:javascript
运行
复制
BorderPane borderPane = new BorderPane();
borderPane.setCenter(tree);
borderPane.setBottom(hBox);
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60284414

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档