java.lang.IllegalArgumentException:Children:添加了重复的子项:parent = VBox @ 872be7 [英] java.lang.IllegalArgumentException: Children: duplicate children added: parent = VBox@872be7

查看:115
本文介绍了java.lang.IllegalArgumentException:Children:添加了重复的子项:parent = VBox @ 872be7的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从MySQL数据库中读取一些值并将其显示在JavaFX的菜单中。我的代码如下。以前我使用Buttons而不是Menu。它运行良好,但我用Buttons感觉不太好,所以我决定使用Menu。当我运行此代码时,我得到了标题中提到的异常。

Am trying to read some values from MySQL database and display it in a Menu in JavaFX. My code is below. Previously I used Buttons instead of Menu. It worked fine, but I don't felt better with Buttons, so I decided to use Menu. I got the exception mentioned in the heading when I Ran this code.

private VBox userSelection() throws ClassNotFoundException, SQLException {

    VBox vb1 = new VBox();
    vb1.setPadding(new Insets(40, 150, 20, 200));
    vb1.setSpacing(20);

    MenuBar menuBar = new MenuBar();
    Menu menuFile1 = new Menu("CHOOSE YOUR ACCOUNT");
    menuFile1.setStyle("-fx-border-color: green; -fx-font-size: 14pt; "
            + "-fx-font-family: Comic Sans MS; -fx-padding: 1 10 1 1");

    Text scenetitle2 = new Text("Choose Your Account");
    scenetitle2.setFont(Font.font("Tahoma", FontWeight.BOLD, 20));
    scenetitle2.setUnderline(true);
    vb1.getChildren().addAll(scenetitle2);

    Class.forName("com.mysql.jdbc.Driver");
    connect = DriverManager
            .getConnection("jdbc:mysql://localhost:3306/project?"
                    + "user=root&password=virus");
    statement = connect.createStatement();

    rs = statement.executeQuery("select * from user");

    while (rs.next()) {

        String username = rs.getString("staffname");

        MenuItem add = new MenuItem(username);
        add.setStyle(" -fx-font-size: 18pt; "
            + "-fx-font-family: Calibri; -fx-padding: 1 10 1 1");
        menuFile1.getItems().addAll(add);
        menuBar.getMenus().addAll(menuFile1);

        vb1.getChildren().addAll(menuBar);
    }

    return vb1;
}

例外情况如下:

java.lang.IllegalArgumentException: Children: duplicate children added: parent = VBox@872be7
at javafx.scene.Parent$1.onProposedChange(Parent.java:307)
at com.sun.javafx.collections.VetoableObservableList.addAll(VetoableObservableList.java:106)
at com.sun.javafx.collections.ObservableListWrapper.addAll(ObservableListWrapper.java:160)
at com.sun.javafx.collections.ObservableListWrapper.addAll(ObservableListWrapper.java:309)
at frontpage.FrontPage.userSelection(FrontPage.java:466)
at frontpage.FrontPage.access$100(FrontPage.java:44)
at frontpage.FrontPage$2.handle(FrontPage.java:129)
at frontpage.FrontPage$2.handle(FrontPage.java:125)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:69)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:217)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:170)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:38)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:37)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:53)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:28)
at javafx.event.Event.fireEvent(Event.java:171)
at javafx.scene.Node.fireEvent(Node.java:6867)
at javafx.scene.control.Button.fire(Button.java:179)
at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:193)
at com.sun.javafx.scene.control.skin.SkinBase$4.handle(SkinBase.java:336)
at com.sun.javafx.scene.control.skin.SkinBase$4.handle(SkinBase.java:329)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:64)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:217)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:170)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:38)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:37)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:53)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:33)
at javafx.event.Event.fireEvent(Event.java:171)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3311)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3151)
at javafx.scene.Scene$MouseHandler.access$1900(Scene.java:3106)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1563)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2248)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:250)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:173)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:292)
at com.sun.glass.ui.View.handleMouseEvent(View.java:530)
at com.sun.glass.ui.View.notifyMouse(View.java:924)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.access$100(WinApplication.java:17)
at com.sun.glass.ui.win.WinApplication$3$1.run(WinApplication.java:67)
at java.lang.Thread.run(Thread.java:744)
Exception in thread "JavaFX Application Thread" 

此异常是如何发生的?我该如何纠正?

How this exception occured ? How can I correct it ?

推荐答案

每次遍历结果集时,都会添加相同的菜单( menuFile1 )到菜单栏。您无法多次添加相同的菜单。

Every time you iterate through your result set, you are adding the same menu (menuFile1) to the menu bar. You can't add the same menu multiple times.

(如果你只看堆栈跟踪,你可以想出来;我假设第466行必须是:

(If you just look at the stack trace, you can figure that out; I assume line 466 must be:

menuBar.getMenus().addAll(menuFile1);

只需将菜单添加到 MenuBar 一次,然后在循环内添加不同的菜单项。

Just add the menu to the MenuBar once, then add the different menu items to it inside the loop.

同样,出于某种原因,您反复将菜单栏添加到 VBox 。只需在循环外添加一次。

Similarly, for some reason, you repeatedly add the menu bar to the VBox. Just add it once, outside the loop.

这篇关于java.lang.IllegalArgumentException:Children:添加了重复的子项:parent = VBox @ 872be7的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆